/**
  * tests the stage_repost function
  * Assertions:
  * 		Garbage in, defaults out.
  * 		The recurring_paypal key is treated like a boolean
  */
 function testStageRepost()
 {
     $start = array('bears' => 'green', 'emus' => 'purple');
     ContributionTrackingProcessor::getLanguage(array('language' => 'en'));
     $expected = ContributionTrackingProcessor::getRepostDefaults();
     $expected['item_name'] = 'One-time donation';
     $expected['notify_url'] = 'https://civicrm.wikimedia.org/fundcore_gateway/paypal';
     $result = ContributionTrackingProcessor::stage_repost($start);
     $this->assertEquals($expected, $result, "Staged Repost with no defined fields should be exactly all the default values.");
     $additional = array('gateway' => 'testgateway', 'recurring_paypal' => 'raspberries', 'amount' => '6.60');
     $expected = array('gateway' => 'testgateway', 'tshirt' => false, 'size' => false, 'premium_language' => false, 'currency_code' => 'USD', 'return' => 'Donate-thanks/en', 'fname' => '', 'lname' => '', 'email' => '', 'recurring_paypal' => '1', 'amount' => '6.60', 'amount_given' => '', 'contribution_tracking_id' => '', 'notify_url' => 'https://civicrm.wikimedia.org/fundcore_gateway/paypal', 'item_name' => 'Recurring monthly donation');
     $result = ContributionTrackingProcessor::stage_repost($additional);
     $this->assertEquals($expected, $result, "Repost not staging properly.");
     unset($additional['recurring_paypal']);
     $expected['recurring_paypal'] = 0;
     $expected['item_name'] = 'One-time donation';
     $result = ContributionTrackingProcessor::stage_repost($additional);
     $this->assertEquals($expected, $result, "Repost not staging properly.");
 }
 /**
  * Takes staged transaction data, and constructs the key/value pairs
  * formatted to be reposted to the gateway specified in $input['gateway']
  * @global string $wgContributionTrackingPayPalBusiness 'Business' string
  * for PayPal: Defined in ContributionTracking.php
  * @global string $wgContributionTrackingReturnToURLDefault Default URL to
  * return to after the transaction was processed by the gateway. Used if
  * none supplied.
  * @param array $input The staged data to repost to a gateway.
  * @return array Key/value pairs, ready to be reposted to the specified
  * gateway to complete the transaction.
  */
 static function getRepostFields($input)
 {
     global $wgContributionTrackingPayPalBusiness, $wgContributionTrackingReturnToURLDefault, $wgContributionTrackingRPPLength;
     // Set the action and tracking ID fields
     $input = ContributionTrackingProcessor::stage_repost($input);
     $repost = array();
     $repost['action'] = 'http://wikimediafoundation.org/';
     $amount_field_name = 'amount';
     // the amount fieldname may be different depending on the service
     if ($input['gateway'] == 'paypal') {
         $repost['action'] = 'https://www.paypal.com/cgi-bin/webscr';
         // Premiums
         if (array_key_exists('tshirt', $input) && $input['tshirt']) {
             $repost['fields']['on0'] = 'Shirt size';
             $repost['fields']['os0'] = $input['size'];
             $repost['fields']['on1'] = 'Shirt language';
             $repost['fields']['os1'] = $input['premium_language'];
             $repost['fields']['no_shipping'] = 2;
         }
         // PayPal
         $repost['fields']['business'] = $wgContributionTrackingPayPalBusiness;
         $repost['fields']['item_number'] = 'DONATE';
         $repost['fields']['no_note'] = '0';
         $returnText = $input['return'];
         $returnTitle = Title::newFromText($returnText);
         if ($returnTitle) {
             $returnto = wfExpandUrl($returnTitle->getFullUrl(), PROTO_CURRENT);
         } else {
             $returnto = $wgContributionTrackingReturnToURLDefault . "/{$language}";
             // FIXME: $language is undefined
         }
         $repost['fields']['return'] = $returnto;
         $repost['fields']['currency_code'] = $input['currency_code'];
         // additional fields to pass to PayPal from single-step credit card form and 1st step with address fields
         if (array_key_exists('fname', $input) && !empty($input['fname'])) {
             $repost['fields']['first_name'] = $input['fname'];
         }
         if (array_key_exists('lname', $input) && !empty($input['lname'])) {
             $repost['fields']['last_name'] = $input['lname'];
         }
         if (array_key_exists('email', $input) && !empty($input['email'])) {
             $repost['fields']['email'] = $input['email'];
         }
         if (array_key_exists('address1', $input) && !empty($input['address1'])) {
             $repost['fields']['address1'] = $input['address1'];
         }
         if (array_key_exists('city', $input) && !empty($input['city'])) {
             $repost['fields']['city'] = $input['city'];
         }
         if (array_key_exists('state', $input) && !empty($input['state'])) {
             $repost['fields']['state'] = $input['state'];
         }
         if (array_key_exists('zip', $input) && !empty($input['zip'])) {
             $repost['fields']['zip'] = $input['zip'];
         }
         if (array_key_exists('country', $input) && !empty($input['country'])) {
             $repost['fields']['country'] = $input['country'];
         }
         if (array_key_exists('address_override', $input) && !empty($input['address_override'])) {
             $repost['fields']['address_override'] = $input['address_override'];
         }
         // if this is a recurring donation, we have add'l fields to send to paypal
         if ($input['recurring_paypal'] && $input['recurring_paypal'] != 0) {
             $repost['fields']['t3'] = "M";
             // The unit of measurement for for p3 (M = month)
             $repost['fields']['p3'] = '1';
             // Billing cycle duration
             $repost['fields']['srt'] = $wgContributionTrackingRPPLength;
             // # of billing cycles
             $repost['fields']['src'] = '1';
             // Make this 'recurring'
             $repost['fields']['sra'] = '1';
             // Turn on re-attempt on failure
             $repost['fields']['cmd'] = '_xclick-subscriptions';
             $amount_field_name = 'a3';
             $repost['fields']['notify_url'] = $input['notify_url'];
             $repost['fields']['item_name'] = $input['item_name'];
         } else {
             $repost['fields']['cmd'] = '_xclick';
             $repost['fields']['notify_url'] = $input['notify_url'];
             $repost['fields']['item_name'] = $input['item_name'];
         }
     } elseif ($input['gateway'] == 'moneybookers') {
         $repost['action'] = 'https://www.moneybookers.com/app/payment.pl';
         // Tracking
         $repost['fields']['merchant_fields'] = 'os0';
         // Moneybookers
         $repost['fields']['pay_to_email'] = '*****@*****.**';
         $repost['fields']['status_url'] = 'https://civicrm.wikimedia.org/fundcore_gateway/moneybookers';
         $repost['fields']['language'] = 'en';
         $repost['fields']['detail1_description'] = 'One-time donation';
         $repost['fields']['detail1_text'] = 'DONATE';
         $repost['fields']['currency'] = $input['currency_code'];
     } else {
         throw new MWException("Unknown payment gateway!");
     }
     // Normalized amount
     $amount = $input['amount'];
     // If amount is not a number, use amount_given
     if (!preg_match('/^\\d+(\\.(\\d+)?)?$/', $amount) && $input['amount_given']) {
         $amount = $input['amount_given'];
     }
     $repost['fields'][$amount_field_name] = $amount;
     // Tracking
     $repost['fields']['custom'] = $input['contribution_tracking_id'];
     return $repost;
 }