本文主要是介绍微信小程序支付之单次支付,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//获取支付必备参数prepay_id 以及进行支付返回paysign public function pay($id){//$order_id = input('order_id'); //进行查询数据库获得支付参数, header("Content-type:text/html;charset=utf-8"); //此处进行字符集初始化, // $pay_data = DB::name('pay')->find(); $order_data = db('order')->where('id', $id)->find();$notify_url = 'https://' . $_SERVER['HTTP_HOST'] . '/index.php/home/Index/payok';$data = array(//'appid' => 'wxf0d27e41678c566c', //小程序appid 'appid' => Config::get('appid'),'body' => '腾讯-游戏',//'mch_id' => '1484855592', //微信支付商户号 'mch_id' => Config::get('mch_id'),'nonce_str' => md5(date('YmdHis') . time() . rand(1000, 9999)), //随机字符串 'notify_url' => $notify_url, //异步回调地址 'openid' => $order_data['openid'], //用户登录时获取的code中含有的值 'out_trade_no' => $order_data['orderno'], //商家订单号 'spbill_create_ip' => '127.0.0.1', //APP和网页支付提交用户端ip 'total_fee' => $order_data['total']*100, //订单总额 // 'total_fee' => 1, 'attach' => 'borrow', //确定是哪个商家进行的支付 'trade_type' => 'JSAPI' //交易类型 );// echo_info($data);die; //$key = 'z4hgl4cnf5ac2wl3msiek5p0x3aiy2yc'; //商户秘钥 $key = Config::get('shopsecret');//制作签名 //签名步骤一:按字典序排序参数 ksort($data);$buff = "";foreach ($data as $k => $v) {if ($k != "sign" && $v != "" && !is_array($v)) {$buff .= $k . "=" . $v . "&";}}$buff = trim($buff, "&");//签名步骤二:在string后加入KEY $string = $buff . "&key=" . $key;//签名步骤三:MD5加密 $string = md5($string);//签名步骤四:所有字符转为大写 $sign = strtoupper($string);$data['sign'] = $sign;ksort($data);//进行拼接数据 $abc_xml = "<xml>";foreach ($data as $key => $val) {if (is_numeric($val)) {$abc_xml .= "<" . $key . ">" . $val . "</" . $key . ">";} else {$abc_xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";}}$abc_xml .= "</xml>";//统一下单接口prepay_id $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';// $abc_xml=array2xml($data); //dump($abc_xml);die; $xml = http_request_curl($url, $abc_xml); //POST方式请求http //将XMl转化为数组 $info = xml2array($xml);// echo_info($info);die(); //进行拼接sign function MakeSign($params, $KEY){//签名步骤一:按字典序排序数组参数 ksort($params);$buff1 = '';foreach ($params as $k => $v) {if ($k != "sign" && $v != "" && !is_array($v)) {$buff1 .= $k . "=" . $v . "&";}}$buff1 = trim($buff1, "&");//签名步骤二:在string后加入KEY $string = $buff1 . "&key=" . $KEY;//签名步骤三:MD5加密 $string = md5($string);//签名步骤四:所有字符转为大写 $result = strtoupper($string);return $result;}if (empty($info['prepay_id'])) {echo_info(array('status' => -1, 'message' => '商户号id和秘钥不匹配'));}$params = array('appId' => Config::get('appid'),'nonceStr' => $data['nonce_str'],'package' => 'Sign=WXPay&prepay_id=' . $info['prepay_id'],'signType' => 'MD5','timeStamp' => time());//$key = 'z4hgl4cnf5ac2wl3msiek5p0x3aiy2yc'; //商户秘钥 $key = Config::get('shopsecret');$info['paySign'] = MakeSign($params, $key);$info['timeStamp'] = "" . $params['timeStamp'] . "";$info['nonceStr'] = $params['nonceStr'];$info['package'] = $params['package'];//请求成功后进行返回数据信息 if ($info['return_code'] == 'SUCCESS' || $info['result_code'] == 'SUCCESS') {echo_info(array('status' => 1, 'message' => '请求成功', 'data' => $info));} else {echo_info(array('status' => -1, 'message' => '请求失败', 'data' => $info));}}public function payok() {error_reporting(0);define('IN_MOBILE', true);libxml_disable_entity_loader(true);$xml = '<?xml version="1.0" encoding="utf-8"?>';$xml .= $GLOBALS['HTTP_RAW_POST_DATA'];$obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);$arrayinfo = xmlToArray($obj);if (($arrayinfo['result_code'] = 'SUCCESS' || $arrayinfo['return_code'] = 'SUCCESS')) {$out_trade_no = $arrayinfo['out_trade_no']; //商户订单号 $transaction_id = $arrayinfo['transaction_id']; //微信订单支付订单号 $result = DB::name('order')->where('orderno', $out_trade_no)->find();if ($result['status'] != 1) {$datas = array('status' => 1,'transid' => $transaction_id );$res = DB::name('order')->where('orderno', $out_trade_no)->update($datas);if ($res) {return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';} else {return '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[NO]]></return_msg></xml>';}} else {return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';}} else {return '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[NO]]></return_msg></xml>';}}//调试过程可能会出现商户号id和秘钥不匹配,重置一下秘钥就好
这篇关于微信小程序支付之单次支付的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!