###第三方支付--通联支付
1.文档地址:[通联H5对接文档](http://www.allinpaysupport.com/news_show.aspx?id=110)
2.对接流程:用户注册请求接口-->页面订单提交接口-->支付结果回调接口
# 整理数据
<?php
if (!(defined('IN_IA')))
{
exit('Access Denied');
}
/**
* Class Allinpay_EweiShopV2Model
* 通联支付交易处理
* https://cashier.allinpay.com/mobilepayment/mobile/SaveMchtOrderServlet.action
*/
class Allinpay_EweiShopV2Model
{
private $HOST;
private $MCHTID;// 商户id
static private $ORGID;// 机构号
static private $receiveUrl; // 后台回调地址
static private $pickupUrl;// 前台跳转地址
public function __construct()
{
global $_W;
$setting = uni_setting_load('payment', $_W['uniacid']);
$this->MCHTID = $setting['payment']['allinpay']['merchantId'];
self::$receiveUrl = trim($setting['payment']['allinpay']['receiveUrl']);
self::$pickupUrl = trim($setting['payment']['allinpay']['pickupUrl']);
}
public function postdata($order_id){
if($order_id==''){
return false;
}
// 字符集(注意顺序&文档地址:http://www.allinpaysupport.com/news_show.aspx?id=110)
//基本必填数据
$data['inputCharset'] = 1;//表示utf-8格式
$data['pickupUrl'] = self::$pickupUrl; // 跳转地址
$data['receiveUrl'] = self::$receiveUrl;// 回调通知
$data['version'] = "v1.0"; // 版本
$data['language'] = 1; // 语言
$data['signType'] = 0;//默认填 0 md5验证,1 证书验证;
// 商户号
$data['merchantId'] = $this->MCHTID;
// 订单号
$orderInfo = pdo_get('ewei_shop_order',['id'=>$order_id],['ordersn','price']);
$data['orderNo'] = $orderInfo['ordersn'];
// 订单总金额
//$data['orderAmount'] = (int)$orderInfo['price']*100;//强制转换整型
$data['orderAmount'] = 1;//强制转换整型
$data['orderCurrency'] = 0; // 0 和 156 代表人民币、840 代表美元、344 代表港币,跨境支付商户不建议使用 0
// 提交时间
$data['orderDatetime'] = date('YmdHis');
// 通联用户id
$allinpay_uid = self::reg_data();
$data['ext1'] = "<USER>$allinpay_uid</USER>"; // H5必须带这个参数
$data['ext2'] = "first";//商户自定义通联会原样fanh
//$data['ext1'] = "<USER>180324786448063</USER>"; // H5必须带这个参数
// 支付类型
$data['payType'] = 33; // H5支付
$data['signMsg'] = self::sign($data);
return $data;
}
//签名处理
public static function sign($data){
return com('Allinpay')->signdata($data);
}
//注册会员
public function reg_data(){
$mid = m('member')->getMid();//获取用户id
$data['signType'] = 0;
$data['merchantId'] = $this->MCHTID;
// 用户id
$data['partnerUserId'] =$mid;
$data['signMsg'] =com('Allinpay')->signdata2($data);
//H5必须注册&模拟post提交数据
$url = "https://cashier.allinpay.com/usercenter/merchant/UserInfo/reg.do";
load()->func('communication');//微擎自带curl
$result = ihttp_request($url,$data);
$allinpay_uid=json_decode($result['content'],true);
return $allinpay_uid['userId'];
}
//验签处理
// public static function checksign($data){
// $check=com('Allinpay')->verify_sign($data);
// $check_one=com('Allinpay')->verify_sign($data_one);
// }
}
<?php
/**
* 通联支付签名&验证签名
*/
if (!(defined('IN_IA')))
{
exit('Access Denied');
}
class Allinpay_EweiShopV2ComModel extends ComModel{
// 签名处理
public function signdata($data,$key='1234567890'){
$bufSignSrc="";
if($data['inputCharset'] !== "")
$bufSignSrc=$bufSignSrc."inputCharset=".$data['inputCharset']."&";
if($data['pickupUrl'] != "")
$bufSignSrc=$bufSignSrc."pickupUrl=".$data['pickupUrl']."&";
if($data['receiveUrl'] != "")
$bufSignSrc=$bufSignSrc."receiveUrl=".$data['receiveUrl']."&";
if($data['version'] != "")
$bufSignSrc=$bufSignSrc."version=".$data['version']."&";
if($data['language'] != "")
$bufSignSrc=$bufSignSrc."language=".$data['language']."&";
if($data['signType'] !== "")
$bufSignSrc=$bufSignSrc."signType=".$data['signType']."&";
if($data['merchantId'] != "")
$bufSignSrc=$bufSignSrc."merchantId=".$data['merchantId']."&";
if($data['orderNo'] != "")
$bufSignSrc=$bufSignSrc."orderNo=".$data['orderNo']."&";
if($data['orderAmount'] !== "")
$bufSignSrc=$bufSignSrc."orderAmount=".$data['orderAmount']."&";
if($data['orderCurrency'] !== "")
$bufSignSrc=$bufSignSrc."orderCurrency=".$data['orderCurrency']."&";
if($data['orderDatetime'] !== "")
$bufSignSrc=$bufSignSrc."orderDatetime=".$data['orderDatetime']."&";
if($data['ext1'] !== "")
$bufSignSrc=$bufSignSrc."ext1=".$data['ext1']."&";
if($data['ext2'] !== "")
$bufSignSrc=$bufSignSrc."ext2=".$data['ext2']."&";
if($data['payType'] !== "")
$bufSignSrc=$bufSignSrc."payType=".$data['payType']."&";
$bufSignSrc=$bufSignSrc."key=".$key; //key为MD5密钥,密钥是在通联支付网关商户服务网站上设置。
$bufSignSrc = trim($bufSignSrc);
return strtoupper(md5($bufSignSrc));
}
/*
* 注册会员
*/
public function register($data,$key='1234567890'){
$bufSignSrc="";
if($data['signType'] !== "")
$bufSignSrc=$bufSignSrc."signType=".$data['signType']."&";
if($data['merchantId'] != "")
$bufSignSrc=$bufSignSrc."merchantId=".$data['merchantId']."&";
if($data['partnerUserId'] != "")
$bufSignSrc=$bufSignSrc."partnerUserId=".$data['partnerUserId']."&";
$bufSignSrc='&'.$bufSignSrc."key=".$key.'&';
$bufSignSrc = trim($bufSignSrc);
/*
* 注册成功返回的数据
* {"signType":"0","userId":"180324786448063","signMsg":"0341F5AB99009F83C2A8A164317D57B98FAD0F8CED91A3FFD30A4FCA8E46F4E8F5F2B050117AFDF5543E5ED79BA1F5AE9F6D30758BA5EDD068DF919575A7CB2C0C1562E15C0F840C2A4B16215B14F7E0D7A4BFC6EB36CF16CCC04A4B3B964E26407E26AB258CFE73C3E4A2D282637B7F046061040A9EAEFDEDD94D0917D3C164","returnDatetime":"20180324140549","resultCode":"0000","YWMA":"2001","merchantId":"009440148160281","JYMA":"0002","CZMA":"13"}
* */
return strtoupper(md5($bufSignSrc));
}
// 验证签名(待完善)
// public function verify_sign($signMsg,$data,$key='1234567890'){
//// require("../../../../payment/allinpay/phpseclib/File/X509.php");
//// require("../../../../payment/allinpay/phpseclib/Crypt/RSA.php");//如果不用证书加密,使用php_rsa.php函数
//// require_once("../../../../payment/allinpay/php_rsa.php");
// $payResult=$data['payResult'];
// $bufSignSrc="";
// if($data['merchantId'] != "")
// $bufSignSrc=$bufSignSrc."merchantId=".$data['merchantId']."&";
// if($data['version'] != "")
// $bufSignSrc=$bufSignSrc."version=".$data['version']."&";
// if($data['language'] != "")
// $bufSignSrc=$bufSignSrc."language=".$data['language']."&";
// if($data['signType'] != "")
// $bufSignSrc=$bufSignSrc."signType=".$data['signType']."&";
// if($data['payType'] != "")
// $bufSignSrc=$bufSignSrc."payType=".$data['payType']."&";
// if($data['paymentOrderId'] != "")
// $bufSignSrc=$bufSignSrc."paymentOrderId=".$data['paymentOrderId']."&";
// if($data['orderNo'] != "")
// $bufSignSrc=$bufSignSrc."orderNo=".$data['orderNo']."&";
// if($data['orderDatetime'] != "")
// $bufSignSrc=$bufSignSrc."orderDatetime=".$data['orderDatetime']."&";
// if($data['orderAmount'] != "")
// $bufSignSrc=$bufSignSrc."orderAmount=".$data['orderAmount']."&";
// if($data['payDatetime'] != "")
// $bufSignSrc=$bufSignSrc."payDatetime=".$data['payDatetime']."&";
// if($data['payAmount'] != "")
// $bufSignSrc=$bufSignSrc."payAmount=".$data['payAmount']."&";
// if($data['payResult'] != "")
// $bufSignSrc=$bufSignSrc."payResult=".$data['payResult']."&";
// if($data['errorCode'] != "")
// $bufSignSrc=$bufSignSrc."errorCode=".$data['errorCode']."&";
// if($data['returnDatetime'] != "")
// $bufSignSrc=$bufSignSrc."returnDatetime=".$data['returnDatetime'];
// $bufSignSrc=$bufSignSrc."key=".$key;
// $bufSignSrc = trim($bufSignSrc);
//// $certfile = file_get_contents('TLCert-test.cer');
//// $x509 = new File_X509();
//// $cert = $x509->loadX509($certfile);
//// $pubkey = $x509->getPublicKey();
//// $rsa = new Crypt_RSA();
//// $rsa->loadKey($pubkey); // public key
//// $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
//// $verifyResult = $rsa->verify($bufSignSrc, base64_decode(trim($signMsg)));
//// $value = null;
//
// return strtoupper(md5($bufSignSrc));
// }
}