Location: PHPKode > scripts > PayPal PHP SDK > rest-api-sdk-php-0.7.0/sample/source/ExecutePayment.html
<!DOCTYPE html><html lang="en"><head><title>ExecutePayment</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="ExecutePayment"><meta name="groc-project-path" content="ExecutePayment.php"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><body></body></html><a href="../">Back<div id="document"><div class="segment"><a id="segment-0" name="segment-0" class="section_anchor"></a><div class="code"><div class="wrapper"><span class="cp">&lt;?php</span></div></div></div><div class="segment"><a id="segment-1" name="segment-1" class="section_anchor"></a><div class="comments"><div class="pilwrap"><a href="#segment-1" class="pilcrow">¶</a></div><div class="wrapper"><h1 id="execute-payment-sample">Execute Payment Sample</h1>

<p>This sample shows how you can complete
a payment that has been approved by
the buyer by logging into paypal site.
You can optionally update transaction
information by passing in one or more transactions.
API used: POST '/v1/payments/payment/<payment-id>/execute'.</p></div></div><div class="code"><div class="wrapper"><span class="k">require</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/../bootstrap.php&#39;</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\ExecutePayment</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Payment</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\PaymentExecution</span><span class="p">;</span>
<span class="nb">session_start</span><span class="p">();</span>
<span class="k">if</span><span class="p">(</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$_GET</span><span class="p">[</span><span class="s1">&#39;success&#39;</span><span class="p">])</span> <span class="o">&amp;&amp;</span> <span class="nv">$_GET</span><span class="p">[</span><span class="s1">&#39;success&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;true&#39;</span><span class="p">)</span> <span class="p">{</span>
  </div></div></div><div class="segment"><a id="segment-2" name="segment-2" class="section_anchor"></a><div class="comments"><div class="pilwrap"><a href="#segment-2" class="pilcrow">¶</a></div><div class="wrapper"><p>Get the payment Object by passing paymentId
payment id was previously stored in session in
CreatePaymentUsingPayPal.php</p></div></div><div class="code"><div class="wrapper">  <span class="nv">$paymentId</span> <span class="o">=</span> <span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">&#39;paymentId&#39;</span><span class="p">];</span>
  <span class="nv">$payment</span> <span class="o">=</span> <span class="nx">Payment</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="nv">$paymentId</span><span class="p">,</span> <span class="nv">$apiContext</span><span class="p">);</span>
  </div></div></div><div class="segment"><a id="segment-3" name="segment-3" class="section_anchor"></a><div class="comments"><div class="pilwrap"><a href="#segment-3" class="pilcrow">¶</a></div><div class="wrapper"><p>PaymentExecution object includes information necessary 
to execute a PayPal account payment. 
The payer_id is added to the request query parameters
when the user is redirected from paypal back to your site</p></div></div><div class="code"><div class="wrapper">  <span class="nv">$execution</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PaymentExecution</span><span class="p">();</span>
  <span class="nv">$execution</span><span class="o">-&gt;</span><span class="na">setPayer_id</span><span class="p">(</span><span class="nv">$_GET</span><span class="p">[</span><span class="s1">&#39;PayerID&#39;</span><span class="p">]);</span>
  
  <span class="c1">//Execute the payment</span></div></div></div><div class="segment"><a id="segment-4" name="segment-4" class="section_anchor"></a><div class="comments"><div class="pilwrap"><a href="#segment-4" class="pilcrow">¶</a></div><div class="wrapper"><p>(See bootstrap.php for more on <code>ApiContext</code>)</p></div></div><div class="code"><div class="wrapper">  <span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">(</span><span class="nv">$execution</span><span class="p">,</span> <span class="nv">$apiContext</span><span class="p">);</span>

  <span class="k">echo</span> <span class="s2">&quot;&lt;html&gt;&lt;body&gt;&lt;pre&gt;&quot;</span><span class="p">;</span>
  <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">toArray</span><span class="p">());</span>
  <span class="k">echo</span> <span class="s2">&quot;&lt;/pre&gt;&lt;a href=&#39;../index.html&#39;&gt;Back&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;&quot;</span><span class="p">;</span>
  
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
  <span class="k">echo</span> <span class="s2">&quot;User cancelled payment.&quot;</span><span class="p">;</span>
<span class="p">}</span></div></div></div><div class="segment"><div class="comments"><a href="../">Back</a></div></div></div><script type="text/javascript" src="assets/jquery.min.js"></script><script type="text/javascript" src="assets/docs.js"></script></a>
Return current item: PayPal PHP SDK