PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/AdaptivePayments/sample/code/Pay.php

https://gitlab.com/CORP-RESELLER/codesamples-php
PHP | 200 lines | 69 code | 40 blank | 91 comment | 2 complexity | ceed65d54f192cc82fd514debc5d459f MD5 | raw file
  1. <?php
  2. $path = '../../lib';
  3. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  4. require_once('PPBootStrap.php');
  5. // # Pay API
  6. // Use the Pay API operation to transfer funds from a sender's PayPal account to one or more receivers' PayPal accounts. You can use the Pay API operation to make simple payments, chained payments, or parallel payments; these payments can be explicitly approved, preapproved, or implicitly approved.
  7. // This sample code uses AdaptivePayments PHP SDK to make API call. You can
  8. // download the SDK [here](https://github.com/paypal/sdk-packages/tree/gh-pages/adaptivepayments-sdk/php)
  9. class Pay
  10. {
  11. public function simplePay()
  12. {
  13. // ##PayRequest
  14. // The code for the language in which errors are returned, which must be
  15. // en_US.
  16. $requestEnvelope = new RequestEnvelope("en_US");
  17. $receiver = array();
  18. $receiver[0] = new Receiver();
  19. // A receiver's email address
  20. $receiver[0]->email = "abc@paypal.com";
  21. // Amount to be credited to the receiver's account
  22. $receiver[0]->amount = "4.00";
  23. $receiverList = new ReceiverList($receiver);
  24. // PayRequest which takes mandatory params:
  25. //
  26. // * `Request Envelope` - Information common to each API operation, such
  27. // as the language in which an error message is returned.
  28. // * `Action Type` - The action for this request. Possible values are:
  29. // * PAY - Use this option if you are not using the Pay request in
  30. // combination with ExecutePayment.
  31. // * CREATE - Use this option to set up the payment instructions with
  32. // SetPaymentOptions and then execute the payment at a later time with
  33. // the ExecutePayment.
  34. // * PAY_PRIMARY - For chained payments only, specify this value to delay
  35. // payments to the secondary receivers; only the payment to the primary
  36. // receiver is processed.
  37. // * `Cancel URL` - URL to redirect the sender's browser to after
  38. // canceling the approval for a payment; it is always required but only
  39. // used for payments that require approval (explicit payments)
  40. // * `Currency Code` - The code for the currency in which the payment is
  41. // made; you can specify only one currency, regardless of the number of
  42. // receivers
  43. // * `Recevier List` - List of receivers
  44. // * `Return URL` - URL to redirect the sender's browser to after the
  45. // sender has logged into PayPal and approved a payment; it is always
  46. // required but only used if a payment requires explicit approval
  47. $payRequest = new PayRequest($requestEnvelope, "PAY", "http://localhost/cancel", "USD", $receiverList, "http://localhost/return");
  48. // The URL to which you want all IPN messages for this payment to be
  49. // sent.
  50. // This URL supersedes the IPN notification URL in your profile
  51. $payRequest->ipnNotificationUrl = "http://localhost/ipn";
  52. return $this->makeAPICall($payRequest);
  53. }
  54. // ##ChainedPayment
  55. // `Note:
  56. // For chained Payment all the above mentioned request parameters in
  57. // simplePay() are required, but in receiverList alone we have to make
  58. // receiver as Primary Receiver or Not Primary Receiver`
  59. public function chainPay()
  60. {
  61. // ##PayRequest
  62. // The code for the language in which errors are returned, which must be
  63. // en_US.
  64. $requestEnvelope = new RequestEnvelope("en_US");
  65. $receiver = array();
  66. $receiver[0] = new Receiver();
  67. // A receiver's email address
  68. $receiver[0]->email = "enduser_biz@gmail.com";
  69. // Amount to be credited to the receiver's account
  70. $receiver[0]->amount = "4.00";
  71. // Set to true to indicate a chained payment; only one receiver can be a
  72. // primary receiver. Omit this field, or set it to false for simple and
  73. // parallel payments.
  74. $receiver[0]->primary = true;
  75. $receiver[1] = new Receiver();
  76. // A receiver's email address
  77. $receiver[1]->email = "xyz@paypal.com";
  78. // Amount to be credited to the receiver's account
  79. $receiver[1]->amount = "2.00";
  80. // Set to true to indicate a chained payment; only one receiver can be a
  81. // primary receiver. Omit this field, or set it to false for simple and
  82. // parallel payments.
  83. $receiver[1]->primary = false;
  84. $receiverList = new ReceiverList($receiver);
  85. $payRequest = new PayRequest($requestEnvelope, "PAY", "http://localhost/cancel", "USD", $receiverList, "http://localhost/return");
  86. // The URL to which you want all IPN messages for this payment to be
  87. // sent.
  88. // This URL supersedes the IPN notification URL in your profile
  89. $payRequest->ipnNotificationUrl = "http://localhost/ipn";
  90. return $this->makeAPICall($payRequest);
  91. }
  92. // ##Parallel Payment
  93. // `Note:
  94. // For parallel Payment all the above mentioned request parameters in
  95. // simplePay() are required, but in receiverList we can have multiple
  96. // receivers`
  97. public function parallelPay()
  98. {
  99. // ##PayRequest
  100. // The code for the language in which errors are returned, which must be
  101. // en_US.
  102. $requestEnvelope = new RequestEnvelope("en_US");
  103. $receiver = array();
  104. $receiver[0] = new Receiver();
  105. // A receiver's email address
  106. $receiver[0]->email = "abc@paypal.com";
  107. // Amount to be credited to the receiver's account
  108. $receiver[0]->amount = "4.00";
  109. $receiver[1] = new Receiver();
  110. // A receiver's email address
  111. $receiver[1]->email = "xyz@paypal.com";
  112. // Amount to be credited to the receiver's account
  113. $receiver[1]->amount = "2.00";
  114. $receiverList = new ReceiverList($receiver);
  115. $payRequest = new PayRequest($requestEnvelope, "PAY", "http://localhost/cancel", "USD", $receiverList, "http://localhost/return");
  116. // The URL to which you want all IPN messages for this payment to be
  117. // sent.
  118. // This URL supersedes the IPN notification URL in your profile
  119. $payRequest->ipnNotificationUrl = "http://localhost/ipn";
  120. return $this->makeAPICall($payRequest);
  121. }
  122. private function makeAPICall($payRequest)
  123. {
  124. $logger = new PPLoggingManager('Pay');
  125. // ## Creating service wrapper object
  126. // Creating service wrapper object to make API call and loading
  127. // configuration file for your credentials and endpoint
  128. $service = new AdaptivePaymentsService();
  129. try {
  130. // ## Making API call
  131. // Invoke the appropriate method corresponding to API in service
  132. // wrapper object
  133. $response = $service->Pay($payRequest);
  134. } catch(Exception $ex) {
  135. $logger->error("Error Message : ". $ex->getMessage());
  136. }
  137. // ## Accessing response parameters
  138. // You can access the response parameters in
  139. // response object as shown below
  140. // ### Success values
  141. if ($response->responseEnvelope->ack == "Success")
  142. {
  143. // The pay key, which is a token you use in other Adaptive
  144. // Payment APIs (such as the Refund Method) to identify this
  145. // payment. The pay key is valid for 3 hours; the payment must
  146. // be approved while the pay key is valid.
  147. $logger->log("Pay Key : ".$response->payKey);
  148. // Once you get success response, user has to redirect to PayPal
  149. // for the payment. Construct redirectURL as follows,
  150. // `redirectURL=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey="
  151. // + $response->payKey`
  152. }
  153. // ### Error Values
  154. // Access error values from error list using getter methods
  155. else{
  156. $logger->error("API Error Message : ".$response->error[0]->message);
  157. }
  158. return $response;
  159. }
  160. }