Ejemplo n.º 1
0
 /**
  * Public function that creates a single instance
  */
 public static function getInstance()
 {
     if (!isset(self::$_instance)) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Ejemplo n.º 2
0
 /**
  * Complete checkout and charge money.
  */
 public function completeCheckout()
 {
     global $language;
     $shop = shop::getInstance();
     $return_url = fix_chars($_REQUEST['return_url']);
     $recurring = isset($_REQUEST['type']) && $_REQUEST['type'] == 'recurring';
     $transaction_uid = $_SESSION['transaction']['uid'];
     // get billing information
     $billing = array();
     $fields = array('billing_full_name', 'billing_card_type', 'billing_credit_card', 'billing_expire_month', 'billing_expire_year', 'billing_cvv');
     foreach ($fields as $field) {
         if (isset($_REQUEST[$field])) {
             $billing[$field] = fix_chars($_REQUEST[$field]);
         }
     }
     // create recurring profile
     if ($recurring) {
         $request_id = 0;
         $plan_name = $_SESSION['recurring_plan'];
         $manager = PayPal_PlansManager::getInstance();
         $plan = $manager->getSingleItem($manager->getFieldNames(), array('text_id' => $plan_name));
         $current_plan = $shop->getRecurringPlan();
         // cancel existing recurring payment if exists
         if (!is_null($current_plan)) {
             $plans = $this->get_recurring_plans();
             $current_group = null;
             // get plan data
             if (isset($plans[$current_plan->plan_name])) {
                 $current_group = $plans[$current_plan->plan_name]['group'];
             }
             // cancel current plan
             if (!is_null($current_group) && $current_group == $plan->group_name) {
                 $shop->cancelTransaction($current_plan->transaction);
             }
         }
         // generate params for description
         $plan_params = array('price' => $plan->price, 'period' => $plan->interval_count, 'unit' => $plan->interval, 'setup' => $plan->setup_price, 'trial_period' => $plan->trial_count, 'trial_unit' => $plan->trial);
         // charge one time setup fee
         // TODO: Charge one time setup fee.
         // create recurring payments profile
         $recurring_fields = $fields;
         // set buyer information
         $name = explode(' ', $billing['billing_full_name']);
         $recurring_fields['CREDITCARDTYPE'] = $this->card_type[$billing['billing_card_type']];
         $recurring_fields['ACCT'] = $billing['billing_credit_card'];
         $recurring_fields['EXPDATE'] = $billing['billing_expire_month'] . $billing['billing_expire_year'];
         $recurring_fields['FIRSTNAME'] = $name[0];
         $recurring_fields['LASTNAME'] = $name[1];
         // set starting date of the profile
         $start_timestamp = strtotime($plan->start_time);
         if ($start_timestamp < time()) {
             $start_timestamp = time();
         }
         $recurring_fields['PROFILESTARTDATE'] = strftime('%Y-%m-%dT%T%z', $start_timestamp);
         // set description
         $recurring_fields['DESC'] = $shop->formatRecurring($plan_params);
         // set currency
         $recurring_fields['AMT'] = $plan->price;
         $recurring_fields['CURRENCYCODE'] = $shop->getDefaultCurrency();
         // billing period
         $recurring_fields['BILLINGPERIOD'] = $this->units[$plan->interval];
         $recurring_fields['BILLINGFREQUENCY'] = $plan->interval_count;
         // trial period
         if ($plan->trial_count > 0) {
             $recurring_fields['TRIALBILLINGPERIOD'] = $this->units[$plan->trial];
             $recurring_fields['TRIALBILLINGFREQUENCY'] = $plan->trial_count;
             $recurring_fields['TRIALTOTALBILLINGCYCLES'] = 1;
         }
         // make api call
         $response = PayPal_Helper::callAPI(PayPal_Helper::METHOD_CreateRecurringPaymentsProfile, $recurring_fields);
         if ($response['ACK'] == 'Success' || $response['ACK'] == 'SuccessWithWarning') {
             // update transaction token
             $shop->setTransactionToken($transaction_uid, fix_chars($response['PROFILEID']));
             // update transaction status
             if ($response['PROFILESTATUS'] == 'ActiveProfile') {
                 $shop->setTransactionStatus($transaction_uid, TransactionStatus::COMPLETED);
             }
         } else {
             // report error
             $error_code = urldecode($response['L_ERRORCODE0']);
             $error_long = urldecode($response['L_LONGMESSAGE0']);
             trigger_error("PayPal_Express: ({$error_code}) - {$error_long}", E_USER_ERROR);
         }
         // redirect user
         header('Location: ' . $return_url, true, 302);
     }
 }
Ejemplo n.º 3
0
 /**
  * Tag handler for drawing multiple plans.
  *
  * @param array $tag_params
  * @param array $children
  */
 public function tag_PlanList($tag_params, $children)
 {
     $manager = PayPal_PlansManager::getInstance();
     $conditions = array();
     $template = $this->loadTemplate($tag_params, 'plans_list_item.xml');
     $items = $manager->getItems($manager->getFieldNames(), $conditions);
     if (count($items) > 0) {
         foreach ($items as $item) {
             $params = array('id' => $item->id, 'text_id' => $item->text_id, 'name' => $item->name, 'trial' => $item->trial, 'trial_count' => $item->trial_count, 'interval' => $item->interval, 'interval_count' => $item->interval_count, 'price' => $item->price, 'setup_price' => $item->setup_price, 'start_time' => $item->start_time, 'group_name' => $item->group_name, 'item_change' => url_MakeHyperlink($this->getLanguageConstant('change'), window_Open('paypal_recurring_plans_change', 405, $this->getLanguageConstant('title_recurring_plans_change'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'recurring_plans_change'), array('id', $item->id)))), 'item_delete' => url_MakeHyperlink($this->getLanguageConstant('delete'), window_Open('paypal_recurring_plans_delete', 400, $this->getLanguageConstant('title_recurring_plans_delete'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'recurring_plans_delete'), array('id', $item->id)))));
             $template->restoreXML();
             $template->setLocalParams($params);
             $template->parse();
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Complete checkout and charge money.
  */
 public function completeCheckout()
 {
     global $language;
     // prepare data for new recurring profile
     $shop = shop::getInstance();
     $token = escape_chars($_REQUEST['token']);
     $payer_id = escape_chars($_REQUEST['payer_id']);
     $return_url = fix_chars($_REQUEST['return_url']);
     $recurring = isset($_REQUEST['type']) && $_REQUEST['type'] == 'recurring';
     $transaction_uid = $_SESSION['transaction']['uid'];
     // get buyer information
     $fields = array('TOKEN' => $token);
     $response = PayPal_Helper::callAPI(PayPal_Helper::METHOD_GetExpressCheckoutDetails, $fields);
     // update transaction status and buyer
     if ($response['ACK'] == 'Success' || $response['ACK'] == 'SuccessWithWarning') {
         $buyer = array('first_name' => $response['FIRSTNAME'], 'last_name' => $response['LASTNAME'], 'email' => $response['EMAIL'], 'uid' => $response['PAYERID']);
         $shop->updateBuyerInformation($transaction_uid, $buyer);
     } else {
         // report error
         $error_code = urldecode($response['L_ERRORCODE0']);
         $error_long = urldecode($response['L_LONGMESSAGE0']);
         trigger_error("PayPal_Express: ({$error_code}) - {$error_long}", E_USER_ERROR);
     }
     // create recurring profile
     if ($recurring) {
         $request_id = 0;
         $plan_name = $_SESSION['recurring_plan'];
         $manager = PayPal_PlansManager::getInstance();
         $plan = $manager->getSingleItem($manager->getFieldNames(), array('text_id' => $plan_name));
         $current_plan = $shop->getRecurringPlan();
         // cancel existing recurring payment if exists
         if (!is_null($current_plan)) {
             $plans = $this->get_recurring_plans();
             $current_group = null;
             // get plan data
             if (isset($plans[$current_plan->plan_name])) {
                 $current_group = $plans[$current_plan->plan_name]['group'];
             }
             // cancel current plan
             if (!is_null($current_group) && $current_group == $plan->group_name) {
                 $shop->cancelTransaction($current_plan->transaction);
             }
         }
         // generate params for description
         $plan_params = array('price' => $plan->price, 'period' => $plan->interval_count, 'unit' => $plan->interval, 'setup' => $plan->setup_price, 'trial_period' => $plan->trial_count, 'trial_unit' => $plan->trial);
         // charge one time setup fee
         if (is_object($plan) && $plan->setup_price > 0) {
             $setup_fields = $fields;
             $setup_fields["PAYMENTREQUEST_{$request_id}_AMT"] = $plan->setup_price;
             $setup_fields["PAYMENTREQUEST_{$request_id}_CURRENCYCODE"] = $shop->getDefaultCurrency();
             $setup_fields["PAYMENTREQUEST_{$request_id}_DESC"] = $this->parent->getLanguageConstant('api_setup_fee');
             $setup_fields["PAYMENTREQUEST_{$request_id}_INVNUM"] = $_SESSION['transaction']['uid'];
             $setup_fields["PAYMENTREQUEST_{$request_id}_PAYMENTACTION"] = 'Sale';
             $response = PayPal_Helper::callAPI(PayPal_Helper::METHOD_DoExpressCheckoutPayment, $setup_fields);
         }
         // create recurring payments profile
         $recurring_fields = $fields;
         // set starting date of the profile
         $start_timestamp = strtotime($plan->start_time);
         if ($start_timestamp < time()) {
             $start_timestamp = time();
         }
         $recurring_fields['PROFILESTARTDATE'] = strftime('%Y-%m-%dT%T%z', $start_timestamp);
         $recurring_fields['PAYERID'] = $payer_id;
         // set description
         $recurring_fields['DESC'] = $shop->formatRecurring($plan_params);
         // set currency
         $recurring_fields['AMT'] = $plan->price;
         $recurring_fields['CURRENCYCODE'] = $shop->getDefaultCurrency();
         // billing period
         $recurring_fields['BILLINGPERIOD'] = $this->units[$plan->interval];
         $recurring_fields['BILLINGFREQUENCY'] = $plan->interval_count;
         // trial period
         if ($plan->trial_count > 0) {
             $recurring_fields['TRIALBILLINGPERIOD'] = $this->units[$plan->trial];
             $recurring_fields['TRIALBILLINGFREQUENCY'] = $plan->trial_count;
             $recurring_fields['TRIALTOTALBILLINGCYCLES'] = 1;
         }
         // make api call
         $response = PayPal_Helper::callAPI(PayPal_Helper::METHOD_CreateRecurringPaymentsProfile, $recurring_fields);
         if ($response['ACK'] == 'Success' || $response['ACK'] == 'SuccessWithWarning') {
             // update transaction token
             $shop->setTransactionToken($transaction_uid, fix_chars($response['PROFILEID']));
             // update transaction status
             if ($response['PROFILESTATUS'] == 'ActiveProfile') {
                 $shop->setTransactionStatus($transaction_uid, TransactionStatus::COMPLETED);
             }
         } else {
             // report error
             $error_code = urldecode($response['L_ERRORCODE0']);
             $error_long = urldecode($response['L_LONGMESSAGE0']);
             trigger_error("PayPal_Express: ({$error_code}) - {$error_long}", E_USER_ERROR);
         }
         // redirect user
         header('Location: ' . $return_url, true, 302);
     }
 }