微信開(kāi)發(fā)SDK:文中用的是php_sdk_v3.0.9 :https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
import("wxpay.lib.AppPay#Config", EXTEND_PATH, ".php");
import("wxpay.lib.WxPay#Api", EXTEND_PATH, ".php");
//$this->weixinpay();
$config = new \AppPayConfig();
//訂單號(hào)
$appId = $config->GetAppId();
$key = $config->GetKey();
$money = 0.01 * 100;
$orderSn = 'M201810241754107557';//訂單號(hào);
$curTime = time();
$input = new \WxPayUnifiedOrder();
$input->SetBody("用戶紅包");
$input->SetOut_trade_no($orderSn);
$input->SetTotal_fee($money);
$input->SetTrade_type("APP");
$unifiedOrder = \WxPayApi::unifiedOrder($config, $input);
if ($unifiedOrder['result_code'] == 'SUCCESS' $unifiedOrder['return_code'] == 'SUCCESS') {
/*$unifiedOrder 組成:
* ["appid"] => string(18) "wx2xxxx49"
["mch_id"] => string(10) "1xxxxxx01"
["nonce_str"] => string(16) "1GnEUXTuTcFtJVtb"
["prepay_id"] => string(36) "wx241933xxx68fa24ece70611692955"
["result_code"] => string(7) "SUCCESS"
["return_code"] => string(7) "SUCCESS"
["return_msg"] => string(2) "OK"
["sign"] => string(32) "7903xxxxx4710E460CB156"
["trade_type"] => string(3) "APP"*/
$curTime = time();
// 第一部分
$wxpayResult = new \WxPayResults();
$wxpayResult->SetData('appid', $unifiedOrder['appid']);
$wxpayResult->SetData('partnerid', $unifiedOrder['mch_id']);
$wxpayResult->SetData('prepayid', $unifiedOrder['prepay_id']);
$wxpayResult->SetData('noncestr', $unifiedOrder['nonce_str']);
$wxpayResult->SetData('timestamp',$curTime);
$wxpayResult->SetData('package',"Sign=WXPay");
$wxpayResult->SetData('sign',"");
$wxpayResult->SetSign($config);
$data = $wxpayResult->GetValues();
// 第二部分
$data['appid'] = $unifiedOrder['appid'];
$data['partnerid'] = $unifiedOrder['mch_id'];
$data['prepayid'] = $unifiedOrder['prepay_id'];
$data['noncestr'] = $unifiedOrder['nonce_str'];;
$data['timestamp'] = $curTime;
$data['package'] = "Sign=WXPay";
$data['sign'] = $this->wxappPaySign($key, $data);//$unifiedOrder['sign'];//簽名,具體簽名方案參見(jiàn)微信公眾號(hào)支付幫助文檔;
$this->returnSuccess($data);
} else {
Log::write(var_export($unifiedOrder, true));
$this->returnError(300, '微信支付失敗');
}
private function wxappPaySign($key,$parameters)
{
$sign = md5(sprintf("appid=%snoncestr=%spackage=%spartnerid=%sprepayid=%stimestamp=%skey=%s",
$parameters['appid'],
$parameters['noncestr'],
$parameters['package'],
$parameters['partnerid'],
$parameters['prepayid'],
$parameters['timestamp'],
$key));
return strtoupper($sign);
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開(kāi)發(fā)技巧匯總》、《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP中json格式數(shù)據(jù)操作技巧匯總》及《PHP針對(duì)XML文件操作技巧總結(jié)》