PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/PayPal-PHP-SDK/paypal/rest-api-sdk-php/sample/payments/CreateThirdPartyPayment.php

https://bitbucket.org/developerbit/codeigniter_paypal
PHP | 125 lines | 62 code | 17 blank | 46 comment | 0 complexity | b1a2231e5dfdc6a033ea37a4d4f449a4 MD5 | raw file
Possible License(s): BitTorrent-1.0
  1. <?php
  2. // # Create Third Party Payment using PayPal as payment method
  3. // This sample code demonstrates how you can process a
  4. // PayPal Account based Payment with a third party paypal account.
  5. // API used: /v1/payments/payment
  6. require __DIR__ . '/../bootstrap.php';
  7. use PayPal\Api\Amount;
  8. use PayPal\Api\Details;
  9. use PayPal\Api\Item;
  10. use PayPal\Api\ItemList;
  11. use PayPal\Api\Payee;
  12. use PayPal\Api\Payer;
  13. use PayPal\Api\Payment;
  14. use PayPal\Api\RedirectUrls;
  15. use PayPal\Api\Transaction;
  16. // ### Payer
  17. // A resource representing a Payer that funds a payment
  18. // For paypal account payments, set payment method
  19. // to 'paypal'.
  20. $payer = new Payer();
  21. $payer->setPaymentMethod("paypal");
  22. // ### Itemized information
  23. // (Optional) Lets you specify item wise
  24. // information
  25. $item1 = new Item();
  26. $item1->setName('Ground Coffee 40 oz')
  27. ->setCurrency('USD')
  28. ->setQuantity(1)
  29. ->setSku("123123") // Similar to `item_number` in Classic API
  30. ->setPrice(7.5);
  31. $item2 = new Item();
  32. $item2->setName('Granola bars')
  33. ->setCurrency('USD')
  34. ->setQuantity(5)
  35. ->setSku("321321") // Similar to `item_number` in Classic API
  36. ->setPrice(2);
  37. $itemList = new ItemList();
  38. $itemList->setItems(array($item1, $item2));
  39. // ### Additional payment details
  40. // Use this optional field to set additional
  41. // payment information such as tax, shipping
  42. // charges etc.
  43. $details = new Details();
  44. $details->setShipping(1.2)
  45. ->setTax(1.3)
  46. ->setSubtotal(17.50);
  47. // ### Amount
  48. // Lets you specify a payment amount.
  49. // You can also specify additional details
  50. // such as shipping, tax.
  51. $amount = new Amount();
  52. $amount->setCurrency("USD")
  53. ->setTotal(20)
  54. ->setDetails($details);
  55. // ### Payee
  56. // Specify a payee with that user's email or merchant id
  57. // Merchant Id can be found at https://www.paypal.com/businessprofile/settings/
  58. $payee = new Payee();
  59. $payee->setEmail("stevendcoffey-facilitator@gmail.com");
  60. // ### Transaction
  61. // A transaction defines the contract of a
  62. // payment - what is the payment for and who
  63. // is fulfilling it.
  64. $transaction = new Transaction();
  65. $transaction->setAmount($amount)
  66. ->setItemList($itemList)
  67. ->setDescription("Payment description")
  68. ->setPayee($payee)
  69. ->setInvoiceNumber(uniqid());
  70. // ### Redirect urls
  71. // Set the urls that the buyer must be redirected to after
  72. // payment approval/ cancellation.
  73. $baseUrl = getBaseUrl();
  74. $redirectUrls = new RedirectUrls();
  75. $redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true")
  76. ->setCancelUrl("$baseUrl/ExecutePayment.php?success=false");
  77. // ### Payment
  78. // A Payment Resource; create one using
  79. // the above types and intent set to 'sale'
  80. $payment = new Payment();
  81. $payment->setIntent("sale")
  82. ->setPayer($payer)
  83. ->setRedirectUrls($redirectUrls)
  84. ->setTransactions(array($transaction));
  85. // For Sample Purposes Only.
  86. $request = clone $payment;
  87. // ### Create Payment
  88. // Create a payment by calling the 'create' method
  89. // passing it a valid apiContext.
  90. // (See bootstrap.php for more on `ApiContext`)
  91. // The return object contains the state and the
  92. // url to which the buyer must be redirected to
  93. // for payment approval
  94. try {
  95. $payment->create($apiContext);
  96. } catch (Exception $ex) {
  97. // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
  98. ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
  99. exit(1);
  100. }
  101. // ### Get redirect url
  102. // The API response provides the url that you must redirect
  103. // the buyer to. Retrieve the url from the $payment->getApprovalLink()
  104. // method
  105. $approvalUrl = $payment->getApprovalLink();
  106. // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
  107. ResultPrinter::printResult("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "<a href='$approvalUrl' >$approvalUrl</a>", $request, $payment);
  108. return $payment;