/** * Send the Express Checkout NVP request * * @param $form_id * @throws Exception */ public function send_EC_Request($form_id) { if (!session_id()) { @session_start(); } if (!count($this->products)) { throw new Exception('Products not found!'); } $total = 0; // create the data to send on PayPal $data = '&SOLUTIONTYPE=' . 'Sole' . '&PAYMENTREQUEST_0_PAYMENTACTION=' . 'Sale' . '&PAYMENTREQUEST_0_CURRENCYCODE=' . urlencode(get_option('ab_paypal_currency')) . '&RETURNURL=' . urlencode(add_query_arg(array('action' => 'ab-paypal-returnurl', 'ab_fid' => $form_id), AB_Utils::getCurrentPageURL())) . '&CANCELURL=' . urlencode(add_query_arg(array('action' => 'ab-paypal-cancelurl', 'ab_fid' => $form_id), AB_Utils::getCurrentPageURL())); foreach ($this->products as $k => $product) { $data .= "&L_PAYMENTREQUEST_0_NAME{$k}=" . urlencode($product->name) . "&L_PAYMENTREQUEST_0_DESC{$k}=" . urlencode($product->desc) . "&L_PAYMENTREQUEST_0_AMT{$k}=" . urlencode($product->price) . "&L_PAYMENTREQUEST_0_QTY{$k}=" . urlencode($product->qty); $total += $product->qty * $product->price; } $data .= "&PAYMENTREQUEST_0_AMT=" . urlencode($total) . "&PAYMENTREQUEST_0_ITEMAMT=" . urlencode($total); // send the request to PayPal $response = self::sendNvpRequest('SetExpressCheckout', $data); //Respond according to message we receive from Paypal if ("SUCCESS" == strtoupper($response["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($response["ACK"])) { $_SESSION['bookly'][$form_id]['paypal_response'] = array($response, $form_id); $paypalurl = 'https://www' . get_option('ab_paypal_ec_mode') . '.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=' . urldecode($response["TOKEN"]); header('Location: ' . $paypalurl); exit; } else { header('Location: ' . wp_sanitize_redirect(add_query_arg(array('action' => 'ab-paypal-errorurl', 'ab_fid' => $form_id, 'error_msg' => $response["L_LONGMESSAGE0"]), AB_Utils::getCurrentPageURL()))); exit; } }
/** * Process the Express Checkout RETURNURL */ public function paypalResponseSuccess() { $form_id = $_GET['ab_fid']; $paypal = new AB_PayPal(); if (isset($_GET["token"]) && isset($_GET["PayerID"])) { $token = $_GET["token"]; $payer_id = $_GET["PayerID"]; // send the request to PayPal $response = $paypal->sendNvpRequest('GetExpressCheckoutDetails', sprintf('&TOKEN=%s', $token)); if (strtoupper($response["ACK"]) == "SUCCESS") { $data = sprintf('&TOKEN=%s&PAYERID=%s&PAYMENTREQUEST_0_PAYMENTACTION=Sale', $token, $payer_id); // response keys containing useful data to send via DoExpressCheckoutPayment operation $response_data_keys_pattern = sprintf('/^(%s)/', implode('|', array('PAYMENTREQUEST_0_AMT', 'PAYMENTREQUEST_0_ITEMAMT', 'PAYMENTREQUEST_0_CURRENCYCODE', 'L_PAYMENTREQUEST_0'))); foreach ($response as $key => $value) { // collect product data from response using defined response keys if (preg_match($response_data_keys_pattern, $key)) { $data .= sprintf('&%s=%s', $key, $value); } } //We need to execute the "DoExpressCheckoutPayment" at this point to Receive payment from user. $response = $paypal->sendNvpRequest('DoExpressCheckoutPayment', $data); if ("SUCCESS" == strtoupper($response["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($response["ACK"])) { // get transaction info $response = $paypal->sendNvpRequest('GetTransactionDetails', "&TRANSACTIONID=" . urlencode($response["PAYMENTINFO_0_TRANSACTIONID"])); if ("SUCCESS" == strtoupper($response["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($response["ACK"])) { // need session to get Total and Token $token = $_SESSION['bookly'][$form_id]['paypal_response'][0]['TOKEN']; $userData = new AB_UserBookingData($form_id); $userData->load(); if ($userData->get('service_id')) { $appointment = $userData->save(); $customer_appointment = new AB_CustomerAppointment(); $customer_appointment->loadBy(array('appointment_id' => $appointment->get('id'), 'customer_id' => $userData->getCustomerId())); $payment = new AB_Payment(); $payment->set('token', urldecode($token)); $payment->set('total', $userData->getFinalServicePrice() * $userData->get('number_of_persons')); $payment->set('customer_appointment_id', $customer_appointment->get('id')); $payment->set('transaction', urlencode($response["TRANSACTIONID"])); $payment->set('created', current_time('mysql')); $payment->save(); $userData->setPayPalStatus('success'); } @wp_redirect(remove_query_arg(array('action', 'token', 'PayerID', 'ab_fid'), AB_Utils::getCurrentPageURL())); exit(0); } else { header('Location: ' . wp_sanitize_redirect(add_query_arg(array('action' => 'ab-paypal-errorurl', 'ab_fid' => $form_id, 'error_msg' => $response["L_LONGMESSAGE0"]), AB_Utils::getCurrentPageURL()))); exit; } } else { header('Location: ' . wp_sanitize_redirect(add_query_arg(array('action' => 'ab-paypal-errorurl', 'ab_fid' => $form_id, 'error_msg' => $response["L_LONGMESSAGE0"]), AB_Utils::getCurrentPageURL()))); exit; } } else { header('Location: ' . wp_sanitize_redirect(add_query_arg(array('action' => 'ab-paypal-errorurl', 'ab_fid' => $form_id, 'error_msg' => 'Invalid token provided'), AB_Utils::getCurrentPageURL()))); exit; } } else { throw new Exception('Token parameter not found!'); } }