'paypal_ec_username', 'type' => 'text', 'value' => ''), array('name' => 'paypal_ec_password', 'type' => 'text', 'value' => ''), array('name' => 'paypal_ec_signature', 'type' => 'text', 'value' => ''), array('name' => 'paypal_ec_currency', 'type' => 'select', 'value' => 'USD') ); return; } /** * 类 */ class paypal_ec { /** * 构造函数 * * @access public * @param * * @return void */ function __construct() { } /** * 生成支付代码 * @param array $order 订单信息 * @param array $payment 支付方式信息 */ function get_code($order, $payment) { $token = ''; $serverName = $_SERVER['SERVER_NAME']; $serverPort = $_SERVER['SERVER_PORT']; $url=dirname('http://'.$serverName.':'.$serverPort.$_SERVER['REQUEST_URI']); $paymentAmount=$order['order_amount']; $currencyCodeType=$payment['paypal_ec_currency']; $paymentType='Sale'; $data_order_id = $order['log_id']; $_SESSION['paypal_username']=$payment['paypal_ec_username']; $_SESSION['paypal_password']=$payment['paypal_ec_password']; $_SESSION['paypal_signature']=$payment['paypal_ec_signature']; $returnURL =urlencode($url.'/respond.php?code=paypal_ec¤cyCodeType='.$currencyCodeType.'&paymentType='.$paymentType.'&paymentAmount='.$paymentAmount.'&invoice='.$data_order_id); $cancelURL =urlencode("$url/SetExpressCheckout.php?paymentType=$paymentType" ); $nvpstr="&Amt=".$paymentAmount."&PAYMENTACTION=".$paymentType."&ReturnUrl=".$returnURL."&CANCELURL=".$cancelURL ."&CURRENCYCODE=".$currencyCodeType ."&ButtonSource=ECSHOP_cart_EC_C2"; $resArray=$this->hash_call("SetExpressCheckout",$nvpstr); $_SESSION['reshash']=$resArray; if(isset($resArray["ACK"])) { $ack = strtoupper($resArray["ACK"]); } if (isset($resArray["TOKEN"])) { $token = urldecode($resArray["TOKEN"]); } $payPalURL = PAYPAL_URL.$token; $button = '
'; return $button; } /** * 响应操作 */ function respond() { $order_sn = $_REQUEST['invoice']; $token =urlencode( $_REQUEST['token']); $nvpstr="&TOKEN=".$token; $resArray=$this->hash_call("GetExpressCheckoutDetails",$nvpstr); $_SESSION['reshash']=$resArray; $ack = strtoupper($resArray["ACK"]); if($ack=="SUCCESS") { $_SESSION['token']=$_REQUEST['token']; $_SESSION['payer_id'] = $_REQUEST['PayerID']; $_SESSION['paymentAmount']=$_REQUEST['paymentAmount']; $_SESSION['currCodeType']=$_REQUEST['currencyCodeType']; $_SESSION['paymentType']=$_REQUEST['paymentType']; $resArray=$_SESSION['reshash']; $token =urlencode( $_SESSION['token']); $paymentAmount =urlencode ($_SESSION['paymentAmount']); $paymentType = urlencode($_SESSION['paymentType']); $currCodeType = urlencode($_SESSION['currCodeType']); $payerID = urlencode($_SESSION['payer_id']); $serverName = urlencode($_SERVER['SERVER_NAME']); $nvpstr='&TOKEN='.$token.'&PAYERID='.$payerID.'&PAYMENTACTION='.$paymentType.'&AMT='.$paymentAmount.'&CURRENCYCODE='.$currCodeType.'&IPADDRESS='.$serverName ; $resArray=$this->hash_call("DoExpressCheckoutPayment",$nvpstr); $ack = strtoupper($resArray["ACK"]); if($ack=="SUCCESS") { /* 改变订单状态 */ order_paid($order_sn, 2); return true; } else { return false; } } else { return false; } } function hash_call($methodName,$nvpStr) { global $API_Endpoint; $version='53.0'; $API_UserName=$_SESSION['paypal_username']; $API_Password=$_SESSION['paypal_password']; $API_Signature=$_SESSION['paypal_signature']; $nvp_Header; $ch = curl_init(); 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); if(USE_PROXY) { curl_setopt ($ch, CURLOPT_PROXY, PROXY_HOST.":".PROXY_PORT); } $nvpreq="METHOD=".urlencode($methodName)."&VERSION=".urlencode($version)."&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature).$nvpStr; curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq); $response = curl_exec($ch); $nvpResArray=$this->deformatNVP($response); $nvpReqArray=$this->deformatNVP($nvpreq); $_SESSION['nvpReqArray']=$nvpReqArray; if (curl_errno($ch)) { $_SESSION['curl_error_no']=curl_errno($ch) ; $_SESSION['curl_error_msg']=curl_error($ch); } else { curl_close($ch); } return $nvpResArray; } 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; } } ?>