醉近在做一個國外支付的項目 看到論壇上面那個v9的PayPal支付模塊 有點問題 所以自己搞了一個
弟一步:添加語言包 添加下面代碼到 phpcms\\languages\\zh-cn\\pay.lang.php
$LANG['paypal'] = 'PayPal支付'; $LANG['paypal_tip'] = 'PayPal, 全球在線收付款解決方案領(lǐng)導(dǎo)者,快速,安全便捷的網(wǎng)上支付平臺,為中國提供PayPal新用戶培訓(xùn)及電子商務(wù)一站式專業(yè)服務(wù)。<a href="http://www.paypal.com" target="_blank"><font color="red">立即在線申請Paypal</font></a>'; $LANG['paypal_account'] = '商戶帳號'; $LANG['paypal_currency'] = '支付貨幣'; $LANG['paypal_sandbox'] = '測試環(huán)境(Sandbox)'; $LANG['paypal_sandbox_range'][0] = "開啟"; $LANG['paypal_sandbox_range'][1] = "關(guān)閉"; $LANG['paypal_currency_range']['AUD'] = '澳元'; $LANG['paypal_currency_range']['CAD'] = '加元'; $LANG['paypal_currency_range']['EUR'] = '歐元'; $LANG['paypal_currency_range']['GBP'] = '英鎊'; $LANG['paypal_currency_range']['JPY'] = '日元'; $LANG['paypal_currency_range']['USD'] = '美元'; $LANG['paypal_currency_range']['HKD'] = '港元';
第二步:保存下面代碼命名為“PayPal.class.php”到\\phpcms\\modules\\pay\\classes
<?php
if (isset($set_modules) && $set_modules == TRUE)
{
$i = isset($modules) ? count($modules) : 0;
$modules[$i]['code'] = basename(__FILE__, '.class.php');
$modules[$i]['name'] = L('paypal', '', 'pay');
$modules[$i]['desc'] = L('paypal_tip', '', 'pay');
$modules[$i]['is_cod'] = '0';
$modules[$i]['is_online'] = '1';
$modules[$i]['author'] = 'PHPCMS開發(fā)團隊';
$modules[$i]['website'] = 'http://www.paypal.com';
$modules[$i]['version'] = '1.0.0';
$modules[$i]['config'] = array(
array('name' => 'paypal_account','type' => 'text','value' => ''),
array('name' => 'paypal_currency','type' => 'select','value' => 'USD'),
array('name' => 'paypal_sandbox','type' => 'select','value' => '1')
);
return;
}
pc_base::load_app_class('pay_abstract','','0');
class PayPal extends paymentabstract{
public function __construct($config = array()) {
if (!empty($config)) $this->set_config($config);
$paypal_currency_arr = array('AUD','CAD','EUR','GBP','JPY','USD','HKD');
$this->config['currency'] = in_array($this->config['paypal_currency'],$paypal_currency_arr) ? $this->config['paypal_currency'] : 'USD';
if($this->config['paypal_sandbox']==0){
$this->config['gateway_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$this->config['verify_url'] = 'www.sandbox.paypal.com';
}
else{
$this->config['gateway_url'] = 'https://www.paypal.com/cgi-bin/webscr';
$this->config['verify_url'] = 'www.paypal.com';
}
$this->config['gateway_method'] = 'POST';
$this->config['return_url'] = return_url('PayPal');
$this->config['notify_url'] = return_url('PayPal',1);
//pc_base::load_app_func('alipay');
}
public function getpreparedata() {
//固定值
$prepare_data['cmd'] = '_xclick';
//商戶帳號
$prepare_data['business'] = $this->config['paypal_account'];
//返回地址
$prepare_data['return'] = $this->config['return_url'];
//字符集
$prepare_data['charset'] = CHARSET;
//不提示郵寄低級
$prepare_data['no_shipping'] = 1;
//付款說明
$prepare_data['no_note'] = '';
//支付貨幣
$prepare_data['currency_code'] = $this->config['currency'];
//支付通知地址
$prepare_data['notify_url'] = $this->config['notify_url'];
//訂單號
$prepare_data['invoice'] = $this->order_info['id'];
//訂單名稱
$prepare_data['item_name'] = $this->product_info['name'];
//訂單金額
$prepare_data['amount'] = $this->product_info['price'];
return $prepare_data;
}
/**
* GET 支付后返回(沒有對交易結(jié)果進行寫入)
*
*/
public function receive() {
showmessage('交易完成 返回查看交易結(jié)果',APP_PATH.'index.php?m=pay&c=deposit&a=init');
return false;
}
/**
* POST接收數(shù)據(jù)
* 狀態(tài)碼說明 (0 交易完成 1 交易失敗 2 交易操時 3 交易處理中 4 交易未支付 5交易取消6交易發(fā)生錯誤)
*/
public function notify() {
$req = 'cmd=_notify-validate';
foreach($_POST as $key=>$value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$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";
$fp = fsockopen($this->config['verify_url'], 80, $errno, $errstr, 30);
$item_invoice = $_POST['invoice'];
$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'];
$return_data['order_id'] = $item_invoice;
$return_data['order_total'] = $return_data['price'] = $payment_amount;
//判斷回復(fù) POST 是否創(chuàng)建成功
if (!$fp) {
$return_data['order_status'] = 6;
fclose($fp);
error_log(date('m-d H:i:s',SYS_TIME).'| POST: HTTP errer |'."\\r\\n", 3, CACHE_PATH.'pay_error_log.php');
return $return_data;
} else {
fputs($fp, $header.$req);
while (!feof($fp)) {
$res = fgets($fp, 1024);
//已經(jīng)通過認證
if (strcmp ($res, "VERIFIED") == 0) {
if($payment_status != 'Completed'){
$return_data['order_status'] = 3;
fclose($fp);
return $return_data;
}
if($receiver_email != $this->config['paypal_account'])
{
$return_data['order_status'] = 6;
fclose($fp);
error_log(date('m-d H:i:s',SYS_TIME).'| POST: receiver_email errer |'."\\r\\n", 3, CACHE_PATH.'pay_error_log.php');
return $return_data;
}
if($payment_currency != $this->config['currency'])
{
$return_data['order_status'] = 6;
fclose($fp);
error_log(date('m-d H:i:s',SYS_TIME).'| POST: currency errer |'."\\r\\n", 3, CACHE_PATH.'pay_error_log.php');
return $return_data;
}
$return_data['order_status'] = 0;
} else if (strcmp($res, "INVALID") == 0) {
$return_data['order_status'] = 6;
error_log(date('m-d H:i:s',SYS_TIME).'| POST: INVALID errer |'."\\r\\n", 3, CACHE_PATH.'pay_error_log.php'); return $return_data;
}
}
fclose($fp);
}
return $return_data;
}
/**
* 相應(yīng)服務(wù)器應(yīng)答狀態(tài)
* @param $result
*/
public function response($result) {
if (FALSE == $result) echo 'fail';
else echo 'success';
}
}
?>說明:
要正常使用PayPal后臺設(shè)置”即時付款通知“必須開啟 不然支付成功后系統(tǒng)不能收到PayPal 通知消息
保存文件編碼根據(jù)自己網(wǎng)站編碼來
支付不能在內(nèi)網(wǎng)測試 應(yīng)為內(nèi)網(wǎng)收不到PayPal 通知消息
