Advertisement
GenRIh

Execute payment php code

Jan 28th, 2017
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2. ini_set('error_reporting', E_ALL);
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5.  
  6.  
  7. // #Execute Payment Sample
  8. // This is the second step required to complete
  9. // PayPal checkout. Once user completes the payment, paypal
  10. // redirects the browser to "redirectUrl" provided in the request.
  11. // This sample will show you how to execute the payment
  12. // that has been approved by
  13. // the buyer by logging into paypal site.
  14. // You can optionally update transaction
  15. // information by passing in one or more transactions.
  16. // API used: POST '/v1/payments/payment/<payment-id>/execute'.
  17. require __DIR__  . '/PayPal-PHP-SDK/autoload.php';
  18.  
  19. // After Step 1
  20. $apiContext = new \PayPal\Rest\ApiContext(
  21.     new \PayPal\Auth\OAuthTokenCredential(
  22.         'GET_THIS_CODE_IN_DEVELOPER.PAYPAL.COM',     // ClientID
  23.         'GET_THIS_CODE_IN_DEVELOPER.PAYPAL.COM'      // ClientSecret
  24.     )
  25. );
  26.  
  27. use PayPal\Api\Amount;
  28. use PayPal\Api\Details;
  29. use PayPal\Api\ExecutePayment;
  30. use PayPal\Api\Payment;
  31. use PayPal\Api\PaymentExecution;
  32. use PayPal\Api\Transaction;
  33.  
  34.  
  35.  
  36.  
  37. // ### Approval Status
  38. // Determine if the user approved the payment or not
  39. if (isset($_GET['token'])) {
  40.     // Get the payment Object by passing paymentId
  41.     // payment id was previously stored in session in
  42.     // CreatePaymentUsingPayPal.php
  43.  
  44.     $_GET['token'] = strtoupper($_GET['token']);
  45.  
  46.     if (isset($_GET['paymentId'])) { $paymentId = strtoupper($_GET['paymentId']); }
  47.     if (isset($_GET['paymentid'])) { $paymentId = strtoupper($_GET['paymentid']); $_GET['paymentId'] = $paymentId;}
  48.  
  49.     //$paymentId = $_GET['paymentId'];
  50.     $payment = Payment::get($paymentId, $apiContext);
  51.     // ### Payment Execute
  52.     // PaymentExecution object includes information necessary
  53.     // to execute a PayPal account payment.
  54.     // The payer_id is added to the request query parameters
  55.     // when the user is redirected from paypal back to your site
  56.     $execution = new PaymentExecution();
  57.  
  58.     if (isset($_GET['PayerID'])) { $temPayerId = strtoupper($_GET['PayerID']); }
  59.     if (isset($_GET['payerid'])) { $temPayerId = strtoupper($_GET['payerid']); $_GET['PayerID'] = $temPayerId;}
  60.  
  61.     $execution->setPayerId(strtoupper($temPayerId));
  62.     try {
  63.         // Execute the payment
  64.         // (See bootstrap.php for more on `ApiContext`)
  65.         $result = $payment->execute($execution, $apiContext);
  66.         // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
  67.         //ResultPrinter::printResult("Executed Payment", "Payment", $payment->getId(), $execution, $result);
  68.         print_r($payment->getId());
  69.         echo "<h2>Your payment was <span>successfully proccessed!</span></h2>";
  70.  
  71.         //print_r($result);
  72.         try {
  73.             $payment = Payment::get($paymentId, $apiContext);
  74.         } catch (Exception $ex) {
  75.             // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
  76.             //  ResultPrinter::printError("Get Payment", "Payment", null, null, $ex);
  77.             print_r($ex);
  78.             exit(1);
  79.         }
  80.     } catch (Exception $ex) {
  81.         // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
  82.         //ResultPrinter::printError("Executed Payment", "Payment", null, null, $ex);
  83.         print_r($ex);
  84.         exit(1);
  85.     }
  86.     // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
  87.     //ResultPrinter::printResult("Get Payment", "Payment", $payment->getId(), null, $payment);
  88.     print_r($payment);
  89.     return $payment;
  90. } else {
  91.     // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
  92.     echo "User Cancelled the Approval";
  93.     exit;
  94. }
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement