Created
September 19, 2013 16:12
-
-
Save anonymous/6c21f19dc4aaa123ebe1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function process($paymentDetail, $amt) { | |
$requestOptions = array( | |
'FIRSTNAME'=>$paymentDetail['first_name'], | |
'LASTNAME'=>$paymentDetail['last_name'], | |
'CREDITCARDTYPE'=>determineCardType($paymentDetail['card_number']), | |
'ACCT'=>$paymentDetail['card_number'], | |
'EXPDATE'=>$paymentDetail['card_expires'], | |
'CVV2'=>$paymentDetail['card_security '], | |
'AMT'=>$amt, | |
'CURRENCYCODE'=>"USD", | |
'COUNTRYCODE'=>"US", | |
'PAYMENTACTION'=>"Sale", | |
'DESC'=>'General Web Purchase' | |
); | |
$details = ''; | |
foreach ($requestOptions as $k=>$this_detail) { | |
$details.= "&".strtoupper($k)."=".urlencode(strtotitle($this_detail)); | |
} | |
$API_USERNAME = 'my.paypal.api.username'; | |
$API_PASSWORD = 'APAYPALLAPIPASSWORD'; | |
$API_CERTIFICATE = '/path/to/cert_key_pem'; | |
$API_SIGNATURE = ''; | |
$API_ENDPOINT = 'https://api.paypal.com/nvp'; | |
$PAYPAL_URL = 'https://www.paypal.com/webscr&cmd=_express-checkout&token='; | |
$VERSION = '51.0'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_SSLCERT, $API_CERTIFICATE); | |
curl_setopt($ch, CURLOPT_URL,$API_ENDPOINT); | |
curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
$nvpreq="METHOD=doDirectPayment&VERSION=".urlencode($VERSION)."&PWD=".urlencode($API_PASSWORD)."&USER=".urlencode($API_USERNAME).$details; | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq); | |
$response = curl_exec($ch); | |
$nvpArray = array(); | |
$intial = 0; | |
while(strlen($response)){ | |
$keypos= strpos($response,'='); | |
$valuepos = strpos($response,'&') ? strpos($response,'&'): strlen($response); | |
$keyval=substr($response,$intial,$keypos); | |
$valval=substr($response,$keypos+1,$valuepos-$keypos-1); | |
$nvpResArray[urldecode($keyval)] =urldecode( $valval); | |
$response=substr($response,$valuepos+1,strlen($response)); | |
} | |
$ack = strtoupper($nvpResArray["ACK"]); | |
if($ack=="FAILURE") { | |
if (isset($nvpResArray['L_LONGMESSAGE0'])) | |
$message = $nvpResArray['L_LONGMESSAGE0']; | |
else | |
$message = 'General error processing payment'; | |
return array( | |
'result'=>false, | |
'message'=>$message, | |
'transaction_id'=>'' | |
); | |
} | |
return array( | |
'result'=>true, | |
'message'=>'', | |
'transaction_id'=>$nvpResArray['TRANSACTIONID'] | |
); | |
} | |
function determineCardType ($ccnum) { | |
if (strlen($ccnum) == 16 && preg_match('/^5[1-5]/', $ccnum)) | |
return 'Mastercard'; | |
if ((strlen($ccnum) == 16 || strlen($ccnum) == 13) && substr($ccnum,0,1)==4) | |
return 'Visa'; | |
if (strlen($ccnum) == 15 && preg_match('/^3[47]/', $ccnum)) | |
return 'Amex'; | |
if (strlen($ccnum) == 16 && substr($ccnum,0,4) == '6011') | |
return 'Discover'; | |
if ($ccnum === '5177261171chris') | |
return 'Visa'; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment