1. 首先去開發(fā)者平臺注冊測試賬號,注冊網(wǎng)站是
2. 好了之后再進入Sandbox建立測試用的Paypal虛擬帳號(至少應(yīng)該建立一個Business的和一個Personal的),這個虛擬帳號可以建一個商家號和一個支付號,可用于測試,里面的金額可以自由設(shè)定。
3. 商家號那里一般填寫的是郵箱(測試的時候,正式的商家號不是郵箱)。
4. 需要注意的是:
5. 1、把相關(guān)配置信息盡量放到數(shù)據(jù)庫中。
6. 2、測試帳號下的支付完成后是不會觸發(fā)異步回調(diào)地址的,需要到開發(fā)者平臺的去模擬相關(guān)觸發(fā)。
7. 3、為了回調(diào)支付準(zhǔn)確,增加了一個可以自定義的驗證密鑰,參與加密驗證。
8. 基本配置信息:
function __construct() {
parent::__construct();
$testConfig = array(
'payUrl' => 'https://www.sandbox.paypal.com/cgi-bin/webscr', //支付地址
'business' => '3HSV6******', //商家號
'currency_code' => 'USD', //支付幣種 具體代碼可參考:https://developer.paypal.com/docs/classic/api/currency_codes/
'lc' => 'US', //支付頁面的語言 具體代碼可參考:https://developer.paypal.com/docs/classic/api/country_codes/
'key' => 'siteape', //個人加密字符串
);
$prodConfig = array(
'payUrl' => 'https://www.paypal.com/cgi-bin/webscr', //支付地址
'business' => '3HSV6******', //商家號
'currency_code' => 'USD', //支付幣種 具體代碼可參考:https://developer.paypal.com/docs/classic/api/currency_codes/
'lc' => 'US', //支付頁面的語言 具體代碼可參考:https://developer.paypal.com/docs/classic/api/country_codes/
'key' => 'siteape', //個人加密字符串
);
$payConfig = $testConfig; //這里切換測試環(huán)境和生產(chǎn)環(huán)境
$this->payConfig = $payConfig;
}
9. paypal支付提交處理
public function paypalpay() {
$oid = I('oid');
//數(shù)據(jù)信息
$order = M('order')->find($oid); //訂單信息
if (!$order) {
$this->display('Public:404');
exit();
}
$data = $this->payConfig;
//訂單詳情信息
$orderDetail = M('order_detail')->where(array('oid' => $oid))->select();
$goodsInfo = '';
foreach ($orderDetail as $k => $v) {
$product = M($v['name'])->field('title')->find($v['pid']);
$product_color = M($v['name'] . '_colors')->field('title')->find($v['color_id']);
$goodsInfo .= $product['title'] . '-' . $product_color['title'] . '-' . $v['diameter'] . '-' . $v['number'] . '-';
}
//加密傳值
$signSrc = '';
$signArr = array(
'invoice' => $order['order_num'], //自定義訂單號
'key' => $data['key'], //自定義的加密字符串
);
foreach ($signArr as $k => $v) {
$signSrc .= $v;
}
$signInfo = strtoupper(hash("sha256", $signSrc));
//表單提交
$formData = array(
'cmd' => '_xclick',
'business' => $data['business'],
'item_name' => string_replace($goodsInfo),
'currency_code' => $data['currency_code'],
'amount' => $order['real_money'],
'notify_url' => C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U('Pay/paypalnotifyurl'),
'return' => C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U('Order/info', array('order_num' => $order['order_num'], 'paypal' => 1)), //支付成功后網(wǎng)頁跳轉(zhuǎn)地址把order_num帶上
'cancel_return' => C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U('Order/info', array('order_num' => $order['order_num'])), //用戶取消支付后跳轉(zhuǎn)的地址
'invoice' => $order['order_num'], //自定義訂單號
'no_shipping' => 1,
'custom' => $signInfo, //自定義變量 原樣返回 訂單id號
'charset' => 'utf8',
'lc' => $data['lc'],
);
header("Content-type: text/html; charset=utf-8");
//輸出提交表單
$payForm = '<form action="' . $data['payUrl'] . '" method="post" id="form" name="form">' . inputs($formData) . '</form><script>document.form.submit()</script>';
echo $payForm;
}
10. paypal回調(diào)地址
public function paypalnotifyurl() {
$data = $this->payConfig;
//加密接收值
$getData = array(
'invoice' => I('post.invoice'),
'key' => $data['key'],
);
//鍵名和鍵值和上面的原加密字符串一致
$payment_status = I('post.payment_status');
$custom = I('post.custom'); //接收的加密字符串
$signSrc = '';
foreach ($getData as $k => $v) {
$signSrc .= $v;
}
$signInfo = strtoupper(hash("sha256", $signSrc));
//驗證與修改
if ($signInfo == $custom && $payment_status == 'Completed') {
//改變訂單的狀態(tài)為已支付
$saveData = array(
'pay_create_time' => time(),
'pay_sign_info' => $signInfo,
'status' => 2,
);
$re = M('order')->where(array('order_num' => $getData['invoice']))->data($saveData)->save();
$payResult = "Congratulations,payment is successful !";
} else {
$payResult = "Data validation failed";
}
/*支付的異步記錄備查*/
$logData = I('post.');
$logfile = . '/payPalLog_c451f8e6e53014.txt';
import("Org.Util.File");
$logObj = new \File($logfile);
$logfile = $logObj->getRealFile();
file_put_contents($logfile, serialize($logData), FILE_APPEND);
}
}
11. 組裝表單域
function inputs($data){
$inputs='';
foreach($data as $k=>$v){
$inputs.='<input type="hidden" name="'.$k.'" value="'.$v.'"/>';
}
return $inputs;
}
如沒特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請注明來自http://m.oulysa.com/news/6461.html