亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于thinkphp5框架實現(xiàn)微信小程序支付 退款 訂單查詢 退款查詢操作

 更新時間:2020年08月17日 14:59:01   作者:科小洋_Lmy  
這篇文章主要介紹了基于thinkphp5框架實現(xiàn)微信小程序支付 退款 訂單查詢 退款查詢操作,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

微信小程序或微信支付相關(guān)操作支付退款訂單查詢退款查詢支付成功,進(jìn)行回調(diào)退款成功 進(jìn)行回調(diào)用到的方法

支付

/**
 * 預(yù)支付請求接口(POST)
 * @param string $openid openid
 * @param string $body 商品簡單描述
 * @param string $order_sn 訂單編號
 * @param string $total_fee 金額
 * @return json的數(shù)據(jù)
 */
 public function prepay()
 {
 tp_log('預(yù)支付請求數(shù)據(jù)===>' . json_encode($_POST), 'prepay', request()->controller());
 $goods_user = db('tf_goods_user')->where(array('order_no' => $_POST['order_no']))->find();
 $goods = db('tf_goods')->where(array('id' => $goods_user['goods_id']))->find();
 //判斷產(chǎn)品的數(shù)量
 if (($goods['sales_initial'] - $goods['sales_actual']) <= 0) {
 $return['status'] = 0;
 $return['info'] = '此產(chǎn)品已售完';
 exit(json_encode($return));
 }

 $order_no = $_POST['order_no']; //訂單號
 $is_sale = $_POST['is_sale'];
 $openid = $_POST['openid'];
 $goods_name = $_POST['goods_name'];
 $pay_price = $_POST['price'];
 $attach['is_sale'] = $_POST['is_sale'];
 $attach['sale_id'] = $_POST['sale_id'];
 $nonce_str = $this->nonce_str();//隨機(jī)字符串


 $order_no_ssh = $this->get_orderssh(); //商戶訂單號
 //組裝支付數(shù)據(jù)
 $data = [
 'appid' => config('pay.APPID'),
 'mch_id' => config('pay.MCHID'),
 'nonce_str' => $nonce_str,
 'body' => $goods_name, //商品名稱組合
 'attach' => json_encode($attach),
 'out_trade_no' => $order_no_ssh,//$order_no, //訂單號 商戶訂單號
 'total_fee' => intval($pay_price * 100),
 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
 'notify_url' => config('pay.NOTIFY_URL'),
 'trade_type' => 'JSAPI',
 'openid' => $openid
 ];

 //訂單支付表創(chuàng)建訂單支付數(shù)據(jù)
 $p_o_data['createtime'] = time();
 $p_o_data['order_no'] = $order_no;
 $p_o_data['order_no_ssh'] = $order_no_ssh;
 $p_o_data['ready'] = json_encode($data);
 $p_o_return = db('tf_pay_order')->insert($p_o_data);
 if(!$p_o_return){
 //失敗
 $return['status'] = -1;
 $return['info'] = $p_o_data;
 exit(json_encode($return));
 }

 // 獲取簽名
 $sign = $this->makeSign($data);
 $data['sign'] = $sign;
 $xml = $this->toXml($data);
 $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; //發(fā)起支付接口鏈接
 //發(fā)起預(yù)支付請求
 $prepay_return_reslut_xml = $this->http_request($url, $xml);
 $xml_to_arr = $this->fromXml($prepay_return_reslut_xml);
 $return_result = json_encode($xml_to_arr, true);
 tp_log('預(yù)支付請求返回數(shù)據(jù)array===>' .$return_result , 'prepay', request()->controller());
 //記錄預(yù)支付返回信息
 db('tf_goods_order')->where(array('order_no' => $order_no))
 ->update(array(
 'go_pay' => $return_result,
 'updatetime' => time(),
 'updateuser' => $openid
 ));
 if($xml_to_arr['return_code'] == 'SUCCESS' && $xml_to_arr['result_code'] == 'SUCCESS'){
 //成功

 $time = time();
 //臨時數(shù)組用于簽名
 $tmp = [
 'appId' => config('pay.APPID'),
 'nonceStr' => $nonce_str,
 'package' => 'prepay_id='.$xml_to_arr['prepay_id'],
 'signType' => 'MD5',
 'timeStamp' => "$time",
 ];
 $data['timeStamp'] = "$time";//時間戳
 $data['nonceStr'] = $nonce_str;//隨機(jī)字符串
 $data['signType'] = 'MD5';//簽名算法,暫支持 MD5
 $data['package'] = 'prepay_id='.$xml_to_arr['prepay_id'];//統(tǒng)一下單接口返回的 prepay_id 參數(shù)值,提交格式如:prepay_id=*
 $data['paySign'] = $this->makeSign($tmp);//簽名,具體簽名方案參見微信公眾號支付幫助文檔;$data['out_trade_no'] = $out_trade_no;


 $return['status'] = 1;
 $return['info'] = $data;
 }else{
 //失敗
 $return['status'] = -1;
 $return['info'] = $xml_to_arr;
 }
 exit(json_encode($return));
 }

 //curl請求
 public function http_request($url, $data = null, $headers = array())
 {
 $curl = curl_init();
 if (count($headers) >= 1) {
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 }
 curl_setopt($curl, CURLOPT_URL, $url);


 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);


 if (!empty($data)) {
 curl_setopt($curl, CURLOPT_POST, 1);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 }
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $output = curl_exec($curl);
 curl_close($curl);
 return $output;
 }

退款

/**
 * 申請退款A(yù)PI
 * @param $transaction_id
 * @param double $total_fee 賬單總金額
 * @param double $refund_fee 退款金額
 * @return bool
 * @throws BaseException
 */
 public function refund()
 {

 $transaction_id = '4200000712202007274705432240';
 $total_fee = '0.01';
 $refund_fee = '0.01';
 // 當(dāng)前時間
 $time = time();
 // 生成隨機(jī)字符串
 $nonceStr = md5($time . $transaction_id . $total_fee . $refund_fee);
 // API參數(shù)
 $params = [
 'appid' => config('pay.APPID'),
 'mch_id' => config('pay.MCHID'),
 'nonce_str' => $nonceStr,
 'transaction_id' => $transaction_id,
 'out_refund_no' => $time,
 'total_fee' => $total_fee * 100,
 'refund_fee' => $refund_fee * 100,
 'notify_url' => config('pay.NOTIFY_URL_REFUND'),//退款回調(diào)地址
 ];
 // 生成簽名
 $params['sign'] = $this->makeSign($params);

 tp_log('退款請求數(shù)據(jù)===>' . json_encode($params), 'refund', request()->controller());

 // 請求API
 $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
 $result = $this->post($url, $this->toXml($params), true, $this->getCertPem());
 $prepay = $this->fromXml($result);
 // 請求失敗
 if (empty($result)) {
 throw new BaseException(['msg' => '微信退款api請求失敗']);
 }
 // 格式化返回結(jié)果
 $prepay = $this->fromXml($result);
 tp_log('退款返回數(shù)據(jù)===>' . json_encode($prepay), 'refund', request()->controller());
 // 請求失敗
// if ($prepay['return_code'] === 'FAIL') {
// throw new BaseException(['msg' => 'return_msg: ' . $prepay['return_msg']]);
// }
// if ($prepay['result_code'] === 'FAIL') {
// throw new BaseException(['msg' => 'err_code_des: ' . $prepay['err_code_des']]);
// }
 return true;
 }
 /**
 * 模擬POST請求
 * @param $url
 * @param array $data
 * @param bool $useCert
 * @param array $sslCert
 * @return mixed
 */
 public function post($url, $data = [], $useCert = false, $sslCert = [])
 {
 $header = [
 'Content-type: application/json;'
 ];
 $curl = curl_init();
 //如果有配置代理這里就設(shè)置代理
// if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
// && WxPayConfig::CURL_PROXY_PORT != 0){
// curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
// curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
// }

 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
 curl_setopt($curl, CURLOPT_HEADER, false);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_POST, TRUE);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 if ($useCert == true) {
 // 設(shè)置證書:cert 與 key 分別屬于兩個.pem文件
 curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
 curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem']);
 curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
 curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem']);
 }
 $result = curl_exec($curl);
 curl_close($curl);
 return $result;
 }

訂單查詢

/**
 * 訂單查詢
 * @param $out_trade_no
 * @return mixed
 * @throws BaseException
 */
 public function orderquery()
 {
 $transaction_id = '4200000712202007274705432240';//微信訂單號
 // 當(dāng)前時間
 $time = time();
 // 生成隨機(jī)字符串
 $nonce_str = md5($time . mt_rand(00000,99999));
 //API參數(shù)
 $params = [
 'appid' => config('pay.APPID'), //公眾號ID
 'mch_id' => config('pay.MCHID'), //商戶號
 'transaction_id' => $transaction_id, //商戶訂單號
 'nonce_str' => $nonce_str, // 隨機(jī)字符串
 ];
 //生成簽名
 $params['sign'] = $this->makeSign($params);
 //請求API
 $url = 'https://api.mch.weixin.qq.com/pay/orderquery';
 $result = $this->post($url, $this->toXml($params));
 $prepay = $this->fromXml($result);
 // 請求失敗
 if ($prepay['return_code'] === 'FAIL') {
 throw new BaseException(['msg' => $prepay['return_msg'], 'code' => 0]);
 }
 if ($prepay['result_code'] === 'FAIL') {
 throw new BaseException(['msg' => $prepay['err_code_des'], 'code' => 0]);
 }
 return $prepay;
 }

退款查詢

/**
 * 退款查詢
 * @param $out_trade_no
 * @return mixed
 * @throws BaseException
 */
 public function refundquery()
 {
 $transaction_id = '4200000712202007274705432240';//微信訂單號
 // 當(dāng)前時間
 $time = time();
 // 生成隨機(jī)字符串
 $nonce_str = md5($time . mt_rand(00000,99999));
 //API參數(shù)
 $params = [
 'appid' => config('pay.APPID'), //公眾號ID
 'mch_id' => config('pay.MCHID'), //商戶號
 'transaction_id' => $transaction_id, //商戶訂單號
 'nonce_str' => $nonce_str, // 隨機(jī)字符串
 ];
 //生成簽名
 $params['sign'] = $this->makeSign($params);
 //請求API
 $url = 'https://api.mch.weixin.qq.com/pay/refundquery';
 $result = $this->post($url, $this->toXml($params));
 $prepay = $this->fromXml($result);
 dump($prepay);die;
 // 請求失敗
 if ($prepay['return_code'] === 'FAIL') {
 throw new BaseException(['msg' => $prepay['return_msg'], 'code' => 0]);
 }
 if ($prepay['result_code'] === 'FAIL') {
 throw new BaseException(['msg' => $prepay['err_code_des'], 'code' => 0]);
 }
 return $prepay;
 }

支付成功,進(jìn)行回調(diào)

public function index()
 {
 $data = file_get_contents('php://input');
 $data = $this->FromXml($data);
 tp_log('支付回調(diào)數(shù)據(jù)===>' . json_encode($data), 'index', request()->controller());
 // 保存微信服務(wù)器返回的簽名sign
 $data_sign = $data['sign'];
 // sign不參與簽名算法
 unset($data['sign']);
 $sign = $this->makeSign($data);//回調(diào)驗證簽名

 $str_success = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
 $str_error = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[簽名失敗]]></return_msg></xml>';

 if (($sign === $data_sign) && ($data['return_code'] == 'SUCCESS') && ($data['result_code'] == 'SUCCESS')) {
 // 支付成功 進(jìn)行你的邏輯處理
 }
echo $str_success;//str_error 告知微信 你已的邏輯處理完畢 不用再推送或再次推送你結(jié)果
 }

退款成功 進(jìn)行回調(diào)

/*
 * 小程序 退款結(jié)果通知
 */
 public function refund(){

 $data = file_get_contents('php://input');
 $data = $this->FromXml($data);
 tp_log('退款回調(diào)數(shù)據(jù)===>' . json_encode($data), 'refund', request()->controller());

 //對加密的字符串解密
 $req_info_xml = openssl_decrypt(base64_decode($data['req_info']), 'aes-256-ecb', md5(config('pay.KEY')),OPENSSL_RAW_DATA);
 $req_info = $this->FromXml($req_info_xml);

// // 保存微信服務(wù)器返回的簽名sign
// $data_sign = $data['sign'];
// // sign不參與簽名算法
// unset($data['sign']);
// $sign = $this->makeSign($data);//回調(diào)驗證簽名
//
// $str_success = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
// $str_error = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[簽名失敗]]></return_msg></xml>';
//
//
//
// if (($sign === $data_sign) && ($data['return_code'] == 'SUCCESS') && ($data['result_code'] == 'SUCCESS')) {
// tp_log('退款成功===>', 'refund', request()->controller());
// //去修改訂單的狀態(tài) 和支付回調(diào)的一樣 修改成功 告知微信 不在推送
// }
 }

用到的方法

/**
 * 生成簽名
 * @param $values
 * @return string 本函數(shù)不覆蓋sign成員變量,如要設(shè)置簽名需要調(diào)用SetSign方法賦值
 */
 private function makeSign($values)
 {
 //簽名步驟一:按字典序排序參數(shù)
 ksort($values);
 $string = $this->toUrlParams($values);
 //簽名步驟二:在string后加入KEY
 $string = $string . '&key=' . config('pay.KEY');
 //簽名步驟三:MD5加密
 $string = md5($string);
 //簽名步驟四:所有字符轉(zhuǎn)為大寫
 $result = strtoupper($string);
 return $result;
 }
 private function ToUrlParams($array)
 {
 $buff = "";
 foreach ($array as $k => $v) {
 if ($k != "sign" && $v != "" && !is_array($v)) {
 $buff .= $k . "=" . $v . "&";
 }
 }
 $buff = trim($buff, "&");
 return $buff;
 }

 /**
 * 輸出xml字符
 * @param $values
 * @return bool|string
 */
 private function toXml($values)
 {
 if (!is_array($values)
 || count($values) <= 0
 ) {
 return false;
 }
 $xml = "<xml>";
 foreach ($values as $key => $val) {
 if (is_numeric($val)) {
 $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
 } else {
 $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
 }
 }
 $xml .= "</xml>";
 return $xml;
 }

 /**
 * 將xml轉(zhuǎn)為array
 * @param $xml
 * @return mixed
 */
 private function fromXml($xml)
 {
 // 禁止引用外部xml實體
 libxml_disable_entity_loader(true);
 return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
 }

 /**
 * 獲取cert證書文件
 * @return array
 * @throws BaseException
 */
 private function getCertPem()
 {
// if (empty($this->config['cert_pem']) || empty($this->config['key_pem'])) {
// throw new BaseException(['msg' => '請先到后臺小程序設(shè)置填寫微信支付證書文件']);
// }
 // cert目錄
 $filePath = EXTEND_PATH.'wxpay/cert/';
 return [
 'certPem' => $filePath . 'apiclient_cert.pem',
 'keyPem' => $filePath . 'apiclient_key.pem'
 ];
 }
/**
 * 生成商戶訂單號
 */
 public function get_orderssh()
 {
 return date("YmdHis") . mt_rand(10000000, 99999999);
 }

證書路徑

在這里插入圖片描述

config配置

在這里插入圖片描述

總結(jié)

到此這篇關(guān)于基于thinkphp5框架實現(xiàn)微信小程序支付 退款 訂單查詢 退款查詢的文章就介紹到這了,更多相關(guān)微信小程序支付 退款 訂單查詢 退款查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Yii 框架使用Forms操作詳解

    Yii 框架使用Forms操作詳解

    這篇文章主要介紹了Yii 框架使用Forms操作,結(jié)合實例形式分析了Yii 框架使用Forms模型、動作創(chuàng)建及使用相關(guān)操作技巧,需要的朋友可以參考下
    2020-05-05
  • php模板引擎技術(shù)簡單實現(xiàn)

    php模板引擎技術(shù)簡單實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了php模板引擎技術(shù)簡單實現(xiàn),感興趣的小伙伴們可以參考一下
    2016-03-03
  • 淺析Yii2 gridview實現(xiàn)批量刪除教程

    淺析Yii2 gridview實現(xiàn)批量刪除教程

    在朋友圈里有童鞋向我討論GridView的問題,有朋友說你用gridview給我去掉表頭的鏈接?我想的很久,用gridview確實不容易實現(xiàn)。有不同見解的朋友歡迎留言。但是呢,這根gridview有個毛線關(guān)聯(lián)啊,明明是要設(shè)置ActiveDataProvider,你要我怎么用gridview實現(xiàn)嘛
    2016-04-04
  • 基于PHP-FPM進(jìn)程池探秘

    基于PHP-FPM進(jìn)程池探秘

    下面小編就為大家?guī)硪黄赑HP-FPM進(jìn)程池探秘。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 淺談discuz密碼加密的方式

    淺談discuz密碼加密的方式

    這篇文章主要介紹了discuz密碼加密的方式,需要的朋友可以參考下
    2014-05-05
  • 詳解WordPress中提醒安裝插件以及隱藏插件的功能實現(xiàn)

    詳解WordPress中提醒安裝插件以及隱藏插件的功能實現(xiàn)

    這篇文章主要介紹了WordPress中提醒安裝插件以及隱藏插件的功能實現(xiàn),這兩種功能通常在多用戶模式下進(jìn)行管理時用得比較多,需要的朋友可以參考下
    2015-12-12
  • 如何使用微信公眾平臺開發(fā)模式實現(xiàn)多客服

    如何使用微信公眾平臺開發(fā)模式實現(xiàn)多客服

    其實微信公眾平臺的多客服功能已經(jīng)出來好久了,并且一出來的時候我就已經(jīng)為自己的公眾號實現(xiàn)了,原本以為大家都已經(jīng)會了,但是今天還是有人問起這個多客服功能怎么使用,我找了下網(wǎng)上也沒什么好的教程,今天我就給大家發(fā)一篇比較簡單易懂的教程吧
    2016-01-01
  • PHP實現(xiàn)賽郵SUBMAIL簡單易用短信通知實例

    PHP實現(xiàn)賽郵SUBMAIL簡單易用短信通知實例

    這篇文章主要介紹了使用PHP實現(xiàn)賽郵SUBMAIL的簡單易用短信通知實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • thinkphp5上傳圖片及生成縮略圖公共方法(分享)

    thinkphp5上傳圖片及生成縮略圖公共方法(分享)

    下面小編就為大家分享一篇thinkphp5上傳圖片及生成縮略圖公共方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • thinkphp實現(xiàn)圖片上傳功能分享

    thinkphp實現(xiàn)圖片上傳功能分享

    圖片上傳在網(wǎng)站里是很常用的功能.ThinkPHP里也有自帶的圖片上傳類(UploadFile.class.php) 和圖片模型類(Image.class.php)。方便于我們?nèi)崿F(xiàn)圖片上傳功能,下面是實現(xiàn)方法
    2014-03-03

最新評論