详解PHP 如何对接 paypal 支付

作者: adm 分类: php 发布时间: 2021-06-17 13:51

环境准备

注册paypal账号
注册paypal开发者账号
创建paypal沙盒测试账户
创建paypal应用
下载PHP SDK
实现支付
实现支付成功回调
实现退款
更多API代码模板
注册paypal账号

www.paypal.com 注册商家账户
选择,”创建商家用户”,根据要求填写信息,注册完得去邮箱激活
注册paypal开发者账号

developer.paypal.com 使用上一步注册的账号登录
在左侧导航栏点击Accounts,创建两个sandbox账号,一个个人账号(personal)用于付款,一个商家账号(business)用于收款,系统已经默认创建了两个,可以自己选择创建,方便记忆; 创建账号后记得给当前账号添加余额用于接下来的测试。

用刚才创建的测试账号登录沙盒测试站点,查看金额和交易信息www.sandbox.paypal.com

 

代码

class Paypal extends Base{
const Currency = 'USD'; //币种 美元 CNY

/**
* 生成支付代码
*/
function getPaypalUrl(){
$payObj = input("payObj/s");

$obj = array();
$data = array();
$orderAmount = 0;
$out_trade_no = "";
$passback_params = "";
$subject = "";
$body = "";

$m = new OM();
$returnUrl = url("home/paypal/payorders","",true,true);

$pkey = input("pkey");
$pkey = WSTBase64urlDecode($pkey);
$pkey = explode('@',$pkey);
$userId = (int)session('WST_USER.userId');
$obj["userId"] = $userId;
$obj["orderNo"] = $pkey[0];
$obj["isBatch"] = (int)$pkey[1];
$data = $m->checkOrderPay2($obj);
if($data["status"]==1){
$order = $m->getPayOrders($obj);
$orderAmount = $order["needPay"];
$payRand = $order["payRand"];
$out_trade_no = $obj["orderNo"]."a".$payRand;
$passback_params = $payObj."@".$userId."@".$obj["isBatch"];
$subject = '支付购买商品费用'.$orderAmount.'元';
$body = '支付订单费用';

$returnUrl = url("home/paypal/payorders","",true,true);
}

if($data["status"]==1){

//请求
$result = $this->buildRequestForm ($orderAmount,$out_trade_no,$returnUrl,$passback_params);
$data["result"]= $result;
return $data;
}else{
if(!(isSet($data["msg"])) || $data["msg"]=="") $data["msg"]= "支付失败";
return $data;
}

}

private function buildRequestForm($order_amount,$order_sn,$returnUrl,$passback_params) {
$m = new M();
$payment = $m->getPayment("paypal");
$data_pay_account = $payment['paypalcn_account'];
$data_notify_url = url("home/paypal/respond","",true,true);
$cancel_return = url("home/paypal/cancel","",true,true);

$url="https://www.sandbox.paypal.com/cgi-bin/webscr";
//$url="https://www.paypal.com/cgi-bin/webscr";
$sHtml = '<form id="form" action="'.$url.'" method="post">' . // 不能省略
"<input type='hidden' name='cmd' value='_xclick'>" . // 不能省略
"<input type='hidden' name='business' value='$data_pay_account'>" . // 贝宝帐号
"<input type='hidden' name='return' value='$returnUrl'>" . // 付款后页面
"<input type='hidden' name='amount' value='$order_amount'>" . // 订单金额
"<input type='hidden' name='invoice' value='$order_sn'>" . // 订单号
"<input type='hidden' name='charset' value='utf-8'>" . // 字符集
"<input type='hidden' name='no_shipping' value='1'>" . // 不要求客户提供收货地址
"<input type='hidden' name='no_note' value='0'>" . // 付款说明
"<input type='hidden' name='currency_code' value='".self::Currency."'>" . // 货币:人民币
"<input type='hidden' name='notify_url' value='$data_notify_url'>" .
"<input type='hidden' name='rm' value='2'>" .
"<input type='hidden' name='custom' value='$passback_params'>" .
"<input type='hidden' name='cancel_return' value='$cancel_return'>" .
"<input type='submit' value='ok' style='display:none;''>".
"</form><script>document.getElementById('form').submit();</script>";
return $sHtml;
}
function payorders(){
print_r($_GET);
//if($this->aliCheck($_GET)){
// $this->redirect(url("home/alipays/paysuccess"));
// }else{
// $this->error('支付失败');
//}
}

function cancel(){
echo "<script>alert('取消支付。');
history.go(-2);
</script>";
exit();
}

/**
* 响应操作
*/
function respond(){
$m = new M();
$payment = $m->getPayment("paypal");
$merchant_id = $payment['paypalcn_account']; ///获取商户编号

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross']; // 付款金额
$payment_currency = $_POST['mc_currency'];
$trade_no = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$order_sn = $_POST['invoice']; //订单号
$memo = !empty($_POST['memo']) ? $_POST['memo'] : '';
$extras = $_POST['custom'];

$url='www.paypal.com';
$url='www.sandbox.paypal.com';

$paypal_url=$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$paypal_url.'/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.$paypal_url));

$res = curl_exec($ch);
curl_close($ch);
if (strcmp ($res, "VERIFIED") == 0) {
if ($payment_status != 'Completed' && $payment_status != 'Pending'){

return false;
}

$this->order_paid($order_sn,$trade_no,$payment_amount,$extras);

}

}

}

        

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!