/** * Before checking out redirect user. * * @param string $method * @param string $return_url * @param string $cancel_url */ public function beforeCheckout($method, $return_url, $cancel_url) { global $language, $section, $action; $result = false; $fields = array(); $request_id = 0; $recurring_plan = isset($_SESSION['recurring_plan']) ? $_SESSION['recurring_plan'] : null; // only react in case right payment method is selected if ($method != $this->name) { return $result; } // add recurring payment plan if (!is_null($recurring_plan)) { $manager = PayPal_PlansManager::getInstance(); $shop = shop::getInstance(); $plan = $manager->getSingleItem($manager->getFieldNames(), array('text_id' => $recurring_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); if (is_object($plan)) { // prepare fields for initial negotiation $fields["PAYMENTREQUEST_{$request_id}_AMT"] = $plan->price; $fields["PAYMENTREQUEST_{$request_id}_CURRENCYCODE"] = $shop->getDefaultCurrency(); $fields["PAYMENTREQUEST_{$request_id}_DESC"] = $plan->name[$language]; $fields["PAYMENTREQUEST_{$request_id}_INVNUM"] = $_SESSION['transaction']['uid']; $fields["PAYMENTREQUEST_{$request_id}_PAYMENTACTION"] = 'Authorization'; $fields['L_BILLINGTYPE' . $request_id] = 'RecurringPayments'; $fields['L_BILLINGAGREEMENTDESCRIPTION' . $request_id] = $shop->formatRecurring($params); // add one time payment if ($plan->setup_price > 0) { $request_id++; $fields["PAYMENTREQUEST_{$request_id}_AMT"] = $plan->setup_price; $fields["PAYMENTREQUEST_{$request_id}_CURRENCYCODE"] = $shop->getDefaultCurrency(); $fields["PAYMENTREQUEST_{$request_id}_DESC"] = $this->parent->getLanguageConstant('api_setup_fee'); $fields["PAYMENTREQUEST_{$request_id}_INVNUM"] = $_SESSION['transaction']['uid']; $fields["PAYMENTREQUEST_{$request_id}_PAYMENTACTION"] = 'Sale'; } } } // TODO: Add other shop items. $return_url = url_Make($action, $section, array('stage', 'return'), array('payment_method', $this->name)); // add regular fields $fields['NOSHIPPING'] = 1; $fields['REQCONFIRMSHIPPING'] = 0; $fields['ALLOWNOTE'] = 0; $fields['RETURNURL'] = $return_url; $fields['CANCELURL'] = $cancel_url; // generate name-value pair string for sending $response = PayPal_Helper::callAPI(PayPal_Helper::METHOD_SetExpressCheckout, $fields); if (isset($response['ACK']) && $response['ACK'] == 'Success' || $response['ACK'] == 'SuccessWithWarning') { $result = true; $token = $response['TOKEN']; // store token for later use $_SESSION['paypal_token'] = $token; // redirect to paypal site PayPal_Helper::redirect(PayPal_Helper::COMMAND_ExpressCheckout, $token); } else { if (!is_null($response['ACK'])) { // 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); } } return $result; }