小程式呼叫API發起微信支付

2021-09-05 10:45:22 字數 3614 閱讀 9471

下面查詢 $orderdetails返回結果

乙個**:totle

乙個內部訂單號:order_no

內部定義的支付型別(自己想怎麼取就怎麼取,可要可不要):pay_type_text

/**

* auth:leishaofa

* date:2018-12-26

*/public function wxpay()

if($orderdetails['uid'] !== (int)$uid)

if($orderdetails['store_id'] !== (int)$storeid)

//呼叫統一下單介面

'mch_id'=>$mch_id,

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

'sign_type'=>'hmac-sha256',

'body'=>$orderdetails['order_no'].$orderdetails['pay_type_txt'],

'attach'=>json_encode(array('order_no'=>$orderdetails['order_no'],'pay_type'=>$orderdetails['pay_type_txt']),json_unescaped_unicode),

'out_trade_no'=>$orderdetails['order_no'],

'fee_type'=>'cny',

'total_fee'=>intval($orderdetails['total'] * 100),

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

'time_start'=>date("ymdhis"),

'time_expire'=>date("ymdhis", time() + 600),

'notify_url'=>"",

'trade_type'=>$type,

'product_id'=>'',

'limit_pay'=>'no_credit',

'openid'=>$openid,

'receipt'=>'',

'scene_info'=>'',//json_encode(array("store_info"=>array("id"=>"","name"=> "","area_code"=> "編碼","address"=>"位址"))),

);$data=array_filter($data);

ksort($data);

$sign= trim($this->tourlparams($data), "&");

$sign = $sign.'&key='.$key;

$sign = hash_hmac("sha256",$sign ,$key);

$data['sign']=strtoupper($sign);

$config=array('mch_id'=>$mch_id,'cert'=>'','key'=>'');

$xml = $this->toxml($data);

$response = $this->postxmlcurl($config, $xml, '', false, 6);

if(!$response)

//將xml轉為array

libxml_disable_entity_loader(true);

$resultarray = json_decode(json_encode(******xml_load_string($response, '******xmlelement', libxml_nocdata)), true);

if($resultarray['return_code'] != 'success')

}self::returnmsg(401, '失敗',$resultarray);

}if(!array_key_exists('sign', $resultarray))

self::returnmsg(0, '成功',$resultarray);

}/**

* auth:leishaofa

* date:2018-12-26

* parame:格式化引數格式化成url引數

*/public function tourlparams($data)

}$buff = trim($buff, "&");

return $buff;}/*

* auth:leishaofa

* date:2018-12-26

* parame:生成隨機字串

*/private function createnoncestr($length = 32)

return $str;

}/**

* 將array轉為xml

* @param wxpayconfiginte***ce $config 配置物件

* @param string $data array

* @throws wxpayexception

*/public function toxml($data)

$xml = "";

foreach ($data as $key=>$val)

else

}$xml.="";

return $xml;

}/**

* 以post方式提交xml到對應的介面url

* @param wxpayconfiginte***ce $config 配置物件

* @param string $xml 需要post的xml資料

* @param string $url url

* @param bool $usecert 是否需要證書,預設不需要

* @param int $second url執行超時時間,預設30s

* @throws wxpayexception

*/private static function postxmlcurl($config, $xml, $url, $usecert = false, $second = 30)

curl_setopt($ch,curlopt_url, $url);

//證書檔案請放入伺服器的非web目錄下

//設定header

curl_setopt($ch, curlopt_header, false);

//要求結果為字串且輸出到螢幕上

curl_setopt($ch, curlopt_returntransfer, true);

if($usecert == true)

//post提交方式

curl_setopt($ch, curlopt_post, true);

curl_setopt($ch, curlopt_postfields, $xml);

//執行curl

$data = curl_exec($ch);

//返回結果

if($data) else

}

微信小程式PHP 微信支付介面呼叫

小程式端 伺服器端 準備資料 都在後台系統中可查到 mch id 12 02 商戶號 key lk afk12 openid trim post openid out trade no mch id.time out trade no trim post out trade no body 答題獎金...

小程式呼叫微信支付返回錯誤

網上搜了一些資訊,發現乙個很讓人無奈的事情,無論支付缺少啥引數,都提示 缺少total fee 一般以下情況會報錯 1.後台返回的package欄位錯誤 wx.requestpayment中package引數必須是package prepay id wx 格式的 2.訂單編號重複 乙個訂單編號只能用...

小程式微信支付

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