Created
August 9, 2012 18:15
-
-
Save GRMule/3306716 to your computer and use it in GitHub Desktop.
Quickie paypal implementation (untested)
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 chargeCreditCard($detail) { | |
$API_USERNAME = '-USERNAME-'; | |
$API_PASSWORD = '-PASSWORD'; | |
$API_CERTIFICATE = '-SSL-CERT-'; | |
$API_SIGNATURE = ''; | |
$API_ENDPOINT = 'https://api.paypal.com/nvp'; | |
$PAYPAL_URL = 'https://www.paypal.com/webscr&cmd=_express-checkout&token='; | |
$VERSION = '51.0'; | |
$return = array( | |
'error'=>false, | |
'message'=>'', | |
'transaction_id'=>false | |
); | |
if ( | |
!isset($detail['CREDITCARDTYPE']) || | |
!isset($detail['ACCT']) || | |
!isset($detail['EXPDATE']) || | |
!isset($detail['CVV2']) || | |
!isset($detail['CURRENCYCODE']) || | |
!isset($detail['COUNTRYCODE']) || | |
!isset($detail['PAYMENTACTION']) || | |
!isset($detail['DESC']) || | |
!isset($detail['AMT']) | |
){ | |
$return['error'] = true; | |
$return['message'] = 'Missing required values'; | |
} | |
$post_string=' | |
METHOD=doDirectPayment& | |
VERSION='.urlencode($VERSION).'& | |
PWD='.urlencode($API_PASSWORD).'& | |
USER='.urlencode($API_USERNAME) | |
; | |
foreach($detail as $k=>$v) | |
$post_string.= "&".$k."=".urlencode($v); | |
$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); | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq); | |
$response = curl_exec($ch); | |
$response = deformatNVP($response); | |
$ack = strtoupper($response["ACK"]); | |
if($ack=="FAILURE") { | |
$out = ''; | |
if (isset($response['L_LONGMESSAGE0'])) | |
$out = $response['L_LONGMESSAGE0']; | |
if (isset($response['L_LONGMESSAGE0'])) | |
$out .= '('.$response['L_ERRORCODE0'].')'; | |
$return['error'] = true; | |
$return['message'] = $out; | |
return $return; | |
} | |
$return['transaction_id'] = $response['TRANSACTIONID']; | |
return $return; | |
} | |
function deformatNVP($nvpstr) { | |
$intial=0; | |
$nvpArray = array(); | |
while(strlen($nvpstr)){ | |
$keypos= strpos($nvpstr,'='); | |
$valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr); | |
$keyval=substr($nvpstr,$intial,$keypos); | |
$valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1); | |
$nvpArray[urldecode($keyval)] =urldecode( $valval); | |
$nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr)); | |
} | |
return $nvpArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment