/** * 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); } }
/** * Handle IPN. */ private function handleIPN() { if (!PayPal_Helper::validate_notification()) { trigger_error('PayPal: Invalid notification received. ' . json_encode($_POST), E_USER_WARNING); return; } // get objects $transaction_manager = ShopTransactionsManager::getInstance(); // get data $handled = false; $type = escape_chars($_POST['txn_type']); $amount = escape_chars($_POST['amount']); // handle different notification types switch ($type) { case 'recurring_payment': case 'recurring_payment_expired': case 'recurring_payment_failed': case 'recurring_payment_profile_created': case 'recurring_payment_profile_cancel': case 'recurring_payment_skipped': case 'recurring_payment_suspended': case 'recurring_payment_suspended_due_to_max_failed_payment': $profile_id = escape_chars($_REQUEST['recurring_payment_id']); $transaction = $transaction_manager->getSingleItem($transaction_manager->getFieldNames(), array('token' => $profile_id)); if (is_object($transaction)) { $handled = $this->handleRecurringIPN($transaction, $type, $amount); } else { trigger_error("PayPal: Unable to handle IPN, unknown transaction {$profile_id}.", E_USER_WARNING); } break; } // record unhandled notifications if (!$handled) { trigger_error("PayPal: Unhandled notification '{$type}'.", E_USER_NOTICE); } }
/** * Set API credentials. * * @param string $username * @param string $password * @param string $signature */ public static function setCredentials($username, $password, $signature) { self::$api_username = $username; self::$api_password = $password; self::$api_signature = $signature; }
/** * 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); } }