PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sample/doc/payments/OrderCreateForCapture.html

https://gitlab.com/CORP-RESELLER/PayPal-PHP-SDK
HTML | 74 lines | 72 code | 2 blank | 0 comment | 0 complexity | ad926f7dfad53371220bf89db8f6dc2d MD5 | raw file
  1. <!DOCTYPE html><html lang="en"><head><title>payments/OrderCreateForCapture</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content="../"><meta name="groc-document-path" content="payments/OrderCreateForCapture"><meta name="groc-project-path" content="payments/OrderCreateForCapture.php"><link rel="stylesheet" type="text/css" media="all" href="../assets/style.css"><script type="text/javascript" src="../assets/behavior.js"></script><body><div id="meta"><div class="file-path">payments/OrderCreateForCapture.php</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="hljs-preprocessor">&lt;?php</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h1 id="order-create-using-paypal">Order Create Using PayPal</h1>
  2. <p>In a call to the /payment resource, provide the payment details. In the intent field, specify order, and set the payment_method to paypal. Creating an order is similar to creating a payment.
  3. API used: /v1/payments/payment</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">require</span> <span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../bootstrap.php'</span>;
  4. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Amount</span>;
  5. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Details</span>;
  6. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Item</span>;
  7. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">ItemList</span>;
  8. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Payer</span>;
  9. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Payment</span>;
  10. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">RedirectUrls</span>;
  11. <span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Transaction</span>;</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payer">Payer</h3>
  12. <p>A resource representing a Payer that funds a payment
  13. For paypal account payments, set payment method
  14. to &#39;paypal&#39;.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$payer</span> = <span class="hljs-keyword">new</span> Payer();
  15. <span class="hljs-variable">$payer</span>-&gt;setPaymentMethod(<span class="hljs-string">"paypal"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="itemized-information">Itemized information</h3>
  16. <p>(Optional) Lets you specify item wise
  17. information</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$item1</span> = <span class="hljs-keyword">new</span> Item();
  18. <span class="hljs-variable">$item1</span>-&gt;setName(<span class="hljs-string">'Ground Coffee 40 oz'</span>)
  19. -&gt;setCurrency(<span class="hljs-string">'USD'</span>)
  20. -&gt;setQuantity(<span class="hljs-number">1</span>)
  21. -&gt;setPrice(<span class="hljs-number">7.5</span>);
  22. <span class="hljs-variable">$item2</span> = <span class="hljs-keyword">new</span> Item();
  23. <span class="hljs-variable">$item2</span>-&gt;setName(<span class="hljs-string">'Granola bars'</span>)
  24. -&gt;setCurrency(<span class="hljs-string">'USD'</span>)
  25. -&gt;setQuantity(<span class="hljs-number">5</span>)
  26. -&gt;setPrice(<span class="hljs-number">2</span>);
  27. <span class="hljs-variable">$itemList</span> = <span class="hljs-keyword">new</span> ItemList();
  28. <span class="hljs-variable">$itemList</span>-&gt;setItems(<span class="hljs-keyword">array</span>(<span class="hljs-variable">$item1</span>, <span class="hljs-variable">$item2</span>));</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="additional-payment-details">Additional payment details</h3>
  29. <p>Use this optional field to set additional
  30. payment information such as tax, shipping
  31. charges etc.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$details</span> = <span class="hljs-keyword">new</span> Details();
  32. <span class="hljs-variable">$details</span>-&gt;setShipping(<span class="hljs-number">1.2</span>)
  33. -&gt;setTax(<span class="hljs-number">1.3</span>)
  34. -&gt;setSubtotal(<span class="hljs-number">17.50</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="amount">Amount</h3>
  35. <p>Lets you specify a payment amount.
  36. You can also specify additional details
  37. such as shipping, tax.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$amount</span> = <span class="hljs-keyword">new</span> Amount();
  38. <span class="hljs-variable">$amount</span>-&gt;setCurrency(<span class="hljs-string">"USD"</span>)
  39. -&gt;setTotal(<span class="hljs-number">20</span>)
  40. -&gt;setDetails(<span class="hljs-variable">$details</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="transaction">Transaction</h3>
  41. <p>A transaction defines the contract of a
  42. payment - what is the payment for and who
  43. is fulfilling it. </p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$transaction</span> = <span class="hljs-keyword">new</span> Transaction();
  44. <span class="hljs-variable">$transaction</span>-&gt;setAmount(<span class="hljs-variable">$amount</span>)
  45. -&gt;setItemList(<span class="hljs-variable">$itemList</span>)
  46. -&gt;setDescription(<span class="hljs-string">"Payment description"</span>)
  47. -&gt;setInvoiceNumber(uniqid());</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="redirect-urls">Redirect urls</h3>
  48. <p>Set the urls that the buyer must be redirected to after
  49. payment approval/ cancellation.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$baseUrl</span> = getBaseUrl();
  50. <span class="hljs-variable">$redirectUrls</span> = <span class="hljs-keyword">new</span> RedirectUrls();
  51. <span class="hljs-variable">$redirectUrls</span>-&gt;setReturnUrl(<span class="hljs-string">"$baseUrl/OrderCapture.php?success=true"</span>)
  52. -&gt;setCancelUrl(<span class="hljs-string">"$baseUrl/OrderCapture.php?success=false"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payment">Payment</h3>
  53. <p>A Payment Resource; create one using
  54. the above types and intent set to &#39;order&#39;</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$payment</span> = <span class="hljs-keyword">new</span> Payment();
  55. <span class="hljs-variable">$payment</span>-&gt;setIntent(<span class="hljs-string">"order"</span>)
  56. -&gt;setPayer(<span class="hljs-variable">$payer</span>)
  57. -&gt;setRedirectUrls(<span class="hljs-variable">$redirectUrls</span>)
  58. -&gt;setTransactions(<span class="hljs-keyword">array</span>(<span class="hljs-variable">$transaction</span>));</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>For Sample Purposes Only.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$request</span> = <span class="hljs-keyword">clone</span> <span class="hljs-variable">$payment</span>;</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="create-payment">Create Payment</h3>
  59. <p>Create a payment by calling the &#39;create&#39; method
  60. passing it a valid apiContext.
  61. (See bootstrap.php for more on <code>ApiContext</code>)
  62. The return object contains the state and the
  63. url to which the buyer must be redirected to
  64. for payment approval</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">try</span> {
  65. <span class="hljs-variable">$payment</span>-&gt;create(<span class="hljs-variable">$apiContext</span>);
  66. } <span class="hljs-keyword">catch</span> (<span class="hljs-keyword">Exception</span> <span class="hljs-variable">$ex</span>) {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY</p></div></div><div class="code"><div class="wrapper"> ResultPrinter::printError(<span class="hljs-string">"Created Payment Order Using PayPal. Please visit the URL to Approve."</span>, <span class="hljs-string">"Payment"</span>, <span class="hljs-keyword">null</span>, <span class="hljs-variable">$request</span>, <span class="hljs-variable">$ex</span>);
  67. <span class="hljs-keyword">exit</span>(<span class="hljs-number">1</span>);
  68. }</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="get-redirect-url">Get redirect url</h3>
  69. <p>The API response provides the url that you must redirect
  70. the buyer to. Retrieve the url from the $payment-&gt;getApprovalLink()
  71. method</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$approvalUrl</span> = <span class="hljs-variable">$payment</span>-&gt;getApprovalLink();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY</p></div></div><div class="code"><div class="wrapper"> ResultPrinter::printResult(<span class="hljs-string">"Created Payment Order Using PayPal. Please visit the URL to Approve."</span>, <span class="hljs-string">"Payment"</span>, <span class="hljs-string">"&lt;a href='$approvalUrl' &gt;$approvalUrl&lt;/a&gt;"</span>, <span class="hljs-variable">$request</span>, <span class="hljs-variable">$payment</span>);
  72. <span class="hljs-keyword">return</span> <span class="hljs-variable">$payment</span>;</div></div></div></div></body></html>