求大神指教 怎么弄 啊 本人新手 最近给一个商城做一个微信扫码支付功能
回复讨论(解决方案)
之前写的一个项目里也有用到生成微信支付二维码,
$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 = ”;
foreach ($param as $key => $vo) {
$stringsigntemp .= $key . ‘=’ . $vo . ‘&’;
$xml .= ” . $vo . ”;
}
// $stringsigntemp = rtrim($stringsigntemp,”&”);
$stringsigntemp .= ‘key=’ . $wechatkey;
$sign = strtoupper(md5($stringsigntemp));
$xml .= ” . $sign . ”;
$xml .= ”;
$result_arr = [
‘sign’ => $sign,
‘xml’ => $xml
];
return $result_arr;
}
}