首页 » PHP » 正文

paypal(贝宝)支付接口实现-php

以下代码实现paypal的在线支付接口。代码由php实现。也是pims订单管理系统里使用的方式。

首先是提交页面,我使用的是get方式提交参数过来,你可以改成post。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Paypal支付</title>
</head>

<body onload="javascript:document.py.submit();">
<form action="<?php echo $paypal_pay_url ?>" method="post" name="py">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php echo $paypal_business ?>">
<input type="hidden" name="item_name" value="<?=$_GET['pname']?>">
<input type="hidden" name="amount" value="<?=$_GET['price']?>">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="return" value="http://<?php echo $_SERVER["SERVER_NAME"] ?>/paypal_finish.php">
<input type="hidden" name="invoice" value="<?=$_GET['ordernum']?>">
<input type="hidden" name="charset" value="utf-8">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="no_note" value="">
<input type="hidden" name="notify_url" value="http://<?php echo $_SERVER["SERVER_NAME"] ?>/paypal_finish.php">
<input type="hidden" name="rm" value="2">
<input type="submit" name="Submit" value="Buy it Now" style="display:none" />
</form>
</body>
</html>

php文件代码,接收参数后提交给paypal:

<?php
require_once("../../config.php");
//从 PayPal 出读取 POST 信息同时添加变量cmd
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
//建议在此将接受到的信息记录到日志文件中以确认是否收到 IPN 信息
//将信息 POST 回给 PayPal 进行验证
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type:application/x-www-form-urlencoded\r\n";
$header .= "Content-Length:" . strlen($req) ."\r\n\r\n";
//在 Sandbox 情况下,设置:
//$fp = fsockopen(www.sandbox.paypal.com,80,$errno,$errstr,30);
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
//将 POST 变量记录在本地变量中
//该付款明细所有变量可参考:
//https://www.paypal.com/IntegrationCenter/ic_ipn-pdt-variable-reference.html
$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'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$ordernum=$_POST['invoice'];
//…
//判断回复 POST 是否创建成功
if (!$fp) {
//HTTP 错误
}else {
//将回复 POST 信息写入 SOCKET 端口
fputs ($fp, $header .$req);
//开始接受 PayPal 对回复 POST 信息的认证信息
while (!feof($fp)) {
$res = fgets ($fp, 1024);
//已经通过认证
if (strcmp ($res, "VERIFIED") == 0) {
//检查付款状态
//检查 txn_id 是否已经处理过
//检查 receiver_email 是否是您的 PayPal 账户中的 EMAIL 地址
//检查付款金额和货币单位是否正确
//处理这次付款,包括写数据库
$host=$spConfig['db']['host'];
$logname=$spConfig['db']['login'];
$logpass=$spConfig['db']['password'];
$logdata=$spConfig['db']['database'];
$prefix=$spConfig['db']['prefix'];
mysql_connect($host,$logname,$logpass);
mysql_select_db($logdata);
mysql_query("update ".$prefix."orders set zt2=2 where ordernum='$ordernum'");
echo "Success!<br>";
echo "Order number:".$ordernum."<br>";
echo "Product Name:".$item_name."<br>";
echo "Amount:".$payment_amount."<br>";
}else if (strcmp ($res, "INVALID") == 0) {
//未通过认证,有可能是编码错误或非法的 POST 信息
}
}
fclose ($fp);
}
?>

博主的文章或程序如果给您带来了价值,感谢您打赏一二
微信扫码支付 支付宝扫码支付

发表评论