Adventures Inwards Paypal: Executing The Sale


In gild to execute the sale you'll conduct hold a provide page setup where PayPal redirects to afterwards the client has paid. Since the user is going away from the page together with returning to either the same i or a novel i you lot involve to abide by a agency of storing the access token hence that you lot tin utilization this i time to a greater extent than for execution.

While it's truthful that you lot tin exactly create a novel access token, together with this volition hold upwards as valid for performing the execution, what you lot are meant to exercise is to utilization access tokens until their death (which occurs 8–9 hours afterwards their creation). So inward all likelihood the access token volition hold upwards active when the user returns from PayPal.

To address this it is possible to shop the token as a cookie or inward session storage. Session storage keeps the information on the server together with cookies salvage to the user's machine. So session storage seems similar the obvious choice.

To perform this nosotros involve to add together a 2 lines of code to the original call to recollect the PayPal access token. First nosotros involve to kickoff a session, which tin hold upwards done at the caput of the page:
session_start();
together with instant a business needs to hold upwards added to the token creation code
$_SESSION["paypal_access_token"] = $token;
hence that it looks similar this
echo "\n"; echo "###########################################\n"; echo "Obtaining OAuth2 Access Token.... \n"; $url = $host.'/v1/oauth2/token';  $postArgs = 'grant_type=client_credentials'; $token = get_access_token($url,$postArgs); echo "Got OAuth Token: ".$token; $_SESSION["paypal_access_token"] = $token; echo "\n \n"; 
For safety, nosotros also involve to recollect the death fourth dimension of the token. To exercise this nosotros tin house a business of code within the get_access_token: method:
$_SESSION["paypal_expires"] = time() + $jsonResponse->expires_in;
right away earlier its provide statement.

Executing the sale

Now everything volition hold upwards prepared for the execution of the sale together with our code looks similar this:
<?php session_start(); // http://stackoverflow.com/questions/15729167/paypal-api-with-php-and-curl // https://github.com/paypal/adaptivepayments-sdk-php/issues/24 // https://github.com/paypal/rest-api-curlsamples/blob/master/execute_all_calls.php  //http://stackoverflow.com/questions/15885742/pay-with-paypal-through-paypal-rest-api-does-not-show-up-payment-description-on // Full payment capabilities // https://developer.paypal.com/docs/integration/direct/explore-payment-capabilities/ $host = 'https://api.sandbox.paypal.com'; $clientId = "your client id goes here"; $secret = "your surreptitious goes here"; // banking concern jibe death together with if expired asking novel token $_SESSION["paypal_expiry"] $token = '';  role get_access_token($url, $postdata) {  global $clientId, $clientSecret;  $curl = curl_init($url);   curl_setopt($curl, CURLOPT_POST, true);   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret); // curl_setopt($ch, CURLOPT_SSLVERSION, 1); // curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');  curl_setopt($curl, CURLOPT_HEADER, false);   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);   curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);   // curl_setopt($curl, CURLOPT_VERBOSE, TRUE);  $response = curl_exec( $curl );  if (empty($response)) {      // some form of an fault happened      die(curl_error($curl));      curl_close($curl); // unopen cURL handler  } else {      $info = curl_getinfo($curl);   echo "Time took: " . $info['total_time']*1000 . "ms\n";      curl_close($curl); // unopen cURL handler   if($info['http_code'] != 200 && $info['http_code'] != 201 ) {    echo "Received error: " . $info['http_code']. "\n";    echo "Raw response:".$response."\n";    die();      }  }  // Convert the upshot from JSON format to a PHP array   $jsonResponse = json_decode( $response );  provide $jsonResponse->access_token; }   role make_post_call($url, $postdata) {  global $token;  $curl = curl_init($url);   curl_setopt($curl, CURLOPT_POST, true);  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  curl_setopt($curl, CURLOPT_HEADER, false);  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  curl_setopt($curl, CURLOPT_HTTPHEADER, array(     'Authorization: Bearer '.$token,     'Accept: application/json',     'Content-Type: application/json'     ));    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);   #curl_setopt($curl, CURLOPT_VERBOSE, TRUE);  $response = curl_exec( $curl );  if (empty($response)) {      // some form of an fault happened      die(curl_error($curl));      curl_close($curl); // unopen cURL handler  } else {      $info = curl_getinfo($curl);   echo "Time took: " . $info['total_time']*1000 . "ms\n";      curl_close($curl); // unopen cURL handler   if($info['http_code'] != 200 && $info['http_code'] != 201 ) {    echo "Received error: " . $info['http_code']. "\n";    echo "Raw response:".$response."\n";    die();      }  }   // Convert the upshot from JSON format to a PHP array   $jsonResponse = json_decode($response, TRUE);  provide $jsonResponse; }  // banking concern jibe if token has expired, create novel i if it has if (isset($_SESSION["expiry_time"]) && $_SESSION["expiry_time"] > time()) {      $url = $host.'/v1/oauth2/token';      $postArgs = 'grant_type=client_credentials';     $token = get_access_token($url,$postArgs); } else if (isset($_SESSION["paypal_access_token"])) {  $token = $_SESSION["paypal_access_token"]; }  $payment_execute_url = $host."/v1/payments/payment/".$_GET["paymentId"]."/execute/";  $payerId = $_GET["PayerID"]; // payerId non beingness received. echo "\n \n"; echo $token; echo "###########################################\n"; echo "Executing the PayPal Payment for PayerId (".$_GET["PayerID"].")... \n".$payment_execute_url; $payment_execute = array(   "payer_id" => $_GET["PayerID"]         ); $json = json_encode($payment_execute); echo $json; $json_resp = make_post_call($payment_execute_url, $json); echo "Payment Execute processed " . $json_resp['id'] ." alongside dry reason '". $json_resp['state']."'"; echo "\n \n";  ?>
In existent utilization you lot belike don't desire to become repeating your storing your surreptitious together with id across multiple files, it's exactly done hither for convenience inward gild to present a self-contained provide file but inward real-world utilization nosotros tin hold upwards smarter inward our code arrangement. Not exclusively this but of course of written report functions themselves conduct hold been repeated across the transaction together with execution files, which i time to a greater extent than doesn't truly involve to hold upwards happening. The betoken is, however, that within 2 fairly uncomplicated files we're able to shipping a asking for payment together with have dorsum the information necessary to execute that payment together with to hence execute.

Next steps

From hither nosotros tin progress to brand calls for inspecting transactions that conduct hold taken house amid other things together with I promise to hold off at these inward a hereafter post as good as making this code to a greater extent than object-oriented inward style.

Comments

Popular posts from this blog

Removing The Index.Php File From Url Inward Codeigniter

What Are The Main Components of a Computer System

Delete Daily Doppler E-Mail Spam From An Iphone [Fix]