This repository has been archived on 2026-04-06. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
PayPal-PHP-SDK/sample/doc/payments/CreatePayment.html
japatel 0cb302326a Enabled Payment Experience
- Updated Api to enabled Payment Experience
- Updated Tests and Samples
- Added Json Validator
- Ability for PPModel to return array of self objects
2014-10-13 12:00:31 -05:00

97 lines
10 KiB
HTML

<!DOCTYPE html><html lang="en"><head><title>payments/CreatePayment</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/CreatePayment"><meta name="groc-project-path" content="payments/CreatePayment.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/CreatePayment.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="createpaymentsample">CreatePaymentSample</h1>
<p>This sample code demonstrate how you can process
a direct credit card payment. Please note that direct
credit card payment and related features using the
REST API is restricted in some countries.
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">CreditCard</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">FundingInstrument</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>;</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="creditcard">CreditCard</h3>
<p>A resource representing a credit card that can be
used to fund a payment.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$card</span> = <span class="hljs-keyword">new</span> CreditCard();
<span class="hljs-variable">$card</span>-&gt;setType(<span class="hljs-string">"visa"</span>)
-&gt;setNumber(<span class="hljs-string">"4417119669820331"</span>)
-&gt;setExpireMonth(<span class="hljs-string">"11"</span>)
-&gt;setExpireYear(<span class="hljs-string">"2019"</span>)
-&gt;setCvv2(<span class="hljs-string">"012"</span>)
-&gt;setFirstName(<span class="hljs-string">"Joe"</span>)
-&gt;setLastName(<span class="hljs-string">"Shopper"</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="fundinginstrument">FundingInstrument</h3>
<p>A resource representing a Payer&#39;s funding instrument.
For direct credit card payments, set the CreditCard
field on this object.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$fi</span> = <span class="hljs-keyword">new</span> FundingInstrument();
<span class="hljs-variable">$fi</span>-&gt;setCreditCard(<span class="hljs-variable">$card</span>);</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 direct credit card payments, set payment method
to &#39;credit_card&#39; and add an array of funding instruments.</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>-&gt;setPaymentMethod(<span class="hljs-string">"credit_card"</span>)
-&gt;setFundingInstruments(<span class="hljs-keyword">array</span>(<span class="hljs-variable">$fi</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>-&gt;setName(<span class="hljs-string">'Ground Coffee 40 oz'</span>)
-&gt;setDescription(<span class="hljs-string">'Ground Coffee 40 oz'</span>)
-&gt;setCurrency(<span class="hljs-string">'USD'</span>)
-&gt;setQuantity(<span class="hljs-number">1</span>)
-&gt;setTax(<span class="hljs-string">'0.30'</span>)
-&gt;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>-&gt;setName(<span class="hljs-string">'Granola bars'</span>)
-&gt;setDescription(<span class="hljs-string">'Granola Bars with Peanuts'</span>)
-&gt;setCurrency(<span class="hljs-string">'USD'</span>)
-&gt;setQuantity(<span class="hljs-number">5</span>)
-&gt;setTax(<span class="hljs-string">'0.20'</span>)
-&gt;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>-&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>
<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>-&gt;setShipping(<span class="hljs-string">'1.20'</span>)
-&gt;setTax(<span class="hljs-string">'1.30'</span>)
-&gt;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>-&gt;setCurrency(<span class="hljs-string">"USD"</span>)
-&gt;setTotal(<span class="hljs-string">"20.00"</span>)
-&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>
<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>-&gt;setAmount(<span class="hljs-variable">$amount</span>)
-&gt;setItemList(<span class="hljs-variable">$itemList</span>)
-&gt;setDescription(<span class="hljs-string">"Payment description"</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 &#39;sale&#39;</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>-&gt;setIntent(<span class="hljs-string">"sale"</span>)
-&gt;setPayer(<span class="hljs-variable">$payer</span>)
-&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"><h3 id="create-payment">Create Payment</h3>
<p>Create a payment by calling the payment-&gt;create() method
with a valid ApiContext (See bootstrap.php for more on <code>ApiContext</code>)
The return object contains the state.</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">try</span> {
<span class="hljs-variable">$payment</span>-&gt;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>-&gt;getMessage() . PHP_EOL;
var_dump(<span class="hljs-variable">$ex</span>-&gt;getData());
<span class="hljs-keyword">exit</span>(<span class="hljs-number">1</span>);
}
<span class="hljs-preprocessor">?&gt;</span>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Direct Credit card payments&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
Created payment:
<span class="hljs-preprocessor">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-variable">$payment</span>-&gt;getId();<span class="hljs-preprocessor">?&gt;</span>
&lt;/div&gt;
&lt;pre&gt;<span class="hljs-preprocessor">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-variable">$payment</span>-&gt;toJSON(JSON_PRETTY_PRINT);<span class="hljs-preprocessor">?&gt;</span>&lt;/pre&gt;
&lt;a href=<span class="hljs-string">'../index.html'</span>&gt;Back&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;</div></div></div></div></body></html>