赞
踩
本文讲解如何开发微信小程序支付,包含小程序发起支付,后端统一下单,查询订单,订单回调,订单入库等操作。
微信小程序获取订单参数->向后端发起同意下单请求->获取订单参数->小程序调用Api进行发起支付->支付完成->发送回调->支付结果入库->查询订单支付状态。
getOpenid.php
需要配置的参数有 $appid、$secret、orderPrice
,其中 $appid、$secret、orderPrice
是你小程序的两个参数,orderPrice
是订单金额,以元为单位。
<?php
// 页面编码
header("content-type:application/json");
// 获得小程序传过来的CODE
$code = trim($_GET['code']);
// 小程序appid
$appid = "这里填写你的";
// 小程序appscret
$secret = "这里填写你的";
// 授权登录api接口
$api = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
// 发起请求
$result = file_get_contents($api);
// 获取openid
$arr_result = json_decode($result, true);
$openid = $arr_result["openid"];
// 返回信息
$result = array(
'code' => 200,
'msg' => '获取openid成功',
'openid' => $openid,
'orderPrice' => 0.01 // 单位:元
);
// 输出JSON
echo json_encode($result,JSON_UNESCAPED_UNICODE);
?>
creatOrder.php
需要配置的参数都在代码中有说明。$appid、$mchid、$xlid、$data['notify_url']、$set_body、$price
<?php
// 页面编码
header('Content-type:text/html; Charset=utf-8');
ini_set('date.timezone','Asia/Shanghai');
// 统一下单
function wechartAddOrder($name,$ordernumber,$money,$openid,$timeStamp,$noncestr){
$url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
$urlarr = parse_url($url);
// 需要配置以下参数
// 需要配置以下参数
// 需要配置以下参数
// 需要配置以下参数
// 需要配置以下参数
$appid = '填写你的小程序appid'; // appID
$mchid = '填写你的微信支付商户号'; // 商户ID
$xlid = '填写你的秘钥序列号'; // 秘钥序列号 可在这个网址中查询 https://myssl.com/cert_decode.html
$data = array();
$time = $timeStamp;
$data['appid'] = $appid;
$data['mchid'] = $mchid;
$data['description'] = $name; // 商品描述
$data['out_trade_no'] = $ordernumber; // 订单编号
// 异步订单回调线上地址
// 就是notify.php这个文件的线上URL
// 例如你的notify.php所在服务器的位置是wwwroot/xcxpay/notify.php
// 你的域名是https://www.qq.com
// 那么应该按照这个格式填写URL:https://www.qq.com/xcxpay/notify.php
$data['notify_url'] = "填写notify.php这个文件的线上URL";
$data['amount']['total'] = intval($money * 1); // 金额(单位:分)
$data['payer']['openid'] = $openid; // 用户openID
$data = json_encode($data);
$key = getSign($data,$urlarr['path'],$noncestr,$time); //
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。