小程序支付 和 回调处理
发布日期:2021-05-13 20:53:21 浏览次数:18 分类:精选文章

本文共 7818 字,大约阅读时间需要 26 分钟。

������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


���������������������������������

  • ���������

    ���������������������������������������������������������������������������������������������������������������ID���prepay_id���������������������������������������������������������������������������������������������������������������������������������

    • ���������������������������������������
    • ������ID���������������������������������������
    • ������������������������������trade_type���JSAPI���������������������������������������������������������
  • ���������������������

    ������������������������������������prepay_id������������������������������������������������������������������������������������������������������������������������������

    • appId������������������ID���
    • package������������ID���
    • timeStamp���������������������
    • nonceStr������������������������
    • signType������������������
    • paySign������������������
  • ������������������������

    ������������������������������wx.requestPayment���������������������������������������������������������������������������������������������

    • timeStamp������������������������������
    • nonceStr������������������������
    • package������������������ID���������������
    • signType���������������������������MD5������
    • paySign���������������
  • ������������

    ������������������������������URL���������notify_url���������������������������������������������������������������������������������������������������������

    • ������������������������������
    • ������������������������������������������������
    • ���������������������������������������

  • ������������������������

    ���������������������������������������������������������������������

    static $wxpayconf = [
    'wx_smallprogram_appid' => 'wx13212312313123', // ���������������ID
    'mch_id' => '12312345454', // ���������������������������
    'key' => 'Gjanuarygaoqingsong', // ������
    'appsecret' => '1231231212daseqe123213', // ���������������������������
    'notify_url' => 'http://www.gaoqingsong.com/index.php?' // ������������������
    ];

    ���������������������������������

    1. ���������������������

    public function unifiedorder($paydata, $orderSn) {
    // ������������������������������
    $wxpayconf = self::$wxpayconf;
    // ������������IP������
    $userIP = $_SERVER['REMOTE_ADDR'];
    $sign = [
    'appid' => $wxpayconf['wx_smallprogram_appid'],
    'mch_id' => $wxpayconf['mch_id'],
    'nonce_str' => $this->nonceStr(),
    'body' => 'Gjanury������������',
    'out_trade_no' => $orderSn,
    'total_fee' => 1,
    'spbill_create_ip' => $userIP,
    'trade_type' => 'JSAPI',
    'openid' => $paydata['openid'],
    'notify_url' => $wxpayconf['notify_url']
    ];
    // ������������������
    $sign['sign'] = $this->getSign($sign, $wxpayconf['key']);
    // ������������������������
    $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
    $data = $this->arrayToXml($sign);
    $res = $this->postXmlCurl($data, $url);
    if ($res['errNum'] != 0) {
    echo json_encode($res);
    die;
    } else {
    $res = $res['info'];
    if ($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS') {
    $prepay_id = $res['prepay_id'];
    // ������������������������������
    $yuzhifudata = [
    'appId' => $wxpayconf['wx_smallprogram_appid'],
    'package' => 'prepay_id='.$prepay_id,
    'timeStamp' => (string)time(),
    'nonceStr' => $this->nonceStr(),
    'signType' => 'MD5',
    'paySign' => $this->getSign($yuzhifudata, $wxpayconf['key'])
    ];
    exit(json_encode($yuzhifudata));
    } else {
    // ������������������������
    if ($res['return_code'] == 'FAIL') {
    $info['return_msg'] = $res['return_msg'];
    }
    if ($res['result_code'] == 'FAIL') {
    $info['err_code'] = $res['err_code'];
    $info['err_code_des'] = $res['err_code_des'];
    }
    exit($info);
    }
    }
    }

    2. ������������������

    private function getSign($params, $key) {
    ksort($params);
    $singstring = '';
    foreach ($params as $key => $value) {
    $singstring .= '&'.$key . '=' . $value;
    }
    $string = $singstring . "&key=" . $key;
    $string = ltrim($string, '&');
    $string = md5($string);
    $result = strtoupper($string);
    return $result;
    }

    3. XML������������

    public function arrayToXml($arr, $is_array = false) {
    if (!$is_array) {
    $xml = '
    ';
    }
    foreach ($arr as $key => $val) {
    if (is_array($val)) {
    $xml .= "<".$key.">".($this->arrayToXml($val, true))+"<";
    } else {
    $xml .= "<".$key.">".$val."<";
    }
    }
    if (!$is_array) {
    $xml .= "
    ";
    }
    return $xml;
    }

    ������������������������������

    wx.request({
    url: 'http://www.gaoqingsong.com/index.php?m=xxxx&a=payment',
    data: {
    tablenumber: tablenumber,
    orderdetail: JSON.stringify(that.data.goods),
    amount: that.data.totalprice,
    actualpayment: that.data.totalprice,
    openid: useropenid,
    wxheadimg: wxinfo.avatarUrl,
    wxname: wxinfo.nickName
    },
    method: "POST",
    header: {
    "content-type": "application/x-www-form-urlencoded"
    },
    success: function (res) {
    wx.navigateTo({
    url: '../paysuccess/paysuccess'
    });
    var timeStamp = res.data.timeStamp;
    wx.requestPayment({
    timeStamp: timeStamp,
    nonceStr: res.data.nonceStr,
    package: res.data.package,
    signType: res.data.signType,
    paySign: res.data.paySign,
    success: function (res) {
    if (res.errMsg == 'requestPayment:ok') {
    wx.navigateTo({
    url: '../paysuccess/paysuccess'
    });
    }
    },
    fail: function (res) {
    console.log(res);
    },
    complete: function (res) {
    }
    });
    }
    });

    ������������������������

    $receipt = $_REQUEST;
    if ($receipt == null) {
    $receipt = file_get_contents("php://input");
    }
    if ($receipt == null) {
    $receipt = $GLOBALS['HTTP_RAW_POST_DATA'];
    }
    $post_data = $this->xml_to_array($receipt);
    $postSign = $post_data['sign'];
    unset($post_data['sign']);
    ksort($post_data);
    $str = $this->ToUrlParams($post_data);
    $user_sign = strtoupper(md5($post_data));
    $ordernumber = $post_data['out_trade_no'];
    if ($post_data['return_code'] == 'SUCCESS' && $postSign) {
    // ������������������
    $result = M('userorder')->where('ordernumber = "'.$ordernumber.'"')->select();
    if ($result) {
    if ($result[0]['paystatus'] == 0) {
    // ������������������
    $obj = array(
    "paystatus" => 1,
    );
    $res = M('userorder')->where('ordernumber = "'.$ordernumber.'"')->save($obj);
    file_put_contents('gg.txt', $res);
    if ($res) {
    $this->return_success();
    }
    } else {
    $this->return_success();
    }
    } else {
    echo '������������������,���������������������������';
    }
    } else {
    // ������������������������
    file_put_contents('wxpayerrorlog.txt', $post_data['return_code'].PHP_EOL, FILE_APPEND);
    echo '������������������';
    }

    ���������������������

  • ���������������
  • ���������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:基于phpqrcode类,生成二维码
    下一篇:tp分页自己做

    发表评论

    最新留言

    第一次来,支持一个
    [***.219.124.196]2025年04月06日 01时06分44秒