共计 6722 个字符,预计需要花费 17 分钟才能阅读完成。
前言:
这里我就伪装你曾经注册了微信小程序,并且根本的配置都曾经好了。注: 集体注册小程序不反对微信领取,所以我还是伪装你是企业或者个体工商户的微信小程序,其余的商户号注册,二者绑定,受权,领取开明,就浏览文档吧,这里我先负责实战。
根本流程:
- 申请商户平台账号
- 微信小程序绑定已有商户号并开明微信领取
- 登录商户平台对小程序受权,下载领取证书,记录商户号,领取密钥。
- 浏览微信领取官网文档,实现接口的对接编码。
开发领取流程:
- 微信小程序的根本配置。(app_id[ 小程序惟一 id],mch_id[商户号],md5_key[领取密钥],notify_url[异步回调告诉] )。
- 按微信要求的程序将参数组成键值对数组,并对其进行签名 (先将参数进行字段排序,参数能够解决中文字符,在申请参数字符串后拼上领取密钥,最初 md5,签名实现)
- 所有申请参数和签名一起组成新数组,再转为 XML。
- 以 XML 格局参数,POST 申请形式发动对立下单申请。
- 微信服务器接管下单申请,返回预领取 ID(prepay_id) 到本人服务端。
- 本人服务端联结预领取 ID,小程序 APPID,32 位随机串,工夫戳,签名形式一并返回到小程序。
- 小程序依据微信提供的函数和返回的参数集调起微信领取。
- 领取实现,微信通过异步告诉到本人服务指定的控制器。
- 承受微信返回的告诉,将 XML 转为数组,须要先判断告诉过去的是不是同一个订单 (依据订单号),因为有时微信异步告诉,本人服务器未接管解决,他会过一段时间反复发动告诉。
-
依据告诉状态,更新本人业务的数据表,最初返回一个胜利标识的 XML 给微信服务器。
一、领取配置
'wxxcx' =>[ | |
'app_id' => 'wx4c0e*******664b4', // 微信小程序 appid | |
'mch_id' => '149***3342', // 微信商户 id | |
'md5_key' => '3FN8WHO**********iPnNoJ576AxMmwQ', // 微信领取密钥 | |
'app_cert_pem' => APP_PATH.'v1/wechat_cert/apiclient_cert.pem', // 领取证书,对立下单不需,退款等其余接口须要 | |
'app_key_pem' => APP_PATH.'v1/wechat_cert/apiclient_key.pem', | |
'sign_type' => 'MD5',// MD5 HMAC-SHA256 | |
'limit_pay' => [ ], | |
'fee_type' => 'CNY',// 货币类型 以后仅反对该字段 | |
'notify_url' => 'https://***********.com/v1/Pay/notifyUrlApi', // 异步告诉地址 | |
'redirect_url' => '','return_raw' => false, | |
] |
二、前端传来的参数或服务端生成
$this->openid = $openid; // 前端也可不传 $this->out_trade_no = $out_trade_no; // 服务端生成 $this->body = $body;$this->total_fee = $total_fee; // 最好服务端数据库抓取,防止前端传 $this->spbill_create_ip = $spbill_create_ip; // 申请的 ip 地址
三、封装对立下单类
<?php | |
/** | |
* @createTime: 2018-04-30 18:02 | |
* @description: 小程序微信领取 | |
* 公众号:ZERO 开发 | |
*/ | |
namespace app\v1\extend; | |
class WeixinPay { | |
protected $appid; | |
protected $mch_id; | |
protected $key; | |
protected $openid; | |
protected $out_trade_no; | |
protected $body; | |
protected $total_fee; | |
protected $notify_url; | |
protected $spbill_create_ip; | |
function __construct($appid, $openid, $mch_id, $key,$out_trade_no,$body,$total_fee,$notify_url,$spbill_create_ip) { | |
$this->appid = $appid; | |
$this->openid = $openid; | |
$this->mch_id = $mch_id; | |
$this->key = $key; | |
$this->out_trade_no = $out_trade_no; | |
$this->body = $body; | |
$this->total_fee = $total_fee; | |
$this->notify_url = $notify_url; | |
$this->spbill_create_ip = $spbill_create_ip; | |
} | |
/************ 测试方法可删除 *****************/ | |
public function test() { | |
$ha = "hello world"; | |
return $this->appid; | |
} | |
/************ 可删除 *****************/ | |
public function pay() {// var_dump($this->notify_url); | |
// die; | |
// 对立下单接口 | |
$return = $this->weixinapp(); | |
return $return; | |
} | |
// 对立下单接口 | |
private function unifiedorder() { | |
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; | |
// 这里的参数程序肯定要按上面的,不然可能就始终报商户号此性能未受权等谬误 | |
$parameters = array( | |
'appid' => $this->appid, // 小程序 ID | |
//'body' => 'test', // 商品形容 | |
'body' => $this->body, | |
'mch_id' => $this->mch_id, // 商户号 | |
'nonce_str' => $this->createNoncestr(), // 随机字符串 | |
'notify_url' => $this->notify_url, //'https://shop.gdpress.cn/syw_jingzhun/index.php/Api/xiaochengxu/notify_url_api', // 告诉地址 确保外网能失常拜访 | |
'openid' => $this->openid, // 用户 id | |
// 'out_trade_no' => '2015450806125348', // 商户订单号 | |
'out_trade_no'=> $this->out_trade_no, | |
//'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], // 终端 IP | |
'spbill_create_ip' => $this->spbill_create_ip, // 终端 IP | |
'total_fee' => floatval(($this->total_fee) * 100), // 单位 分 | |
//'total_fee' => $this->total_fee, // 单位 分 | |
'trade_type' => 'JSAPI' // 交易类型 | |
); | |
// 对立下单签名 | |
$parameters['sign'] = $this->getSign($parameters); | |
$xmlData = $this->arrayToXml($parameters); | |
$return = $this->xmlToArray($this->postXmlCurl($xmlData, $url, 60)); | |
//$return = $this->postXmlCurl($xmlData, $url, 60); | |
// print_r($return); | |
// die; | |
return $return; | |
} | |
// curl 申请办法封装 | |
private static function postXmlCurl($xml, $url, $second = 30) | |
{$ch = curl_init(); | |
// 设置超时 | |
curl_setopt($ch, CURLOPT_TIMEOUT, $second); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 严格校验 | |
// 设置 header | |
curl_setopt($ch, CURLOPT_HEADER, FALSE); | |
// 要求后果为字符串且输入到屏幕上 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
//post 提交形式 | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 40); | |
set_time_limit(0); | |
// 运行 curl | |
$data = curl_exec($ch); | |
// 返回后果 | |
if ($data) {curl_close($ch); | |
return $data; | |
} else {$error = curl_errno($ch); | |
curl_close($ch); | |
throw new WxPayException("curl 出错,错误码:$error"); | |
} | |
} | |
// 数组转换成 xml | |
private function arrayToXml($arr) { | |
$xml = "<xml>"; | |
foreach ($arr as $key => $val) {if (is_array($val)) {$xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">"; | |
} else {$xml .= "<" . $key . ">" . $val . "</" . $key . ">";} | |
} | |
$xml .= "</xml>"; | |
return $xml; | |
} | |
//xml 转换成数组 | |
private function xmlToArray($xml) { | |
// 禁止援用内部 xml 实体 | |
libxml_disable_entity_loader(true); | |
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); | |
$val = json_decode(json_encode($xmlstring), true); | |
return $val; | |
} | |
// 微信小程序接口 | |
private function weixinapp() { | |
// 对立下单接口 | |
$unifiedorder = $this->unifiedorder(); | |
// 对立下单出错,参数出错等起因 | |
if($unifiedorder['return_code'] == 'FAIL') {$retrunInfo['code'] = 0; | |
$retrunInfo['msg'] = $unifiedorder['return_msg']; | |
return $retrunInfo; | |
} | |
$parameters = array( | |
'appId' => $this->appid, // 小程序 ID | |
'timeStamp' => ''. time() .'', // 工夫戳 | |
'nonceStr' => $this->createNoncestr(), // 随机串 | |
'package' => 'prepay_id=' . $unifiedorder['prepay_id'], // 数据包 | |
'signType' => 'MD5' // 签名形式 | |
); | |
// 小程序发动领取签名 | |
$parameters['paySign'] = $this->getSign($parameters); | |
// 胜利返回 | |
$retrunInfo['code'] = 1; | |
$retrunInfo['msg'] = $parameters; | |
return $retrunInfo; | |
} | |
// 作用:产生随机字符串,不长于 32 位 | |
private function createNoncestr($length = 32) { | |
$chars = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
$str = ""; | |
for ($i = 0; $i < $length; $i++) {$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); | |
} | |
return $str; | |
} | |
// 作用:生成签名 | |
private function getSign($Obj) {foreach ($Obj as $k => $v) {$Parameters[$k] = $v; | |
} | |
// 签名步骤一:按字典序排序参数 | |
ksort($Parameters); | |
$String = $this->formatBizQueryParaMap($Parameters, false); | |
// 签名步骤二:在 string 后退出 KEY | |
$String = $String . "&key=" . $this->key; | |
// 签名步骤三:MD5 加密 | |
$String = md5($String); | |
// 签名步骤四:所有字符转为大写 | |
$result_ = strtoupper($String); | |
return $result_; | |
} | |
// 作用:格式化参数,签名过程须要应用 | |
private function formatBizQueryParaMap($paraMap, $urlencode) { | |
$buff = ""; | |
ksort($paraMap); | |
foreach ($paraMap as $k => $v) {if ($urlencode) {$v = urlencode($v); | |
} | |
$buff .= $k . "=" . $v . "&"; | |
} | |
$reqPar; | |
if (strlen($buff) > 0) {$reqPar = substr($buff, 0, strlen($buff) - 1); | |
} | |
return $reqPar; | |
} | |
} |
四、发动申请接口的业务代码
/** | |
* ** | |
* date: 2018-04-30 | |
* desc: 这里开始对立下单领取 | |
*/ | |
$wxxcx_config = config('pay.wxxcx'); // 微信小程序设置 | |
$appid = $wxxcx_config['app_id']; // 小程序 id | |
$mch_id = $wxxcx_config['mch_id']; // 领取商户 id | |
$key = $wxxcx_config['md5_key']; // 商户的领取密钥 | |
$notify_url = $wxxcx_config['notify_url']; // 微信服务器异步告诉 | |
$spbill_create_ip = $_SERVER['REMOTE_ADDR']; // 客户端 ip | |
$openid = $Xcxopenid; // 用户 openid | |
$out_trade_no = $Orderno; // 订单编号 | |
$body = $params['body']; // 订单形容 | |
$total_fee = $Alltotal; // 领取金额 | |
// 实例微信领取基类 | |
$weixinPay = new WeixinPay($appid, $openid, $mch_id, $key,$out_trade_no,$body,$total_fee,$notify_url,$spbill_create_ip); | |
// 发动微信领取 | |
$result = $weixinPay->pay(); | |
if($result['code'] == 0) { // 对立下单出错 | |
return $this->sendError(1, $result['msg'], 200); | |
} | |
// 获取预领取返回参胜利 | |
return $this->sendSuccess($result, 'success', 200); |
五、异步告诉
六、其余辅助办法
/** | |
* 将 xml 转为 array | |
* @param string $xml xml 字符串 | |
* @return array 转换失去的数组 | |
*/ | |
public function toArray($xml) { | |
// 禁止援用内部 xml 实体 | |
libxml_disable_entity_loader(true); | |
$result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); | |
return $result; | |
} |
正文完