forked from LiveCarta/PayPal-PHP-SDK
- Updated Api to enabled Payment Experience - Updated Tests and Samples - Added Json Validator - Ability for PPModel to return array of self objects
90 lines
10 KiB
HTML
90 lines
10 KiB
HTML
<!DOCTYPE html><html lang="en"><head><title>payments/CreatePaymentUsingPayPal</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/CreatePaymentUsingPayPal"><meta name="groc-project-path" content="payments/CreatePaymentUsingPayPal.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/CreatePaymentUsingPayPal.php</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="hljs-preprocessor"><?php</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h1 id="create-payment-using-paypal-as-payment-method">Create Payment using PayPal as payment method</h1>
|
|
<p>This sample code demonstrates how you can process a
|
|
PayPal Account based Payment.
|
|
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>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Amount</span>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Details</span>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Item</span>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">ItemList</span>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Payer</span>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Payment</span>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">RedirectUrls</span>;
|
|
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Transaction</span>;
|
|
session_start();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payer">Payer</h3>
|
|
<p>A resource representing a Payer that funds a payment
|
|
For paypal account payments, set payment method
|
|
to 'paypal'.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$payer</span> = <span class="hljs-keyword">new</span> Payer();
|
|
<span class="hljs-variable">$payer</span>->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>
|
|
<p>(Optional) Lets you specify item wise
|
|
information</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$item1</span> = <span class="hljs-keyword">new</span> Item();
|
|
<span class="hljs-variable">$item1</span>->setName(<span class="hljs-string">'Ground Coffee 40 oz'</span>)
|
|
->setCurrency(<span class="hljs-string">'USD'</span>)
|
|
->setQuantity(<span class="hljs-number">1</span>)
|
|
->setPrice(<span class="hljs-string">'7.50'</span>);
|
|
<span class="hljs-variable">$item2</span> = <span class="hljs-keyword">new</span> Item();
|
|
<span class="hljs-variable">$item2</span>->setName(<span class="hljs-string">'Granola bars'</span>)
|
|
->setCurrency(<span class="hljs-string">'USD'</span>)
|
|
->setQuantity(<span class="hljs-number">5</span>)
|
|
->setPrice(<span class="hljs-string">'2.00'</span>);
|
|
|
|
<span class="hljs-variable">$itemList</span> = <span class="hljs-keyword">new</span> ItemList();
|
|
<span class="hljs-variable">$itemList</span>->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>
|
|
<p>Use this optional field to set additional
|
|
payment information such as tax, shipping
|
|
charges etc.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$details</span> = <span class="hljs-keyword">new</span> Details();
|
|
<span class="hljs-variable">$details</span>->setShipping(<span class="hljs-string">'1.20'</span>)
|
|
->setTax(<span class="hljs-string">'1.30'</span>)
|
|
->setSubtotal(<span class="hljs-string">'17.50'</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="amount">Amount</h3>
|
|
<p>Lets you specify a payment amount.
|
|
You can also specify additional details
|
|
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();
|
|
<span class="hljs-variable">$amount</span>->setCurrency(<span class="hljs-string">"USD"</span>)
|
|
->setTotal(<span class="hljs-string">"20.00"</span>)
|
|
->setDetails(<span class="hljs-variable">$details</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="transaction">Transaction</h3>
|
|
<p>A transaction defines the contract of a
|
|
payment - what is the payment for and who
|
|
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();
|
|
<span class="hljs-variable">$transaction</span>->setAmount(<span class="hljs-variable">$amount</span>)
|
|
->setItemList(<span class="hljs-variable">$itemList</span>)
|
|
->setDescription(<span class="hljs-string">"Payment description"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="redirect-urls">Redirect urls</h3>
|
|
<p>Set the urls that the buyer must be redirected to after
|
|
payment approval/ cancellation.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$baseUrl</span> = getBaseUrl();
|
|
<span class="hljs-variable">$redirectUrls</span> = <span class="hljs-keyword">new</span> RedirectUrls();
|
|
<span class="hljs-variable">$redirectUrls</span>->setReturnUrl(<span class="hljs-string">"$baseUrl/ExecutePayment.php?success=true"</span>)
|
|
->setCancelUrl(<span class="hljs-string">"$baseUrl/ExecutePayment.php?success=false"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payment">Payment</h3>
|
|
<p>A Payment Resource; create one using
|
|
the above types and intent set to 'sale'</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$payment</span> = <span class="hljs-keyword">new</span> Payment();
|
|
<span class="hljs-variable">$payment</span>->setIntent(<span class="hljs-string">"sale"</span>)
|
|
->setPayer(<span class="hljs-variable">$payer</span>)
|
|
->setRedirectUrls(<span class="hljs-variable">$redirectUrls</span>)
|
|
->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"><h3 id="create-payment">Create Payment</h3>
|
|
<p>Create a payment by calling the 'create' method
|
|
passing it a valid apiContext.
|
|
(See bootstrap.php for more on <code>ApiContext</code>)
|
|
The return object contains the state and the
|
|
url to which the buyer must be redirected to
|
|
for payment approval</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">try</span> {
|
|
<span class="hljs-variable">$payment</span>->create(<span class="hljs-variable">$apiContext</span>);
|
|
} <span class="hljs-keyword">catch</span> (PayPal\<span class="hljs-keyword">Exception</span>\PPConnectionException <span class="hljs-variable">$ex</span>) {
|
|
<span class="hljs-keyword">echo</span> <span class="hljs-string">"Exception: "</span> . <span class="hljs-variable">$ex</span>->getMessage() . PHP_EOL;
|
|
var_dump(<span class="hljs-variable">$ex</span>->getData());
|
|
<span class="hljs-keyword">exit</span>(<span class="hljs-number">1</span>);
|
|
}</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="get-redirect-url">Get redirect url</h3>
|
|
<p>The API response provides the url that you must redirect
|
|
the buyer to. Retrieve the url from the $payment->getLinks()
|
|
method</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$payment</span>->getLinks() <span class="hljs-keyword">as</span> <span class="hljs-variable">$link</span>) {
|
|
<span class="hljs-keyword">if</span>(<span class="hljs-variable">$link</span>->getRel() == <span class="hljs-string">'approval_url'</span>) {
|
|
<span class="hljs-variable">$redirectUrl</span> = <span class="hljs-variable">$link</span>->getHref();
|
|
<span class="hljs-keyword">break</span>;
|
|
}
|
|
}</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="redirect-buyer-to-paypal-website">Redirect buyer to PayPal website</h3>
|
|
<p>Save the payment id so that you can 'complete' the payment
|
|
once the buyer approves the payment and is redirected
|
|
back to your website.
|
|
It is not a great idea to store the payment id
|
|
in the session. In a real world app, you may want to
|
|
store the payment id in a database.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$_SESSION</span>[<span class="hljs-string">'paymentId'</span>] = <span class="hljs-variable">$payment</span>->getId();
|
|
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$redirectUrl</span>)) {
|
|
header(<span class="hljs-string">"Location: $redirectUrl"</span>);
|
|
<span class="hljs-keyword">exit</span>;
|
|
}</div></div></div></div></body></html> |