小鹅通支付接口笔记!
2020-04-30 本文已影响0人
DragonersLi
微信商户被封,使用小鹅通的微信支付!
小鹅通云服务:https://admin.cloud.xiaoe-tech.com
设置消息校验Token、消息加密key、消息接收URL,IP白名单配置服务器IP
image.png后台地址:https://admin.xiaoe-tech.com
登录小鹅通后台设置,装修店铺,上架付费商品。点击分享用二维码或者链接,放入自己平台。用户点击链接或者扫码跳转小鹅通产品详情页进行付费,然后小鹅通会通过回调接口推送支付信息达到付费信息互通。
遇到问题及解决方案:只能在微信打开,第一次要授权获取头像昵称等信息,然后在小鹅通生成一个用户,但是微信信息不能区分用户唯一性,在自己平台,有一个微信对应多个账户情况,要用手机号来区分用户唯一性,然后咨询了小鹅通技术人员,可以使用采集信息功能,用户购买商品前必须输入要采集的内容才行。采集手机号绑定微信。达到目的。
付费类型如下:
image.pngimage.png
信息采集
image.png通过接口注册手机号及微信登录授权账号,信息采集手机号绑定用户:
image.png对接前只知道和微信支付宝等第三方支付差不多,对接后踩坑很多。主要是沟通不到位,整个理清后,感觉只有一个回调接口要写代码,其它都不需要,结果把接口文档上大部分接口对接了,结果用不到。
回调接口处理用户付费成功后相关业务逻辑:
<?php
namespace app\app\controller;
use app\common\controller\AppBase;
use think\Loader;
#官方demo放入tp5项目根目录的extends下XiaoeSdk目录
Loader::import('XiaoeSdk.WXBizMsgCrypt', EXTEND_PATH, '.php');
class Xiaoe extends AppBase
{
private $client_id = 'xxxxxxxxx';//
private $client_sercet = 'xxxxxxxxxxxxxx';
private $grant_type = 'client_credential';//类型固定值
private $shop_id = 'xxxxxxxx';//店铺ID
private $token = 'xxxxxxxxxx_online';//消息验证TOKEN
private $key = 'TN3pZuWTC8RYRLscO6VCR620BqxN7QbkAbUmK5eOBPX';//消息加密KEY
private $notify_url = 'https://m.xxx.com/xiaoe/notify';//消息接收URL
/*
* 注册用户
* 成功返回:
* @return array
* [code:0;msg:ok;data:['user_id:小鹅通ID]]
*/
public function regUser(){
$url = 'https://api.xiaoe-tech.com/xe.user.register/1.0.0';
$data['access_token'] = $this->access_token();
$data['data']['phone'] = '18858287938';
$data['wx_union_id'] = '';
$res = curlPost($url,$data);
dump($res);die;
}
/*
*更新用户
*/
public function editUser(){
$url = 'https://api.xiaoe-tech.com/xe.user.info.update/1.0.0';
$data['access_token'] = $this->access_token();
$data['user_id'] = 'u_api_5ea668fa509f2_gojtuemqOz';
$data['data']['update_data']['phone'] = '18858287638';
$res = curlPost($url,$data);
dump($res);die;
}
/*
*获取用户
*/
public function getUser($user_id = ''){
$url = 'https://api.xiaoe-tech.com/xe.user.info.get/1.0.0';
$data['access_token'] = $this->access_token();
$data['user_id'] = $user_id;
//$data['data']['phone'] = '1111111111111';
$data['data']['field_list'] = ['phone','name','nickname','phone_collect'];
$res = curlPost($url,$data);
return empty($res['code']) ? $res['data'] : '';
}
/*
*获取商品详情
*/
public function goodsDetail(){
$url = 'https://api.xiaoe-tech.com/xe.goods.detail.get/3.0.0';
$data['access_token'] = $this->access_token();
$data['data']['goods_id'] = 'p_5ea63e70b24f5_iqAE3OtJ';
$data['data']['goods_type'] = 5;
$res = curlPost($url,$data);
dump($res);die;
}
/*
*获取专栏下的资源列表
*/
public function goodsList(){
$url = 'https://api.xiaoe-tech.com/xe.goods.relation.get/3.0.0';
$data['access_token'] = $this->access_token();
$data['data']['goods_id'] = 'p_5ea63e70b24f5_iqAE3OtJ';
$data['data']['goods_type'] = 5;
$data['data']['page_size'] = 20;
$data['data']['resource_type'] = [1,2,3,4,6,20];
$res = curlPost($url,$data);
dump($res);die;
}
/*
*查询用户订单列表
*/
public function order(){
$url = 'http://api.xiaoe-tech.com/xe.get.user.orders/1.0.0';
$data['access_token'] = $this->access_token();
$data['user_id'] = 'u_5ea91415172b6_XPsESqiKkA';//'u_api_5ea65220ec43e_eQ9qKxSTH9';
$res = curlPost($url,$data);
dump($res);die;
}
/*
*获取订单列表
*/
public function orderList(){
$url = 'https://api.xiaoe-tech.com/xe.order.list.get/1.0.0';
$data['access_token'] = $this->access_token();
$data['app_id'] = $this->shop_id;
//$data['data']['user_id'] = 'u_api_5ea668fa509f2_gojtuemqOz';
$res = curlPost($url,$data);
dump($res);die;
}
/*
*用户购买关系查询
*/
public function payStatus(){
$url = 'http://api.xiaoe-tech.com/xe.product.available/1.0.0';
$data['access_token'] = $this->access_token();
$data['user_id'] = 'u_api_5ea668fa509f2_gojtuemqOz';
$data['data']['product_id'] = 'p_5ea63e70b24f5_iqAE3OtJ';
$data['data']['payment_type'] = 2;
$res = curlPost($url,$data);
dump($res);die;
}
/*
*用户是否拥有某项产品包资源权益
*/
public function payCheck(){
$url = 'https://api.xiaoe-tech.com/xe.user.asset.check/1.0.0';
$data['access_token'] = $this->access_token();
$data['user_id'] = 'u_api_5ea668fa509f2_gojtuemqOz';
$data['app_id'] = $this->shop_id;
$data['asset_type'] = 'goods';
$data['goods_id'] = 'p_5ea63e70b24f5_iqAE3OtJ';
$res = curlPost($url,$data);
dump($res);die;
}
public function access_token(){
$client = new Client(config('redis'));
$client->select(0);
$access_token = $client->get('access_token');
if(!empty($access_token)){
return $access_token;
}else{
$url = 'https://api.xiaoe-tech.com/token?app_id='.$this->shop_id.'&client_id='.$this->client_id.'&secret_key='.$this->client_sercet.'&grant_type='.$this->grant_type;
$data = http_get_data($url);
$data = json_decode($data,true);
$access_token = empty($data['code']) ? $data['data']['access_token'] : '';
if(!empty($access_token)){
$client->setex('access_token',7000,$access_token);
return $access_token;
}
}
}
/*
* 回调
*/
public function notify(){
$response = file_get_contents('php://input');
# $response = '<xml><Encrypt><![CDATA[uGxQgJKKWDlqrHE3on0FxJenDNCBdRe26ljOTRVtfvcE3N5KkdipPgPwCWU4BnVElwnP9ii7H/Eq+8vwVZOQylzbRtYbIBiemZWtSEFErHIa+LeDIYyLs9SSbk39nS4n5xQqJ0PpzGy5aLPfXC9ZxMiYS4si/rY4ifLMIQhXrX8HJCbyDZ4Oh7e6+BZ4zIAfZzygdWghDvyJfHzxN1Oio1EB/fy8SP+qvyZ30/Y0s8Jr6hxxx/dJA0IPxvQ62ySTjN/ZKP02zDwHqfVRUm2cJzGS4Nu3PtxtifzZ4zagkkG1g4E5tXouU132aNZASu8OxZvpD6nPFCc6ZZ1L7y8a8MoA697Mywvs9Xyi0HrGdm/vqyZk5YMrvXiVXHVu47PxWlF5dBJoCoWptQkB3VOvQkBtvHi4l3HyL/GRaTg4CdZQAOTiPjOpLvTVbZ1QMGxUWZptdXPUm/3hOuzyvblj6icmO24tBmo188YrM6J0VocN+k+GorkeUvRk2s2NlxIHYRn0o47H43jmLt6PzrN4EJtfKqWfoGcGRTfENsWjjeh51h6ytVxeGOwzEf3eMsFlfDz0/pV2raj3e/R5GatqWUhsNqIvxMXr5axK8s45W7bBhSYkNYLrCstUlphFsJE9]]></Encrypt><MsgSignature><![CDATA[0d38bbc6d8a21923f7b2775571589e36f7a95e21]]></MsgSignature><TimeStamp>1588147194</TimeStamp><Nonce><![CDATA[8941954052]]></Nonce></xml>';
dlog('xiaoe_notify',$response);
// 小鹅平台发送给第三方平台
$encodingAesKey = $this->key; //加密密钥
$token = $this->token; //加密令牌
$appId = $this->shop_id; //app_id
$xml_tree = new \DOMDocument();
$xml_tree->loadXML($response);
//获取XML中的数据
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$array_m = $xml_tree->getElementsByTagName('MsgSignature');
$array_t = $xml_tree->getElementsByTagName('TimeStamp');
$array_n = $xml_tree->getElementsByTagName('Nonce');
//加密字符串
$encrypt = trim($array_e->item(0)->nodeValue);
//消息体签名
$msg_sign = trim($array_m->item(0)->nodeValue);
//时间戳
$timeStamp = trim($array_t->item(0)->nodeValue);
//随机数
$nonce = trim($array_n->item(0)->nodeValue);
$format = "<xml><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $encrypt);
$msg = '';
$pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
if($errCode == 0){//回调信息解密无误,开始处理业务逻辑
$xml = '<xml>'.$msg.'</xml>';
libxml_disable_entity_loader(true); //禁止引用外部xml实体
$res = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$xiaoeUser = $this->getUser($res['user_id']);//根据小鹅通用户ID获取手机号
$mobile = $xiaoeUser['phone_collect'];//注意这里获取的手机号是采集的phone_collect字段,而不是用户绑定的phone字段
$nickname = $xiaoeUser['nickname'];
$user_id = User::where(['mobile'=>$mobile])->value('id');//根据小鹅通支付用户信息获取己方用户信息
if($res['order_state'] == 1){//支付成功
#支付成功相关逻辑
}
}
}