站长图库向大家介绍了PHP微信支付的类库,微信扫码支付,微信小程序支付等相关知识,希望对您有所帮助
在我们编写相关支付的时候,经常会使用微信支付,在php中使用微信支付还是比较简单的
微信支付文档:https://pay.weixin.qq.com/wiki/doc/api/index.html
这里简单介绍微信支付的几种使用,这里微信支付我封装了一个微信支付的类库,直接传递参数就可使用:
首先把配置文件填写完整(细心不要填错,否则会导致签名错误):
$config = array( 'appid' => '', // 微信支付appid 'xcxappid' => '', // 微信小程序appid 'mch_id' => '', // 微信支付 mch_id 商户收款账号 'key' => '', // 微信支付key 'appsecret' => '', // 公众帐号secert(公众号支付专用) 'notify_url' => '', // 接收支付状态的连接 改成自己的回调地址 'redirect_uri' => '', // 公众号支付时,没有code,获取openid使用);
对于相关支付我也也成了函数便于使用,
微信扫码支付:
/** * [qrcodePay 微信扫码支付] * @param [type] $order [订单信息数组] * @return [type] [description] * $order = array( * 'body' => '', // 产品描述 * 'total_fee' => '', // 订单金额(分) * 'out_trade_no' => '', // 订单编号 * 'product_id' => '', // 产品id(可用订单编号) * ); */public static function qrcodePay($order=NULL){ if(!is_array($order) || count($order) < 4){ die("数组数据信息缺失!"); } $order['trade_type'] = 'NATIVE'; // Native支付 $result = self::unifiedOrder($order); $decodeurl = urldecode($result['code_url']); return $decodeurl; // 使用返回链接直接生成二维码}微信H5支付:
/** * [weixinH5 微信H5支付] * @param [type] $order [订单信息数组] * @return [type] [description] * $order = array( * 'body' => '', // 产品描述 * 'total_fee' => '', // 订单金额(分) * 'out_trade_no' => '', // 订单编号 * 'product_id' => '', // 产品id(可用订单编号) * ); */public static function h5Pay($order=NULL){ if(!is_array($order) || count($order) < 4){ die("数组数据信息缺失!"); } $order['trade_type'] = 'MWEB'; // H5支付 $result = self::unifiedOrder($order); if ($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS') return $result['mweb_url']; // 返回链接让用户点击跳转 if ($result['err_code_des']) die($result['err_code_des']); return false;}微信小程序支付:
/** * [xcxPay 获取jssdk需要用到的数据] * @param [type] $order [订单信息数组] * @param boolean $type [区分是否是小程序,默认 true] * @return [type] [description] * $order = array( * 'body' => '', // 产品描述 * 'total_fee' => '', // 订单金额(分) * 'out_trade_no' => '', // 订单编号 * 'product_id' => '', // 产品id(可用订单编号) * 'openid' => '', // 用户openid * ); */public static function xcxPay($order=NULL,$type=true){ if(!is_array($order) || count($order) < 5){ die("数组数据信息缺失!"); } $order['trade_type'] = 'JSAPI'; // 小程序支付 $result = self::unifiedOrder($order,$type); if ($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS') { $data = array ( 'appId' => $type ? $this->config['xcxappid'] : $this->config['appid'], 'timeStamp' => time(), 'nonceStr' => self::get_rand_str(32, 0, 1), // 随机32位字符串 'package' => 'prepay_id='.$result['prepay_id'], 'signType' => 'MD5', // 加密方式 ); $data['paySign'] = self::makeSign($data); return $data; // 数据小程序客户端 } else { if ($result['err_code_des']) die($re

