微信小程式支付

2021-09-17 03:16:18 字數 3924 閱讀 9441

<?php

class wxpay{

protected $mch_id;

protected $key;

protected $openid;

protected $out_trade_no;

protected $body;

protected $total_fee;

protected $notifyurl;

protected $unfiedorderurl;

protected $trade_type;

$this->mch_id=$mch_id;

$this->openid = $openid;

$this->key = $key;

$this->out_trade_no = $out_trade_no;

$this->body = $body;

$this->total_fee = $total_fee;

$this->notifyurl=$notifyurl;

$this->unfiedorderurl=$unifiedorderurl;

$this->trade_type=$trade_type;

public function pay(){

//統一下單介面

$unifiedorader=$this->unifiedorder();

$parameters=array(

'timestamp'=>''.time().'',

'noncestr'=>$this->createnoncestr(),

'package' =>'prepay_id='.$unifiedorader['prepay_id'],

'signtype'=>'md5'

$parameters['paysign']=$this->getsign($parameters);

//返回商戶號

$parameters['out_trade_no']=$this->out_trade_no;

return $parameters;

public function unifiedorder(){

$parameters=array(

'mch_id'=>$this->mch_id,

'nonce_str'=>$this->createnoncestr(),

'body'=>$this->body,

'out_trade_no'=>$this->out_trade_no,

'total_fee'=>$this->total_fee,

'spbill_create_ip'=>$_server['remote_addr'],

'notify_url'=> $this->notifyurl,

'trade_type'=>$this->trade_type

if($this->trade_type=='jsapi')

$parameters['openid']=$this->openid;

ksort($parameters);

$parameters['sign']=$this->getsign($parameters);

$xmldata=$this->arraytoxml($parameters);

$return=$this->xmltoarray($this->postxmlcurl($xmldata,$this->unfiedorderurl,60));

return $return;

private function postxmlcurl($xml,$url,$second=30){

$ch=curl_init();

//超時

curl_setopt($ch,curlopt_timeout,$second);

curl_setopt($ch,curlopt_url,$url);

curl_setopt($ch, curlopt_ssl_verifypeer, false);

curl_setopt($ch, curlopt_ssl_verifyhost, false); //嚴格校驗

curl_setopt($ch, curlopt_header, false);

curl_setopt($ch, curlopt_returntransfer, true);

curl_setopt($ch, curlopt_post, true);

curl_setopt($ch, curlopt_postfields, $xml);

curl_setopt($ch, curlopt_connecttimeout, 20);

curl_setopt($ch, curlopt_timeout, 40);

set_time_limit(0);

$data=curl_exec($ch);

if($data){

curl_close($ch);

return $data;

else{

$error=curl_errno($ch);

curl_close($ch);

throw new exception("出錯,出錯碼".$error);

public static function xmltoarray($xml){

//xml轉array

libxml_disable_entity_loader(true);

$xmlstring = ******xml_load_string($xml, '******xmlelement', libxml_nocdata);

$val=json_decode(json_encode($xmlstring),true);

return $val;

public static function arraytoxml($arr){

//陣列轉換成xml

$xml="";

foreach ($arr as $k=>$v){

$xml.='<'.$k.'>'.$v.'';

$xml.='';

return $xml;

public static function createnoncestr($strlength=32){

//建立隨機字串

$strings='abcdefghijklmnopqrstuvwxyz0123456789';

$noncestr='';

for($i=0;$i<$strlength;$i++)

$noncestr.=$strings[mt_rand(0,35)];

return $noncestr;

private function getsign($obj){

//生成簽名

foreach ($obj as $k=>$v){

$copyparameters[$k]=$v;

//步驟一:按字典排序引數

$string=$this->formatbizqueryparamap($copyparameters,false);

//步驟二:在string後面加key

$string.='key='.$this->key;

//步驟三:md5加密

$string=md5($string);

//步驟四:轉大小寫

return strtoupper($string);

private function formatbizqueryparamap($paramap,$urlencode){

//格式化引數

$buff="";

ksort($paramap);

foreach ($paramap as $k=>$v){

if($urlencode){

$v=urlencode($v);

$buff.=$k.'='.$v.'&';

return $buff;

小程式微信支付

weixinpay new weixinpay openid,orderid.res2,訂單支付 res3 goods name total price 100,tsd res2 ret weixinpay pay class weixinpay public function pay 統一下單介面...

微信小程式微信支付流程

1.小程式呼叫wx.login獲取登入憑證code wx.login 無請求引數 返回code 有效期5分鐘 wx.login 2.小程式通過code獲取openid 向介面位址 請求引數 返回引數 openid 使用者唯一標識 session key 回話金鑰 3.小程式向服務端傳送openid,...

微信小程式 微信支付模組

1 小程式登入介面 獲取openid 小程式登入 2 商戶端呼叫支付統一下單介面,返回預付單資訊 統一下單 3 小程式端顯示 客戶掃碼支付,小程式端鑑權調起支付,返回支付結果到小程式端 再次簽名 4 推送支付結果到小程式端 支付結果通知api 5 商戶查詢支付結果 查詢訂單api 注意點 1 簽名 ...