微信小程序如何使用微信支付、小程序微信支付案例

2021-08-22   阅读:1911   分类:后端    标签: 微信小程序

前提条件:

1、企业微信小程序、并接入微信支付

2、登录小程序关联商户号

3、开通微信支付商户

开通地址:https://pay.weixin.qq.com/

实现步骤:

一、小程序关联微信支付商户号

1、 登录小程序后台,点击功能->微信支付->关联更多商户号

2、点击之后,会跳转到微信支付商户平台,

https://pay.weixin.qq.com/index.php/extend/merchant_appid/mapay_platform/account_manage

没登录微信支付商户平台的可以先登录,接着点击产品中心-》点击左侧的AppID账号管理-》点击右侧的关联AppID按钮

3、会跳到新增授权页面,我们输入我们小程序的appid,勾选阅读并同意协议

4、 提交之后微信商户平台就会向小程序发出授权申请,在小程序后台,点击确认即可

5、下载证书和设置秘钥

二、ThinkPHP6小程序使用微信支付v3版本

方式一:在项目目录中,通过composer命令行添加:

composer require wechatpay/wechatpay-guzzle-middleware

方式二:在项目的composer.json中加入以下配置:

   "require": {
       "wechatpay/wechatpay-guzzle-middleware": "^0.2.0"
   }

添加配置后,执行安装

composer install

1、 微信部分支付代码

<?php
use GuzzleHttp\Exception\RequestException;
use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
use WechatPay\GuzzleMiddleware\Util\PemUtil;
//微信支付
public function config()
{
      // 商户相关配置
      $merchantId = ''; // 商户号
      $merchantSerialNumber = ''; // 商户API证书序列号
      $filepath = "uploads/attach/2020/12/20201202/afba9ad6b68a66c144a36d9abbb8c52b.pem"; //私钥在本地的位置
      $file = file_get_contents($filepath);
      $mch_private_key = openssl_get_privatekey($file);
 
      $appid = ''; //小程序appid
      $out_trade_no = date('YmdHis', time()) . rand(1000, 9999);
      
      $data = [
             "appid" => $appid,
             "mchid" => $merchantId,
             "description" => '标题',
             'out_trade_no' => $out_trade_no,
             'notify_url' => '', //回调地址
             "amount" => [
                    "total" => 1,
                    "currency" => "CNY"
             ],
             "payer" => [
                    "openid" => "" //用户openid
             ]
      ];
 
      $timestamp = time();
      $nonce = date('YmdHis', time()) . rand(1000, 9999);
      $url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi';
      $url_parts = parse_url($url);
      $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
 
      $data = json_encode($data);
      $message = 'POST' . "\n" .
             $canonical_url . "\n" .
             $timestamp . "\n" .
             $nonce . "\n" .
             $data . "\n";
 
      openssl_sign($message, $signature, $mch_private_key, "sha256WithRSAEncryption");
      $sign = base64_encode($signature);
      $schema = 'WECHATPAY2-SHA256-RSA2048';
      $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $merchantId, $nonce, $timestamp, $merchantSerialNumber, $sign);
 
      $header = "Authorization: " . $schema . " " . $token;
 
      $res = $this->http_post($url, $header, $data);
      $arr = json_decode($res, true);
 
      $time = time();
      $str = time() . round('1000', '9999');
      $prepay = 'prepay_id=' . $arr['prepay_id'];
 
      $message1 = $appid . "\n" .
             $time . "\n" .
             $str . "\n" .
             $prepay . "\n";
 
      openssl_sign($message1, $signature, $mch_private_key, "sha256WithRSAEncryption");
      $sign1 = base64_encode($signature);
 
 
      $data = array();
      $data['timeStamp'] = $time;
      $data['nonce'] = $str;
      $data['prepay_id'] = $arr['prepay_id'];
      $data['sign'] = $sign1;
 
      return app('json')->status('', $data);
}
 
function http_post($url, $header, $data)
{
      $headers[] = "Accept:application/json";
      $headers[] = "Content-Type:application/json";
      $headers[] = "User-Agent:application/json";
      $headers[] = $header;
 
      $curl = curl_init(); // 启动一个CURL会话
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_HEADER, 0);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      $tmpInfo = curl_exec($curl);
      //关闭URL请求
      curl_close($curl);
      return $tmpInfo;
}

2、 微信支付回调

//支付回调
public function sharenotify()
{
      $post = input('');
      $key = '';//商户平台设置的api v3 密码
      $text = base64_decode($post['resource']['ciphertext']);
      $str = sodium_crypto_aead_aes256gcm_decrypt($text,$post['resource']['associated_data'],$post['resource']['nonce'],$key);
      $res = json_decode($str, true);
 
      if($res['trade_state'] == 'SUCCESS'){
             //成功操作
      }
}
【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/796.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×