app微信支付统一下单

php版的

需要注意的地方记录一下

  1. 签名的加密方式为sha256
  2. 返回给客户端的参数全小写,不是驼峰
  3. 返回给客户端调起支付的签名统一下单的加密方式要一样,即sha256
  4. open_id不用传(传了还有可能报错说app_id和open_id不一致)

全部代码 参数记得替换

  public function payFor()
  {
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods:POST,GET");
    header("Access-Control-Allow-Headers:x-requested-with,content-type");
    header("Content-type:text/json;charset=utf-8");

    //正式
    $appid = ''; 
    $secret = '';
    $mch_id = '';//商户号
    $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    $nonce_str = "";
    for($i=0;$i<32;$i++)
     {
         $nonce_str .= $str{mt_rand(0,32)};    //生成php随机数
     }//随机字符串
    
    $timeStamp = strval(time());

    //return $timeStamp;
    $body = '椰子游app钻石充值微信支付';//商品描述
    $out_trade_no = $timeStamp.'mshJSAPI'.$str{mt_rand(0,14)};//商户订单号
    $total_fee = 1;//标价金额

    $spbill_create_ip = '';
    $notify_url = '';//通知地址
    $trade_type = 'APP';//交易类型
    
    $pay_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    
    $map = [
      'appid'=>$appid,
      'mch_id'=>$mch_id,
      'nonce_str'=>$nonce_str,
      'body'=>$body,
      'out_trade_no'=>$out_trade_no,
      'total_fee'=>$total_fee,
      'spbill_create_ip'=>$spbill_create_ip,
      'notify_url'=>$notify_url,
      'trade_type'=>$trade_type,
      'sign_type'=>'HMAC-SHA256',
    ];

    $key = '';
    $sign = $this->sign($map,$key,$type ='sha256');//dump($sign);
    $map['sign'] = $sign;
    
    //$XML = $this->MapConvertXML($map);
    //$arr = array('name' => 'PhalApi');
    $xml = \PhalApi\Tool::arrayToXml($map);
    //return json_encode('<xml><name>PhalApi</name></xml>');
    //return $map;
    $res = '';
    try {
        $curl = new \PhalApi\CUrl(2);

        $res = $curl->post($pay_url, $xml, 6000000);

    } catch (\PhalApi\Exception\InternalServerErrorException $ex) {
      return 'error';
    }

    $bean = \PhalApi\Tool::xmlToArray($res);
    //return $bean;
    $returnarray = [
      'appid'=>$appid,
      'partnerid'=>$mch_id,
      'prepayid'=>$bean['prepay_id'],
      'noncestr'=>$nonce_str,
      'timestamp'=>(int)$timeStamp,
      'package'=>'Sign=WXPay',
    ];
    $sign = $this->sign($returnarray,$key,$type ='sha256');//dump($sign);

    $returnarray['sign'] = $sign;
    //return 1;
    return $returnarray;
  }
发布了38 篇原创文章 · 获赞 7 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览