Use PayPal Express
Overview
Use the getPayPalExpressCheckoutRedirectURL method to retrieve the RedirectURL by sending an Order object with PAYPAL_EXPRESS payment method.
Parameters
Parameters | Type/Description |
---|---|
sessionID |
Required (string) |
|
Session identifier, the output of the Login method. Include sessionID into all your requests. 2Checkout throws an exception if the values are incorrect. The sessionID expires in 10 minutes. |
Required (Object) |
|
|
Object designed to collect all data necessary for an order, including billing, product/subscription plan and payment details. Use PAYPAL_EXPRESS as the payment method. |
Response
Parameters | Type/Description |
---|---|
PayPalExpressCheckoutRedirectURL |
String |
Retrieve PayPal redirect URL
<? require ('PATH_TO_AUTH'); $Order = new stdClass(); $Order->Language = 'en'; $Order->CustomerReference = 'APITEST'; // set customer reference (optional) $Order->Currency = 'EUR'; $Order->Items = array(); $Order->Items[0] = new stdClass(); $Order->Items[0]->Code = 'my_subscription_1'; // send the product code $Order->Items[0]->Quantity = 1; $Order->PaymentDetails = new stdClass(); $Order->PaymentDetails->Type = 'PAYPAL_EXPRESS'; // payment method, must be PAYPAL_EXPRESS for the express flow to take place. $Order->PaymentDetails->Currency = 'EUR'; $Order->PaymentDetails->CustomerIP = '91.220.121.21'; $PayPalExpress = new stdClass(); $PayPalExpress->Email='customer@email.com'; $PayPalExpress->ReturnURL = 'http://' . $_SERVER['HTTP_HOST'] . '/api/place_order_api_json_paypal_express_response.php'; $PayPalExpress->CancelURL = 'http://' . $_SERVER['HTTP_HOST'] . '/api/place_order_api_json_paypal_express_response.php' . '?cancel=true'; $Order->PaymentDetails->PaymentMethod = $PayPalExpress; // Call the method for retrieving Express Checkout redirect URL $jsonRpcRequest = new stdClass(); $jsonRpcRequest->jsonrpc = '2.0'; $jsonRpcRequest->method = 'getPayPalExpressCheckoutRedirectURL'; $jsonRpcRequest->params = array($sessionID, $Order); $jsonRpcRequest->id = $i++; $redirectUrl = callRPC($jsonRpcRequest, $host); header('Location:' . $redirectUrl);
Place order with PayPal Express
<?php declare(strict_types=1); class Configuration { public const MERCHANT_CODE = ''; public const MERCHANT_KEY = ''; public const URL = 'http://api.2checkout.com/rpc/6.0'; public const ACTION = 'placeOrder'; public const ADDITIONAL_OPTIONS = null; //array or JSON public const PAYLOAD = <<<JSON { "Country": "us", "Currency": "USD", "CustomerIP": "91.220.121.21", "ExternalReference": "RPC_API_AVANGTE", "Language": "en", "Source": "testAPI.com", "BillingDetails": { "Address1": "Test Address", "City": "LA", "State": "California", "CountryCode": "US", "Email": "testcustomer@2Checkout.com", "FirstName": "Customer", "LastName": "2Checkout", "Zip": "12345" }, "Items": [ { "Code": "A90B3D8FDE", "Quantity": 1 } ], "PaymentDetails": { "Currency": "USD", "CustomerIP": "91.220.121.21", "PaymentMethod": { "Email": "customer@example.com", "Token": "token-gotten-from-getPayPalExpressCheckoutRedirectURL", "ReturnURL": "http://secure.avangate.local/test/index.php", "CancelURL": "http://secure.avangate.local/test/create_order.php" }, "Type": "PAYPAL_EXPRESS" } } JSON; } class Client { private const LOGIN_METHOD = 'login'; private $calls = 1; private $sessionId; private function generateAuth(): array { $merchantCode = Configuration::MERCHANT_CODE; $key = Configuration::MERCHANT_KEY; $date = gmdate('Y-m-d H:i:s'); $string = strlen($merchantCode) . $merchantCode . strlen($date) . $date; $hash = hash_hmac('md5', $string, $key); return compact('merchantCode', 'date', 'hash'); } public function login(string $url) { $payload = $this->generateAuth(); $response = $this->call($url, array_values($payload), self::LOGIN_METHOD); $this->sessionId = $response['result']; } public function call( string $url = Configuration::URL, $payload = Configuration::PAYLOAD, string $action = Configuration::ACTION ): ?array { if (empty($this->sessionId) && $action !== self::LOGIN_METHOD) { $this->login($url); } if(is_string($payload)) { $payload = json_decode($payload, true); } if (!empty($this->sessionId)) { $payload = [$this->sessionId, $payload, Configuration::ADDITIONAL_OPTIONS]; } $payload = array_filter($payload); $request = json_encode([ 'jsonrpc' => '2.0', 'method' => $action, 'params' => $payload, 'id' => $this->calls++, ]); $curl = curl_init($url); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSLVERSION, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json', 'Cookie: XDEBUG_SESSION=PHPSTORM')); curl_setopt($curl, CURLOPT_POSTFIELDS, $request); $response = curl_exec($curl); if(empty($response)) { die('Server unavailable'); } echo $response . '</br>'; return json_decode($response, true);; } } $client = new Client(); $result = $client->call(); var_dump($result);
Workflow
- Authentication via login API method.
- Retrieve PayPal redirect URL. When you retrieve the PayPal redirect URL, apart from the token you will also receive 2 parameters that are encoded: billingDetails and deliveryDetails. These have to be base64_decoded and the information should be used in the placeOrder API call (email, billing address etc.).
- Place the order with PayPal Express using the token sent by PayPal.