zoukankan      html  css  js  c++  java
  • php 微信支付分开发 免押

    写在前面

    先说一下这个版本 是V1.3的, 2020.03.15日更新版本   之前曾经做过一个支付分V1.0的。 v1.0和v1.3已经是大不一样了  ,所以如果你在做这个话看好版本,别让我带跑偏。

    前期准备

    看到这篇文章,相信你已经做好了前期的准备工作,包括已经开通了微信支付分 ,向腾讯的商务提交了相关的文档 获得了 属于你自己的微信支付 service_id 

    代码编写

    微信支付分提供了比较详尽的文档  这次开发 也要接触到他 api-v3 接口规则

    上个官方文档接口

         微信支付分文档地址:   https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/payscore/chapter1_1.shtml

          api-v3规则文档:           https://wechatpay-api.gitbook.io/wechatpay-api-v3/  

    当然了这个地址可能后面会有变更  那你就去百度找一下吧

       首先咱们最重要的是创建微信支付分订单   不说了 直接上代码

     1 public function zhiFen($order0no='', $ya_money=2)
     2     {
     3         $url = "https://api.mch.weixin.qq.com/v3/payscore/serviceorder";
     4         $json = [ // JSON请求体
     5             'out_order_no' => $order0no,//订单号
     6             'appid' => '',
     7             'service_id' => '',
     8             'service_introduction' => '陪护宝使用租金',
     9             'time_range' => ['start_time' => 'OnAccept'],
    10             'risk_fund' => ['name' => 'DEPOSIT', 'amount' => 2],
    11             'notify_url' => url('index', '', false, true),
    12             'need_user_confirm' => true,
    13         ];
    14 
    15         $arr_header[] = "Content-Type: application/json";
    16         $arr_header[] = "Accept: application/json";
    17         $arr_header[] = "User-Agent: " . $_SERVER['HTTP_USER_AGENT'];
    18 
    19         $http_method = 'POST';
    20         $timestamp = time();
    21         $nonce = $this->str_rand(32);
    22         $body = json_encode($json, true);
    23         $merchant_id = '';  //商户号
    24         $serial_no = '';       //这个是证书号  这个必须使用新版证书
    25 
    26         $url_parts = parse_url($url);
    27         $path = APP_PATH . 'cert/';
    28         $mch_private_key = PemUtil::loadPrivateKey($path . 'apiclient_key.pem');
    29 
    30         $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
    31         $message = $http_method . "
    " .
    32             $canonical_url . "
    " .
    33             $timestamp . "
    " .
    34             $nonce . "
    " .
    35             $body . "
    ";
    36 
    37         openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
    38         $sign = base64_encode($raw_sign);
    39 
    40         $schema = 'WECHATPAY2-SHA256-RSA2048 ';
    41         $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
    42             $merchant_id, $nonce, $timestamp, $serial_no, $sign);
    43         //这个是生成认证参数  并加到请求头中
    44         $arr_header[] = "Authorization:" . $schema . $token;
    45         $res = $this->https_request($url, json_encode($json), $arr_header);
    46         $res_arr = json_decode($res, true);
    47         $sign_type = "HMAC-SHA256";//HMAC-SHA256签名方式
    48         $key = "hHezylOkgatMqYKrVeQthR4qfGvyOebD";
    49         $res_arr['timestamp'] = $timestamp;
    50         $res_arr['nonce_str'] = $nonce;
    51         $res_arr['sign_type'] = $sign_type;
    52 
    53         $res_arr['sign'] = bc_sign([
    54             'mch_id'=>$merchant_id,
    55             'package'=>$res_arr['package'],
    56             'timestamp'=>$timestamp,
    57             'nonce_str'=>$nonce,
    58             'sign_type'=>$sign_type,
    59         ],$key);
    60         $this->success('', $res_arr);
    61     }
    62 
    63     //生成随机字符串
    64     public function str_rand($length = 32, $char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
    65     {
    66         if (!is_int($length) || $length < 0) {
    67             return false;
    68         }
    69         $string = '';
    70         for ($i = $length; $i > 0; $i--) {
    71             $string .= $char[mt_rand(0, strlen($char) - 1)];
    72         }
    73 
    74         return $string;
    75     }
    76 
    77     //请求方法    
    78     private function https_request($url, $data = null, $arr_header = [])
    79     {
    80         $curl = curl_init();
    81         //curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false);
    82         curl_setopt($curl, CURLOPT_URL, $url);
    83         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    84         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    85         if (!empty ($data)) {
    86             curl_setopt($curl, CURLOPT_POST, 1);
    87             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    88         }
    89 
    90         if (!empty($arr_header)) {
    91             curl_setopt($curl, CURLOPT_HTTPHEADER, $arr_header);
    92         }
    93 
    94         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    95         $output = curl_exec($curl);
    96         curl_close($curl);
    97         return $output;
    98     }
    99     

              这个就是这个的请求方法   生成签名的这个应该封装一下   这里为了更好的展现  我就没有封装  ,  其实微信官方提供了一个composer封装的方法  但是我么有用   用的这种简单粗暴的方法 

    通过这个请求就可以获取到微信返回值.

       但是这个还没有完这个还需要你给前端加密的值传过去 .(今天就写到这里了  未完待续 有问题可以联系57044350@qq。com )

       

  • 相关阅读:
    iOS No suitable application records were found. Verify your bundle identifier 'xxx' is correct.
    隐私安全加密
    python-标示符和关键字
    Xcode打包提交至itunes connect后,提交审核成功,随后出现二进制文件无效
    linking against a dylib which is not safe for use in application extensions
    CH收藏的书
    颜色管理
    子线程回到主线程
    python- if-else
    安卓手机传递文件到Windows系统电脑
  • 原文地址:https://www.cnblogs.com/sgj123/p/13032783.html
Copyright © 2011-2022 走看看