前提条件:
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'){
             //成功操作
      }
}
                    
                    
                                
                                
                        
                        
                        
                        
                        
                        
                        
                        
                
                
                        
                        
                        
                        
关于简忆
简忆诞生的故事
            


粤ICP备16092285号
文章评论(0)