微信APP支付demo

2021-08-19 15:35:25 字數 4350 閱讀 4517

<?php

use think\controller;

/** */

class wxpaycontroller extends homecontroller

public function wxpay()

$outtradeno=$order_info['tag'].time();

//$totalfee=$order_info['total_money']*100;

$totalfee=$order_info['total_money']*100;

//$outtradeno=201801151686367077;

$total = round(floatval($totalfee),2);

if (!$total)

$ordername = "訂單編號:".$order_info['orderid'];

$spbillip = $_server['server_addr'];

$this->createjsbizpackage($total, $outtradeno, $ordername, $notifyurl, $spbillip);

}protected function createjsbizpackage($totalfee, $outtradeno, $ordername, $notifyurl, $spbillip)

if ($unifiedorder->return_code != 'success')

if ($unifiedorder->result_code != 'success')

//該陣列內容是需要二次簽名返回給客服端的,必須有時間戳,鍵名命名不能用駝峰法,不能有下劃線或其他任何符號

/*$nonce_str = $this->toarray($unifiedorder->nonce_str);

print_r($nonce_str);die;

$prepay_id = $this->toarray($unifiedorder->prepay_id);*/

$nonce_str = $unifiedorder->nonce_str;

$nonce_str = (string)$nonce_str;

$prepay_id = $unifiedorder->prepay_id;

$prepay_id = (string)$prepay_id;

$arr = array(

"partnerid" => $config['mch_id'],

"timestamp" => time(),

"noncestr" => $nonce_str,

"prepayid" => $prepay_id,

"package" => 'sign=wxpay',

);$arr['sign'] = $this->getsign($arr, $config['key']); //第二次簽名

$arr['outtradeno'] = $unified['out_trade_no']; //返回商戶訂單號

//print_r($arr);die;

$this->ajaxreturn($arr) ;

}public static function toarray($******xml_obj, $array_tags=array(), $strip_white=1)

$attr = array();

foreach ($******xml_obj as $k=>$val) else

}return $attr;

}return false;

}/**

* 生成隨機字串

* @param type $length 設定隨機長度,預設32位

*/protected function createnoncestr($length = 32)

return $str;

}/**

* 獲取簽名

* @param type $params 簽名的物件

* @param type $key 簽名key |_mc`3^+z!kdln>u~5cb

/*** 簽名規則

* @param type $paramap

* @param type $urlencode

* @return type

*/private function formatqueryparamap($paramap, $urlencode = false)

$buff .= $k . "=" . $v . "&";}}

$reqpar = '';

if (strlen($buff) > 0)

return $reqpar;

}/**

* curl post

* @param type $url

* @param type $postdata

* @param type $options

* @return type

*/protected function curlpost($url = '', $postdata = '', $options = array())

$ch = curl_init();

curl_setopt($ch, curlopt_url, $url);

curl_setopt($ch, curlopt_returntransfer, 1);

curl_setopt($ch, curlopt_post, 1);

curl_setopt($ch, curlopt_postfields, $postdata);

curl_setopt($ch, curlopt_timeout, 30); //設定curl允許執行的最長秒數

if (!empty($options))

//https請求 不驗證證書和host

curl_setopt($ch, curlopt_ssl_verifypeer, false);

curl_setopt($ch, curlopt_ssl_verifyhost, false);

$data = curl_exec($ch);

curl_close($ch);

return $data;

}/**

* curl get

* @param string $url

* @param array $options

* @return mixed

*/protected function curlget($url = '', $options = array())

//https請求 不驗證證書和host

curl_setopt($ch, curlopt_ssl_verifypeer, false);

curl_setopt($ch, curlopt_ssl_verifyhost, false);

$data = curl_exec($ch);

curl_close($ch);

return $data;

}/**

* 陣列轉化為xml

* @param type $arr

* @return string

*/protected function arraytoxml($arr) else

$xml .= "<" . $key . ">";

}$xml .= "";

return $xml;

}public function logresult($poststr)

function xmltoarray($xml)

/*** @return type

* 通知頻率為15/15/30/180/1800/1800/1800/1800/3600,單位:秒

*/public function wx_notify()

if ($postobj->return_code != 'success')

if ($postobj->result_code != 'success')

$arr = (array) $postobj;

unset($arr['sign']);

if ($this->getsign($arr, $config['key']) == $postobj->sign) else if ($postobj->result_code == "fail") else

}echo exit('');

//此處應該更新一下訂單狀態,商戶自行增刪操作;操作成功

//這裡就可以做自己資料庫訂單判斷及操作了

//這裡可以選擇直接sql運算元據庫,也可以根據自己需要,模擬自己的加密演算法去請求自己的介面

}} else

}}

微信app支付

商戶id public static final string partner id 14698sdfs402dsfdew402 常量固定值 public static final string grant type client credential 獲取預支付id的介面url public st...

微信app支付

文件 arr mchid mchid,nonce str str random 32 隨機數 partner trade no order id,自己定義乙個不重複訂單號 check name no check desc desc,spbill create ip request getclient...

微信APP支付

final string mchid,final string notifyurl,final string outtradeno,final string noncestr,final string body,final string productid,final string totalfee...