2015-06-21 03:30:38 +00:00
|
|
|
<?php
|
|
|
|
// #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__ . '/../bootstrap.php';
|
|
|
|
use PayPal\Api\ExecutePayment;
|
|
|
|
use PayPal\Api\Payment;
|
|
|
|
use PayPal\Api\PaymentExecution;
|
|
|
|
|
|
|
|
// ### Approval Status
|
|
|
|
// Determine if the user approved the payment or not
|
|
|
|
if (isset($_GET['success']) && $_GET['success'] == 'true') {
|
|
|
|
|
|
|
|
// Get the payment Object by passing paymentId
|
|
|
|
// payment id was previously stored in session in
|
|
|
|
// CreatePaymentUsingPayPal.php
|
|
|
|
$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();
|
|
|
|
$execution->setPayerId($_GET['PayerID']);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Execute the payment
|
|
|
|
// (See bootstrap.php for more on `ApiContext`)
|
|
|
|
$result = $payment->execute($execution, $apiContext);
|
|
|
|
|
2015-07-01 00:16:22 +00:00
|
|
|
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
2015-06-21 03:30:38 +00:00
|
|
|
ResultPrinter::printResult("Executed Payment", "Payment", $payment->getId(), $execution, $result);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$payment = Payment::get($paymentId, $apiContext);
|
|
|
|
} catch (Exception $ex) {
|
2015-07-01 00:16:22 +00:00
|
|
|
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
|
|
|
ResultPrinter::printError("Get Payment", "Payment", null, null, $ex);
|
2015-06-21 03:30:38 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} catch (Exception $ex) {
|
2015-07-01 00:16:22 +00:00
|
|
|
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
|
|
|
ResultPrinter::printError("Executed Payment", "Payment", null, null, $ex);
|
2015-06-21 03:30:38 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:16:22 +00:00
|
|
|
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
2015-06-21 03:30:38 +00:00
|
|
|
ResultPrinter::printResult("Get Payment", "Payment", $payment->getId(), null, $payment);
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
2015-07-01 00:16:22 +00:00
|
|
|
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
|
2015-06-21 03:30:38 +00:00
|
|
|
ResultPrinter::printResult("User Cancelled the Approval", null);
|
|
|
|
exit;
|
|
|
|
}
|