Use PayPal Express
Last updated: 03-Jul-2018
Rate this article:
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
require ('PATH_TO_AUTH');
// This is the start of the second script place_order_api_json_paypal_express_response.php
var_dump($_REQUEST);
if (isset($_GET['billingDetails'])) {
$billingDetails = base64_decode($_GET['billingDetails']);
parse_str($billingDetails, $billingDetailsArray);
}
if (isset($_GET['deliveryDetails'])) {
$deliveryDetails = base64_decode($_GET['deliveryDetails']);
parse_str($deliveryDetails, $deliveryDetailsArray);
}
if (isset($_GET['token'])) {
$token = $_GET['token'];
}
if (!empty($_GET['cancel']) && $_GET['cancel'] == true) {
echo 'canceled';
die();
} else {
echo 'success!';
}
$Billing = new stdClass();
$Billing->Country = empty($billingDetailsArray['Country']) ? '' : $billingDetailsArray['Country'];
$Billing->City = empty($billingDetailsArray['City']) ? '' : $billingDetailsArray['City'];
$Billing->State = empty($billingDetailsArray['State']) ? '' : $billingDetailsArray['State'];
$Billing->PostalCode = empty($billingDetailsArray['PostalCode']) ? '' : $billingDetailsArray['PostalCode'];
$Billing->Email = "customer@email.com";
$Billing->FirstName = empty($billingDetailsArray['FirstName']) ? '' : $billingDetailsArray['FirstName'];
$Billing->LastName = empty($billingDetailsArray['LastName']) ? '' : $billingDetailsArray['LastName'];
$Billing->Address = empty($billingDetailsArray['Address']) ? '' : $billingDetailsArray['Address'];
$Order->BillingDetails = $Billing;
$Delivery = new stdClass();
$Delivery->Country = empty($deliveryDetailsArray['Country']) ? '' : $deliveryDetailsArray['Country'];
$Delivery->City = empty($deliveryDetailsArray['City']) ? '' : $deliveryDetailsArray['City'];
$Delivery->State = empty($deliveryDetailsArray['State']) ? '' : $deliveryDetailsArray['State'];
$Delivery->FirstName = empty($deliveryDetailsArray['FirstName']) ? '' : $deliveryDetailsArray['FirstName'];
$Delivery->LastName = empty($deliveryDetailsArray['LastName']) ? '' : $deliveryDetailsArray['LastName'];
$Delivery->Address = empty($deliveryDetailsArray['Address']) ? '' : $deliveryDetailsArray['Address'];
$Delivery->PostalCode = empty($deliveryDetailsArray['PostalCode']) ? '' : $deliveryDetailsArray['PostalCode'];
$Order->DeliveryDetails = $Delivery;
$Payment = new stdClass();
$Payment->Type = 'PAYPAL_EXPRESS';
$Payment->Currency = 'EUR';
$Payment->CustomerIP = '91.220.121.21';
$PayPal = new stdClass();
$PayPal->Email='customer@email.com';
$PayPal->Token = $token;
$PayPal->ReturnURL = 'http://' . $_SERVER['HTTP_HOST'] . '/api/place_order_api_json_paypal_express_response.php1';
$PayPal->CancelURL = 'http://' . $_SERVER['HTTP_HOST'] . '/api/place_order_api_json_paypal_express_response.php1' . '?cancel=true';
$Payment->PaymentMethod = $PayPal;
$Order->PaymentDetails = $Payment;
$OrderField = new stdClass();
$OrderField->Code = 'ED5310';
$OrderField->Value = 'Value1';
// Call the method for placing the order
$jsonRpcRequest = new stdClass();
$jsonRpcRequest->jsonrpc = '2.0';
$jsonRpcRequest->method = 'placeOrder';
$jsonRpcRequest->params = array($sessionID, $NewOrder);
$jsonRpcRequest->id = $i++;
$APIOrderFullInfo = callRPC((Object)$jsonRpcRequest, $host);
Related articles
Rate this article: