TP微信扫二维码支付接口开发

admin_email 2017-05-26 05:27:56
求大神指教 怎么弄 啊 本人新手 最近给一个商城做一个微信扫码支付功能
...全文
292 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
admin_email 2017-05-27
  • 打赏
  • 举报
回复
谢谢 ! 我想问一下 你这用的是TP框架吗
林0507 2017-05-26
  • 打赏
  • 举报
回复
之前写的一个项目里也有用到生成微信支付二维码, $wechat = new WechatPay(appid,wechat_mchid, wechat_key); $result = $wechat->sendPay(订单号, 价格,名称, 回调地址,id); 你参考下吧,里面一些公共的方法自行替换,二维码url就在$result['code_url']。获取到后用qrcode生成二维码就可以了 Common::getRandChar(32); 是取32位随机字符串 Common::https_request是curl操作 就不贴代码了
class WechatPay{
    protected $wechat_appid;
    protected $wechat_mchid;
    protected $wechat_key;

    function __construct($wechat_appid, $wechat_mchid, $wechat_key) {
        $this->wechat_appid = $wechat_appid;
        $this->wechat_mchid = $wechat_mchid;
        $this->wechat_key = $wechat_key;
    }

    public function sendPay($out_trade_no, $total_fee, $subject, $notifyUrl, $id = 0) {
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        $nonce_str = Common::getRandChar(32);   //随机字符串,长度要求在32位以内。
        $param = [
            'appid' => $this->wechat_appid,
            'mch_id' => $this->wechat_mchid,
            'nonce_str' => $nonce_str,
            'body' => $subject,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $total_fee,
            'spbill_create_ip' => '127.0.0.1',
            'notify_url' => $notifyUrl,
            'trade_type' => 'NATIVE',
            'product_id' => $id,
        ];
        $xml = self::paySign($param, $this->wechat_key);
        $http_result = Common::https_request($url, $xml['xml']);
        $xml = simplexml_load_string($http_result, NULL, LIBXML_NOCDATA);
        $result = json_decode(json_encode($xml), true);
        if($result['return_code'] == 'FAIL'){
            return ['result_code' => 'FAIL'];
        }
        return $result;
    }


    // 支付签名
    public static function paySign($param, $wechatkey) {
        ksort($param);
        $stringSignTemp = '';
        $xml = '<xml>';
        foreach ($param as $key => $vo) {
            $stringSignTemp .= $key . '=' . $vo . '&';
            $xml .= '<' . $key . '>' . $vo . '</' . $key . '>';
        }
        //  $stringSignTemp = rtrim($stringSignTemp,"&");
        $stringSignTemp .= 'key=' . $wechatkey;
        $sign = strtoupper(md5($stringSignTemp));
        $xml .= '<sign>' . $sign . '</sign>';
        $xml .= '</xml>';
        $result_arr = [
            'sign' => $sign,
            'xml' => $xml
        ];
        return $result_arr;
    }

}

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧