Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('error_reporting', E_ALL);
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- // #Execute Payment Sample
- // This is the second step required to complete
- // PayPal checkout. Once user completes the payment, paypal
- // redirects the browser to "redirectUrl" provided in the request.
- // This sample will show you how to execute the payment
- // that has been approved by
- // the buyer by logging into paypal site.
- // You can optionally update transaction
- // information by passing in one or more transactions.
- // API used: POST '/v1/payments/payment/<payment-id>/execute'.
- require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
- // After Step 1
- $apiContext = new \PayPal\Rest\ApiContext(
- new \PayPal\Auth\OAuthTokenCredential(
- 'GET_THIS_CODE_IN_DEVELOPER.PAYPAL.COM', // ClientID
- 'GET_THIS_CODE_IN_DEVELOPER.PAYPAL.COM' // ClientSecret
- )
- );
- use PayPal\Api\Amount;
- use PayPal\Api\Details;
- use PayPal\Api\ExecutePayment;
- use PayPal\Api\Payment;
- use PayPal\Api\PaymentExecution;
- use PayPal\Api\Transaction;
- // ### Approval Status
- // Determine if the user approved the payment or not
- if (isset($_GET['token'])) {
- // Get the payment Object by passing paymentId
- // payment id was previously stored in session in
- // CreatePaymentUsingPayPal.php
- $_GET['token'] = strtoupper($_GET['token']);
- if (isset($_GET['paymentId'])) { $paymentId = strtoupper($_GET['paymentId']); }
- if (isset($_GET['paymentid'])) { $paymentId = strtoupper($_GET['paymentid']); $_GET['paymentId'] = $paymentId;}
- //$paymentId = $_GET['paymentId'];
- $payment = Payment::get($paymentId, $apiContext);
- // ### Payment Execute
- // PaymentExecution object includes information necessary
- // to execute a PayPal account payment.
- // The payer_id is added to the request query parameters
- // when the user is redirected from paypal back to your site
- $execution = new PaymentExecution();
- if (isset($_GET['PayerID'])) { $temPayerId = strtoupper($_GET['PayerID']); }
- if (isset($_GET['payerid'])) { $temPayerId = strtoupper($_GET['payerid']); $_GET['PayerID'] = $temPayerId;}
- $execution->setPayerId(strtoupper($temPayerId));
- try {
- // Execute the payment
- // (See bootstrap.php for more on `ApiContext`)
- $result = $payment->execute($execution, $apiContext);
- // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
- //ResultPrinter::printResult("Executed Payment", "Payment", $payment->getId(), $execution, $result);
- print_r($payment->getId());
- echo "<h2>Your payment was <span>successfully proccessed!</span></h2>";
- //print_r($result);
- try {
- $payment = Payment::get($paymentId, $apiContext);
- } catch (Exception $ex) {
- // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
- // ResultPrinter::printError("Get Payment", "Payment", null, null, $ex);
- print_r($ex);
- exit(1);
- }
- } catch (Exception $ex) {
- // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
- //ResultPrinter::printError("Executed Payment", "Payment", null, null, $ex);
- print_r($ex);
- exit(1);
- }
- // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
- //ResultPrinter::printResult("Get Payment", "Payment", $payment->getId(), null, $payment);
- print_r($payment);
- return $payment;
- } else {
- // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
- echo "User Cancelled the Approval";
- exit;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement