官方文档
产品能力概览-文档中心-微信支付商户平台https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml这里主要说一下小程序的支付,在官方文档中,有php的demo可以参考。
话不多说。直接贴代码
Controller:
/**
* 支付
*/
public function doPay()
{
if (request()->isPost()) {
$list = request()->post('list');
$params = [
'out_trade_no' => 'SDK-KH-' . orderNumber(6), // 订单编号
'description' => $list['description'], // 商品描述
'total' => intval($list['total']), // 付款金额
'trade_type' => $list['trade_type'], // 交易类型
'openid' => $list['openid'], // openid
];
if ($params['defray_type'] == 1) {
// 微信支付
$result = $this->wxPay->createOrder($params);
} else {
// 支付宝支付
$result = $this->alipay->createOrder($params);
}
$this->_error($result['code'], $result['msg'], $result['data']);
}
}
/**
* 微信支付回调通知
*/
public function wxPayNotity()
{
$headers = [
'signature' => request()->header('Wechatpay-Signature'),
'timestamp' => request()->header('Wechatpay-Timestamp'),
'nonceStr' => request()->header('Wechatpay-Nonce'),
'serial' => request()->header('Wechatpay-Serial'),
];
$body = file_get_contents("php://input");
if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
log4j('当前PHP环境不支持SHA256withRSA');
}
// 签名验证
$signRes = $this->wxPay->verifySign($headers, $body);
// 订单操作
$result = $this->wxPay->wxPayNotify($body);
if ($result['code']) {
log4j('微信支付通知回调 ', $result['msg']);
exit(1);
}
return sprintf("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
}
Service:
class WxPayService
{
protected $client;
protected $headers;
protected $mchid; // 商户号
protected $appid; // 小程序id
protected $h5_appid; // 公众号appid
protected $h5_secret; // 公众号密钥
protected $app_secret; // 小程序密钥
protected $mobile_appid; // 移动应用appid
protected $serial_number; // 商户API证书序列号
protected $api_v3_key; // V3秘钥
protected $private_key; // 商户私钥文件路径
protected $public_key; // 微信支付平台证书文件路径
protected $wx_public_serial; // 微信公钥证书序列号
protected $wx_public_key; // 微信公钥证书地址
protected $notify_url; // 支付回调通知地址
protected $ordersModel;
public function __construct(Payment $config, Orders $orders)
{
$this->headers = ['Accept' => 'application/json'];
$this->appid = $config->appid;
$this->h5_appid = $config->h5_appid;
$this->app_secret = $config->app_secret;
$this->mchid = $config->merchant_id;
$this->serial_number = $config->serial_number;