68 lines
2.0 KiB
PHP
68 lines
2.0 KiB
PHP
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\common\pay\wx;
|
|
|
|
|
|
|
|
|
|
|
|
class WechatPay
|
|
|
|
{
|
|
|
|
public $totalFee;
|
|
|
|
public $outTradeNo;
|
|
|
|
public $attach;
|
|
|
|
public $tradeType;
|
|
|
|
public $body;
|
|
|
|
public $notifyUrl;
|
|
|
|
public function __construct($totalFee = 0, $outTradeNo = '', $attach = '', $tradeType = '', $body = '', $notifyUrl = '')
|
|
|
|
{
|
|
|
|
$this->totalFee = $totalFee * 100;
|
|
|
|
$this->outTradeNo = $outTradeNo;
|
|
|
|
$this->attach = $attach;
|
|
|
|
$this->tradeType = $tradeType;
|
|
|
|
$this->body = $body;
|
|
|
|
$this->notifyUrl = empty($notifyUrl) ? Wechat::$notifyUrl : $notifyUrl;
|
|
|
|
}
|
|
|
|
public function setPayParams()
|
|
|
|
{
|
|
|
|
$ip = request()->ip();
|
|
|
|
return [
|
|
|
|
'appid' => Wechat::$appid,
|
|
|
|
'mch_id' => Wechat::$mchId,
|
|
|
|
'spbill_create_ip' => $ip,
|
|
|
|
'nonce_str' => md5(mt_rand(1000, 9999)),
|
|
|
|
'sign_type' => 'MD5',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $subData
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function toPay()
|
|
|
|
{
|
|
|
|
$params = $this->dealPayParams();
|
|
|
|
$data = $this->setPayParams();
|
|
|
|
$params = $data + $params;
|
|
|
|
$sign = Wechat::getSign($params, Wechat::$apikey);
|
|
|
|
$params['sign'] = $sign;
|
|
|
|
// 请求API
|
|
|
|
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
|
|
|
|
$paramsXml = Wechat::arr2xml($params);
|
|
|
|
$curlResult = Wechat::postXmlCurl($url, $paramsXml);
|
|
|
|
//返回值转数组
|
|
|
|
$xmlArr = Wechat::xml2arr($curlResult);//pre($xmlArr);
|
|
|
|
return $xmlArr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dealPayParams()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'total_fee' => $this->totalFee,
|
|
|
|
'attach' => $this->attach,
|
|
|
|
'out_trade_no' => $this->outTradeNo,
|
|
|
|
'trade_type' => $this->tradeType,
|
|
|
|
'notify_url' => $this->notifyUrl,
|
|
|
|
'body' => $this->body,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} |