博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
农业银行快捷支付php版说明和实例代码
阅读量:5905 次
发布时间:2019-06-19

本文共 17477 字,大约阅读时间需要 58 分钟。

接入的是shopnc,代码改改就可以用了,虽然不是一个完善的类,也可以按照类的方法直接调用,省得再去扣开发文档 农行在接收返回信息也会验证一次,还有一点就是页面通知返回结果 一定要用服务器通知,不然会出异常问题,导致问题
2016年5月12日11:15:41

  大概有一下几个文件,

payment_info = $payment_info; $this->order_info = $order_info; } //提交订单 public function submit() { // var_dump($this->payment_info);// // var_dump($this->order_info);// die; //没有自己传参的不要随意修改 $this->order["PayTypeID"] = 'ImmediatePay'; //设定交易类型 $this->order["OrderNo"] = $this->order_info['pay_sn']; //设定订单编号 自己传参数 $this->order["OrderAmount"] = $this->order_info['api_pay_amount']; //设定交易金额 自己传参数// $this->order["OrderAmount"] = '0.01'; //设定交易金额 自己传参数 测试使用 $this->order["CurrencyCode"] = 156; //设定交易币种 $this->order["InstallmentMark"] = 0; //分期标识 $this->order["OrderDate"] = date('Y/m/d'); //设定订单日期 (必要信息 - YYYY/MM/DD) $this->order["OrderTime"] = date('H:i:s'); //设定订单时间 (必要信息 - HH:MM:SS) $this->order["CommodityType"] = '0202'; //设置商品种类 //2、订单明细 $orderitem = array (); $orderitem["ProductName"] = "GG"; //商品名称 自己写随意 $this->orderitems[0] = $orderitem; //3、生成支付请求对象 $this->request["TrxType"] = self::TRX_TYPE_PAY_REQ; //设定交易类型 $this->request["PaymentType"] = 'A'; //设定支付类型 $this->request["PaymentLinkType"] = 1; //设定支付接入方式 $this->request["NotifyType"] = 1; //设定通知方式 $this->request["ResultNotifyURL"] = SHOP_SITE_URL."/api/payment/abchina/notify_url.php"; //设定通知URL地址 自己设定 $this->request["IsBreakAccount"] = 0; //设定交易是否分账 $aMerchantNo =1; //取得交易报文 $tRequestMessage = $this->getRequestMessage(); // echo '
';//            var_export($tRequestMessage);//            echo '
';// die; //组成完整交易报文 $tRequestMessage = $this->composeRequestMessage($aMerchantNo,$tRequestMessage); // echo '
';//            var_export($tRequestMessage);//            echo '
';// die; //对交易报文进行签名 $tRequestMessage = $this->signMessage($aMerchantNo, $tRequestMessage);// echo '
';//            var_export($tRequestMessage);//            echo '
';// die; //发送交易报文至网上支付平台 $tResponseMessage = $this->sendMessage($aMerchantNo,$tRequestMessage); //验证网上支付平台响应报文的签名 $this->res = $this->verifySign($tResponseMessage); // echo '
';//            var_export($tResponseMessage);//            echo '
'; $PaymentURL = self::GetValue($tResponseMessage,'PaymentURL') ; $ReturnCode = self::GetValue($tResponseMessage,'ReturnCode') ; //验证报文签名正确而却返回码是0000,就可以跳转支付页面 if($this->res == 1 && $ReturnCode=='0000' ){ header("Location:".$PaymentURL); die; }else{ header('
'); die('abchina pay check error'); }// var_dump($PaymentURL);// // var_dump($ReturnCode); // echo '
';//            var_export(json_decode($tResponseMessage,true));//            echo '
'; // var_dump($res);// die; } public static function arrayRecursive(&$array, $function, $apply_to_keys_also = false){ foreach ($array as $key => $value) { $array[$key] = $function($value); } } protected function getRequestMessage() { self :: arrayRecursive($this->order, "urlencode", false); self :: arrayRecursive($this->request, "urlencode", false); $js = '"Order":' . (json_encode(($this->order))); $js = substr($js, 0, -1); $js = $js . ',"OrderItems":['; $count = count($this->orderitems, COUNT_NORMAL); for ($i = 0; $i < $count; $i++) { self :: arrayRecursive($this->orderitems[$i], "urlencode", false); $js = $js . json_encode($this->orderitems[$i]); if ($i < $count -1) { $js = $js . ','; } } $js = $js . ']}}'; $tMessage = json_encode($this->request); $tMessage = substr($tMessage, 0, -1); $tMessage = $tMessage . ',' . $js; $tMessage = urldecode($tMessage); return $tMessage; } private function composeRequestMessage($aMerchantNo,$aMessage) { $tMessage = "{\"Version\":\"V3.0.0\",\"Format\":\"JSON\",\"Merchant\":" . "{\"ECMerchantType\":\"" . "EBUS" . "\",\"MerchantID\":\"" . self::MERCHANTID . "\"}," . "\"TrxRequest\":" . $aMessage . "}"; return $tMessage; } private function signMessage($aMerchantNo, $aMessage) {// public function signMessageOp() { //1、读取证书 // $tTrustPayCertFile = dirname(__FILE__).'/TrustPay.cer'; $tTrustPayCertFile = "fuwuqi.pfx";// $iTrustpayCertificate = openssl_x509_read(self :: der2pem(file_get_contents($tTrustPayCertFile))); // var_dump($tTrustPayCertFile);// var_dump(self::MERCHANTCERTPASSWORD);// die; openssl_pkcs12_read(file_get_contents($tTrustPayCertFile), $tCertificate, self::MERCHANTCERTPASSWORD); // var_dump($tCertificate);// die; //2、验证证书是否在有效期内 $cer = openssl_x509_parse($tCertificate['cert']);// var_dump($cer); //3、取得密钥 $pkey = openssl_pkey_get_private($tCertificate['pkey']);// var_dump($pkey); $key = $pkey;// $key = self :: $iMerchantKeys[$aMerchantNo -1]; $signature = ''; $data = strval($aMessage); if (!openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA1)) { return null; } $signature = base64_encode($signature); $tMessage = "{\"Message\":$data" . "," . '"Signature-Algorithm":' . '"' . self :: SIGNATURE_ALGORITHM . '","Signature":"' . $signature . '"}'; return $tMessage; // return $pkey;// var_dump($iTrustpayCertificate); } private function sendMessage($aMerchantNo, $aMessage) { //组成
段 $tMessage = strval($aMessage); $tURL = self::GATEWAY; $opts = array( 'http' => array( 'method' => 'POST', 'user_agent' => 'TrustPayClient V3.0.0', 'protocol_version' => 1.0, 'header' => array('Content-Type: text/html', 'Accept: */*'), 'content' => $tMessage ), 'ssl' => array( 'verify_peer' => false ) ); $context = stream_context_create($opts); $tResponseMessage = file_get_contents($tURL, false, $context); // $tTrxResponse = self::init($tResponseMessage); return $tResponseMessage; } private static function der2pem($der_data) { $pem = chunk_split(base64_encode($der_data), 64, "\n"); $pem = "-----BEGIN CERTIFICATE-----\n" . $pem . "-----END CERTIFICATE-----\n"; return $pem; } public static function verifySign($aMessage) { $TrustPayFile = "TrustPay.cer"; $iTrustpayCertificate = openssl_x509_read(self :: der2pem(file_get_contents($TrustPayFile))); $tTrxResponse = self::GetValue($aMessage,'Message') ; $tSignBase64 = self::GetValue($aMessage,'Signature') ; $tSign = base64_decode($tSignBase64); $key = openssl_pkey_get_public($iTrustpayCertificate); $data = strval($tTrxResponse); return openssl_verify($data, $tSign, $key, OPENSSL_ALGO_SHA1); } public function GetValue($json,$aTag) { $json = $json; $index = 0; $length = 0; $index = strpos($json, $aTag, 0); if ($index === false) return ""; do { if($json[$index-1] === "\"" && $json[$index+strlen($aTag)] === "\"") { break; } else { $index = strpos($json, $aTag, $index+1); if ($index === false) return ""; } } while (true); $index = $index + strlen($aTag) + 2; $c = $json[$index]; if ($c === '{') { $output = self::GetObjectValue($index, $json); } if ($c === '"') { $output = self::GetStringValue($index, $json); } return $output; } private function GetObjectValue($index, $json) { $count = 0; $_output = ""; do { $c = $json[$index]; if ($c === '{') { $count++; } if ($c === '}') $count--; if ($count !== 0) { $_output =$_output.$c; } else { $_output = $_output.$c; return $_output; } $index++; } while (true); } private function GetStringValue($index, $json) { $index++; $_output = ""; do { $c = $json[$index++]; if ($c !== '"') { $_output = $_output.$c; } else { return $_output; } } while (true); } /** * 返回地址验证(同步) * * @param * @return boolean */ public function return_verify(){ $this->order["PayTypeID"] = 'ImmediatePay'; //设定交易类型 $this->order["OrderNo"] = $this->order_info['pay_sn']; //设定订单编号 自己传参数 $this->order["OrderAmount"] = $this->order_info['api_pay_amount']; //设定交易金额 自己传参数// $this->order["OrderAmount"] = '0.01'; //设定交易金额 自己传参数 测试使用 $this->order["CurrencyCode"] = 156; //设定交易币种 $this->order["InstallmentMark"] = 0; //分期标识 $this->order["OrderDate"] = date('Y/m/d'); //设定订单日期 (必要信息 - YYYY/MM/DD) $this->order["OrderTime"] = date('H:i:s'); //设定订单时间 (必要信息 - HH:MM:SS) $this->order["CommodityType"] = '0202'; //设置商品种类 //2、订单明细 $orderitem = array (); $orderitem["ProductName"] = "GG"; //商品名称 自己写随意 $this->orderitems[0] = $orderitem; //3、生成支付请求对象 $this->request["TrxType"] = self::TRX_TYPE_PAY_REQ; //设定交易类型 $this->request["PaymentType"] = 'A'; //设定支付类型 $this->request["PaymentLinkType"] = 1; //设定支付接入方式 $this->request["NotifyType"] = 1; //设定通知方式 $this->request["ResultNotifyURL"] = SHOP_SITE_URL."/api/payment/abchina/notify_url.php"; //设定通知URL地址 自己设定 $this->request["IsBreakAccount"] = 0; //设定交易是否分账 $aMerchantNo =1; //取得交易报文 $tRequestMessage = $this->getRequestMessage(); //组成完整交易报文 $tRequestMessage = $this->composeRequestMessage($aMerchantNo,$tRequestMessage); //对交易报文进行签名 $tRequestMessage = $this->signMessage($aMerchantNo, $tRequestMessage); //发送交易报文至网上支付平台 $tResponseMessage = $this->sendMessage($aMerchantNo,$tRequestMessage); //验证网上支付平台响应报文的签名 $this->res = $this->verifySign($tResponseMessage); $PaymentURL = self::GetValue($tResponseMessage,'PaymentURL') ; $ReturnCode = self::GetValue($tResponseMessage,'ReturnCode') ; //验证报文签名正确而却返回码是0000,就可以跳转支付页面 if($this->res == 1){ return true; }else{ return false; } } public function getPayResult($param){ return $this->res; }}

  

abchina.php

 

 

 

 

notify.php

getValue('Message'); $tSignBase64 = $aMessage->getValue('Signature'); $tSign = base64_decode($tSignBase64); $key = openssl_pkey_get_public($iTrustpayCertificate); $data = strval($tTrxResponse); $result['res'] = openssl_verify($data, $tSign, $key, OPENSSL_ALGO_SHA1); $result['iRspRef'] =$aMessage->getValue('iRspRef'); //交易流水号 $result['OrderNo'] =$aMessage->getValue('OrderNo'); //网站交易订单号 $result['Amount'] =$aMessage->getValue('Amount'); //订单金额 $result['VoucherNo'] =$aMessage->getValue('VoucherNo'); return $result; } $rr = verifySignXML(base64_decode($_POST['MSG']));//var_dump($rr);//die;if ($rr['res'] == 1) {$_GET['act'] = 'payment';$_GET['op'] = 'return';$_GET['payment_code'] = 'abchina'; $_GET['trade_no'] = $rr['iRspRef'];$_GET['out_trade_no'] = $rr['OrderNo'];$_GET['bank_total'] = $rr['Amount'];$_GET['extra_common_param'] = 'real_order';require_once(dirname(__FILE__).'/../../../index.php');} else {die('abchina notify check error');}

  XMLDocument.php

iXMLString, '<'); $tEndIndex = strpos($this->iXMLString, '>'); if ($tEndIndex > $tStartIndex) { $tTagName = substr($this->iXMLString, $tStartIndex + 1, $tEndIndex - ($tStartIndex + 1)); } return $tTagName; } public function __construct($aXMLString='') { $this->init($aXMLString); } public function init($aXMLString) { $this->iXMLString = $aXMLString; return $this; } public function __toString() { return $this->iXMLString; } public function getValue($aTag) { $tXMLDocument = null; $tStartIndex = strpos($this->iXMLString, '<'.trim($aTag).'>'); $tEndIndex = strpos($this->iXMLString, '
'); if (($tStartIndex !== FALSE) && ($tEndIndex !== FALSE) && ($tStartIndex < $tEndIndex)) { $tXMLDocument = substr($this->iXMLString, $tStartIndex + strlen($aTag) + 2, $tEndIndex - ($tStartIndex + strlen($aTag) + 2)); } return $tXMLDocument; } public function getValueNoNull($aTag) { $tValue = ""; $tXML = $this->getValue($aTag); if ($tXML !== null) { $tValue = $tXML; } return $tValue; } public function getValueArray($aTag) { $tValues = array(); $offset = 0; while(TRUE) { $tStartIndex = strpos($this->iXMLString, '<'.trim($aTag).'>', $offset); $tEndIndex = strpos($this->iXMLString, '
', $offset); if (($tStartIndex === FALSE) || ($tEndIndex === FALSE) || ($tStartIndex > $tEndIndex)) { break; } array_push($tValues, new XMLDocument(substr($this->iXMLString, $tStartIndex + strlen($aTag) + 2, $tEndIndex - ($tStartIndex + strlen($aTag) + 2)))); $offset = $tEndIndex + 1; } return $tValues; } public function getValueArrayList($aTag) { return $this->getValueArray($aTag); } public function getDocuments($aTag) { return $this->getValueArray($aTag); } public function getFormatDocument($aSpace) { return $this->getFormatDocumentLevel(0, $aSpace); } private function getFormatDocumentLevel($aLevel, $aSpace) { $tSpace1 = str_repeat($aSpace, $aLevel + 1); $tTagName = $this->getFirstTagName(); if ($tTagName === null) { return $this; } $tXMLString = "\n"; $tXMLDocument = new XMLDocument($this->iXMLString); while (($tTagName = $tXMLDocument->getFirstTagName()) !== null) { $tTemp = $tXMLDocument->getValue($tTagName); $tSpace = ""; if ($tTemp->getFirstTagName() !== null) { $tSpace = $tSpace1; } $tXMLString = "$tXMLString$tSpace1<$tTagName>".$tTemp->getFormatDocumentLevel($aLevel + 1, $aSpace)."$tSpace
\n"; $tXMLDocument = $tXMLDocument->deleteFirstTagDocument(); } return new XMLDocument($tXMLString); } public function deleteFirstTagDocument() { $tTagName = $this->getFirstTagName(); $tStartIndex = strpos($this->iXMLString, "<$tTagName>"); $tEndIndex = strpos($this->iXMLString, "
"); if ($tEndIndex > $tStartIndex) { $this->iXMLString = substr($this->iXMLString, $tEndIndex + strlen($tTagName) + 3); if ($this->iXMLString === FALSE) { $this->iXMLString = ""; } } return $this; }}?>

 

 

代码包和开发文档说明下载地址 :http://download.csdn.net/detail/zh7314/9517627

便宜没好货你懂的,或者你可以直接复制代码也是可以使用的

 

转载于:https://www.cnblogs.com/zx-admin/p/5484962.html

你可能感兴趣的文章
Cox 教学视频5
查看>>
Jenkins持续集成学习-搭建jenkins问题汇总
查看>>
使用ffmpeg实现对h264视频解码 -- (实现了一个易于使用的c++封装库)
查看>>
flink watermark介绍
查看>>
Android Xutils 框架
查看>>
Sysbench 0.5版安装配置
查看>>
书摘—你不可不知的心理策略
查看>>
【博客话题】毕业——开始人生的艰苦历程
查看>>
Linux安装telnet
查看>>
sap scriptfom 多语言翻译
查看>>
黄聪:3分钟学会sessionStorage用法
查看>>
Entity Framework 全面教程详解(转)
查看>>
Windows上Python2.7安装Scrapy过程
查看>>
Chapter 3:Code Style in Django
查看>>
挖掘数据金矿 领军协同创新 曙光荣膺“2016大数据创新应用领袖企业”称号
查看>>
Fast通道获得Win10 Mobile Build 14977更新
查看>>
Firefox 跟踪保护技术将页面加载时间减少 44%
查看>>
java解析虾米音乐
查看>>
mysql 多行合并函数
查看>>
艾级计算机的发展与挑战
查看>>