phpcms v9 开发PayPal支付模块
第一步:添加语言包 添加下面代码到 phpcms\languages\zh-cn\pay.lang.php
$LANG['paypal'] = 'PayPal支付';$LANG['paypal_tip'] = 'PayPal, 全球在线收付款解决方案领导者,快速,安全便捷的网上支付平台,为中国提供PayPal新用户培训及电子商务一站式专业服务。<a href="http://www.paypal.com" target="_blank"><font color="red">立即在线申请Paypal</font></a>';$LANG['paypal_account'] = '商户帐号';$LANG['paypal_currency'] = '支付货币';$LANG['paypal_sandbox'] = '测试环境(Sandbox)';$LANG['paypal_sandbox_range'][0] = "开启";$LANG['paypal_sandbox_range'][1] = "关闭";$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
<?phpif (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开发团队';$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 支付后返回(没有对交易结果进行写入)**/public function receive() {showmessage('交易完成 返回查看交易结果',APP_PATH.'index.php?m=pay&c=deposit&a=init');return false;}/*** POST接收数据* 状态码说明 (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付 5交易取消6交易发生错误)*/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;//判断回复 POST 是否创建成功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);//已经通过认证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;}/*** 相应服务器应答状态* @param $result*/public function response($result) {if (FALSE == $result) echo 'fail';else echo 'success';}}?>
说明:
要正常使用PayPal后台设置”即时付款通知“必须开启 不然支付成功后系统不能收到PayPal 通知消息
保存文件编码根据自己网站编码来
支付不能在内网测试 应为内网收不到PayPal 通知消息