1,申请流程
见其他文档
微信小程序支付也可以使用(JSAPI支付),不过小程序需要绑定微信公众号
https://www.cnblogs.com/openWorld/p/11041615.html
2,微信配置
说明:
APPID
微信小程序的APPID
MCHID
商户号
KEY
商户支付密钥
APPSECRET
微信小程序的秘钥
3,商户支付密钥生成
密钥由自己生成,推荐使用在线密码生成器。(32位字符串)
4,SDK开发者后台接入
在开始后台代码前,我们必须有4个参数,小程序APPID
、商户号
、商户支付密钥
、小程序的秘钥
。 没有这4个参数,就没法唤起微信支付接口!!!
准备好后可以下载微信的官方demo代码参考(JSAPI、APP、小程序)。https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
5,核心步骤代码(小程序支付)
说明:支付
//微信小程序支付接口
public function pay($body, $out_trade_no, $total_fee, $openid)
{
//统一下单接口
$unifiedorder = $this->unifiedorder($openid, $body, $out_trade_no, $total_fee);
$parameters = array(
'appId' => self::APPID, //小程序ID
'timeStamp' => '' . time() . '', //时间戳
'nonceStr' => $this->createNoncestr(), //随机串
'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包
'signType' => 'MD5'//签名方式
);
//签名
$parameters['paySign'] = $this->getSign($parameters);
//return $parameters;
return $this->json_success([$parameters]);
}
说明:统一下单接口
private function unifiedorder($openid, $body, $out_trade_no, $total_fee)
{
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$parameters = array(
'appid' => self::APPID, //小程序ID
'mch_id' => self::MCHID, //商户号
'nonce_str' => $this->createNoncestr(), //随机字符串
'body' => $body, //商品描述
'out_trade_no' => $out_trade_no, //商户订单号
'total_fee' => floatval($total_fee * 100), //总金额 单位 分
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], //终端IP
//'spbill_create_ip' => '192.168.0.161', //终端IP
'notify_url' => 'https://solitaire.laoma-app.com/api/Pay/notify.html', //通知地址 确保外网能正常访问
'openid' => $openid, //用户id
'trade_type' => 'JSAPI'//交易类型
);
//统一下单签名
$parameters['sign'] = $this->getSign($parameters);
$xmlData = $this->arrayToXml($parameters);
$return = $this->xmlToArray($this->postXmlCurl($xmlData, $url, 60));
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 = "";
foreach ($arr as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . arrayToXml($val) . "";
} else {
$xml .= "<" . $key . ">" . $val . "";
}
}
$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 getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//签名步骤二:在string后加入KEY
$String = $String . "&key=" . self::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;
}
说明:将xml转为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;
}
说明:异步回调方法
public function notify()
{
/* $postXml = '
1
';*/
$postXml = file_get_contents("php://input",'r'); //接收微信参数
//写入日志
file_put_contents('pay.txt',$postXml,FILE_APPEND);//写入txt支付宝的返回数据
// 没有数据
if (empty($postXml)) {
return false;
}
//将服务器返回的XML数据转化为数组
$data = $this->toArray($postXml);
//保存微信服务器返回的签名sign, 这里没有做签名验证
//$dataSign = $data['sign'];
//支付成功时
if (($data['return_code']=='SUCCESS') && ($data['result_code']=='SUCCESS')){
$orderNum = $data['out_trade_no'];
// 查询数据库订单
$orderModel = new OrderModel();
$orderInfo = $orderModel->where(['order_num'=>$orderNum])->find();
//判断数据库的订单状态是否已经完成
if ($orderInfo && $orderInfo['order_status'] == 0) {
//判断价格是否正确
if (($data['total_fee'] == $orderInfo['order_money']*100)){
/*if (($data['total_fee'] == $orderInfo['price']*100)){*/
//开始业务逻辑
//修改订单状态
$res = $orderModel->where('order_id', $orderInfo['order_id'])->update([
'pay_order_num' => $data['transaction_id'],
'order_status' => 1,
]);
//修改接龙状态
$detailModel = new DetailModel();
$res2 = $detailModel->where('detail_id', $orderInfo['detail_id'])->update([
'pay_status' => 1
]);
if ($res){
$xml = $this->arrayToXml([
'return_code' => 'SUCCESS',
'return_msg' => 'OK',
]);
return $xml;
}
} else {
echo '订单金额异常';
}
}
}
}
6,参考连接(小程序支付)
https://blog.csdn.net/cwh1010714845/article/details/78944335
https://qq52o.me/1659.html
最全:https://www.cnblogs.com/zerofc/p/9065086.html
案例见百度云