Share Pastebin
Guest
Public paste!

ntom

By: a guest | Nov 22nd, 2007 | Syntax: PHP | Size: 6.12 KB | Hits: 1,842 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2.  
  3.  
  4. $Vars = array(
  5.     'VPS_URL'=>'https://ukvpstest.protx.com/vspgateway/service/vspdirect-register.vsp',
  6.     'VPS_CALLBACK'=>'https://ukvpstest.protx.com/vspgateway/service/direct3dcallback.vsp',
  7.     'TermUrl'=>'http://www.site.com/path-to-file'     /// put this as the path to your file - no trailing slash..
  8. );
  9.  
  10.  
  11. if(!isset($_GET['action']))
  12. {
  13.     print_r('No specific action is set.. <br />');
  14.     // if you want to just set the fields with some prefilled stuff to save yourself having to
  15.     // 'add to the cart' everytime, then use the $optRequest
  16.     // see example below..
  17.     $optRequest = 'VPSProtocol=2.22&TxType=PAYMENT&Vendor=XXXXXX&VendorTxCode='.time().'m&Currency=GBP&Description= ..... etc ';
  18.    
  19.     // if dont have a request pre made, then you have to do it like this
  20.     /*
  21.         $p = array(
  22.             'VPSProtocol'        => '2.22',
  23.             'TxType'        => 'PAYMENT',
  24.             'Vendor'        => 'vps vendor name',
  25.             'VendorTxCode'        => 'your vendor txt code',
  26.             'Currency'        => 'GBP'
  27.            
  28.             .. etc and so on..
  29.         );
  30.     */
  31.     // as were using a prefilled request which i print_r'd earlier, im just setting $p to null.
  32.     $p = null;
  33.     _CompletePayment($p, $Vars, $optRequest);
  34. }
  35. else if($_GET['action'] == 'callback')
  36. {
  37.     __VerifyCallback($_POST, $Vars);
  38. }
  39. else if($_GET['action'] == 'callbacktest')
  40. {
  41.     $optRequest = '...  .. ';
  42.     __VerifyCallback($p = null, $Vars, $optRequest);
  43. }
  44. else
  45. {
  46.     print_r('Invalid action');
  47. }
  48.  
  49.  
  50. function _CompletePayment(&$p, &$Vars, $optReqeust = null)
  51. {        
  52.     $request = '';
  53.     if(is_array($p))
  54.         foreach($p as $k=>$v)
  55.             $request .= ("{$k}=".urlencode($v)."&");
  56.    
  57.     // print_r($request)  // this $request is what im sending with the $optRequest on line 13-16
  58.    
  59.     ($optReqeust) ? $request = $optReqeust : '';
  60.    
  61.     print_r('Attempting to connect to protx to process payment from $reqest variable on line:'.(__LINE__ - 2).'<br />');
  62.     $c = curl_init();
  63.     curl_setopt($c, CURLOPT_URL, $Vars['VPS_URL']);
  64.     curl_setopt($c, CURLOPT_FAILONERROR, 1);
  65.     curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
  66.     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  67.     curl_setopt($c, CURLOPT_TIMEOUT, 5);
  68.     curl_setopt($c, CURLOPT_POST, 1);
  69.     curl_setopt($c, CURLOPT_POSTFIELDS, $request);
  70.     $result = curl_exec($c);
  71.  
  72.     if(!$result)
  73.     {
  74.         // An error occurred while sending a request for authorisation. The request returned: '.curl_error($c);
  75.         print_r('Error while sending request to protx for authorization..  '.curl_error($c));
  76.     }
  77.     else
  78.     {
  79.         curl_close($c);
  80.         $response = _SetResponse($result);
  81.        
  82.         if($response['Status'] !== 'OK')
  83.         {
  84.             if($response['Status'] != '3DAUTH')
  85.             {
  86.                 // Not a 3d auth card.. perform whatever actions you like
  87.                 $errors++;            
  88.                 print_r('Card is not \'OK\' and does not require 3D Auth, Failed payment with detail: <br />');
  89.                 print_r($response);
  90.             }
  91.             else
  92.             {
  93.                 // response Status = 3DAUTH
  94.                 $tpl['PaReq'] = $response['PAReq'];
  95.                 $tpl['TermUrl'] = $Vars['TermUrl'].'/protxscript.php?action=callback';
  96.                 $tpl['MD'] = $response['MD'];
  97.                 $tpl['ACSURL'] = $response['ACSURL'];
  98.                
  99.                 print_r('Sucessfully connected to protx, and populating the form with the following data to send to the callback url: <br />');
  100.                 print_r('<pre>');
  101.                 print_r($tpl);
  102.                 print_r('</pre>');
  103.                        
  104.                 echo '<form method="post" action="'.$tpl['ACSURL'].'">';
  105.                 echo "\n";
  106.                 echo '<input type="hidden" name="PaReq" value="'.$tpl['PaReq'].'" />';
  107.                 echo "\n";
  108.                 echo '<input type="hidden" name="TermUrl" value="'.$tpl['TermUrl'].'" />';
  109.                 echo "\n";
  110.                 echo '<input type="hidden" name="MD" value="'.$tpl['MD'].'" />';
  111.                 echo "\n";
  112.                 echo '<input type="submit" value="Send to server" />';
  113.                 echo "\n";
  114.                 echo '</form>';
  115.             }
  116.         }
  117.         else
  118.         {
  119.             // payment a sucess
  120.         }
  121.        
  122.     }
  123. }
  124.  
  125. function __VerifyCallback(&$p, &$Vars, $optRequest)
  126. {
  127.     // you will want to pass $p as $_POST from your script /site
  128.     $request = '';
  129.     foreach($p as $k=>$v) $request .= ("{$k}=".urlencode($v)."&");
  130.    
  131.     ($optRequest) ? $request = $optRequest : '';
  132.    
  133.     $c = curl_init();
  134.     curl_setopt($c, CURLOPT_URL, $Vars['VPS_CALLBACK']);
  135.     curl_setopt($c, CURLOPT_FAILONERROR, 1);
  136.     curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
  137.     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  138.     curl_setopt($c, CURLOPT_TIMEOUT, 5);
  139.     curl_setopt($c, CURLOPT_POST, 1);
  140.     curl_setopt($c, CURLOPT_POSTFIELDS, $request);
  141.     $result = curl_exec($c);
  142.    
  143.     if(!$result)
  144.     {
  145.         print_r($Vars['VPS_CALLBACK']);
  146.         print_r('Error: '.curl_error($c));
  147.     }
  148.     else
  149.     {
  150.         curl_close($c);
  151.         $response = _SetResponse($result);
  152.  
  153.         switch ($response['Status'])
  154.         {
  155.             // perform the various methods here
  156.             case 'OK':
  157.                 // do somthing like check to see the 3DSecureStatus
  158.                 print_r('Returned OK <br />');
  159.                 print_r('3DSecure Status is: '.$response['3DSecureStatus'].'<br />');
  160.                 break;
  161.             default:
  162.                 print_r('Error: '.$response['StatusDetail'].'<br />');
  163.                 break;
  164.         }
  165.         print_r('Response was: <br />');
  166.         print_r('<pre>');
  167.         print_r($response);
  168.         print_r('</pre>');
  169.     }
  170. }
  171.  
  172. function _SetResponse($result)
  173. {
  174.     $l = split(chr(10), $result);
  175.     foreach($l as $k=>$v)
  176.     {
  177.         $v = explode("=", $v, 2);
  178.         $r[$v[0]] = trim($v[1]);
  179.     }
  180.     return $r;
  181. }
  182.  
  183. print_r('<br /><br /><br />');
  184. highlight_file(__FILE__);
  185.  
  186. ?>