<?php
namespace App\Service\waph5;
/*
-
小程序微信支付
*/class WapPayService {
protected $appid;
protected $mch_id;
protected $key;
protected $openid;
protected $total_fee;
protected $out_trade_no;
protected $body;
function init(array $data) {
$this->appid = '';
$this->mch_id = '';
$this->key = '';
$this->total_fee = $data['total_fee'];
$this->out_trade_no = $data['out_trade_no'];
$this->body = $data['remark'];
}
//統(tǒng)一下單接口
public function unifiedOrder() {
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$parameters = array(
'appid' => $this->appid, //小程序ID
'mch_id' => $this->mch_id, //商戶號
'nonce_str' => $this->createNoncestr(), //隨機(jī)字符串
'body' => $this->body, //商品描述
'out_trade_no' => $this->out_trade_no, //商戶訂單號
'total_fee' => $this->total_fee, //floatval($this->total_fee * 100), //總金額 單位 分
'spbill_create_ip' => $this->getIp(), //終端IP
'notify_url' => \App::config()->get('app', 'host') . 'api/onNotify', //通知地址
'trade_type' => 'MWEB', //交易類型
'scene_info' => '{"h5_info": {"type":"Wap","wap_url": "https://xieyi.gzyoufa.com","wap_name": "攜逸訂房"}}'
);
//統(tǒng)一下單簽名
$parameters['sign'] = $this->getSign($parameters,$this->key);
// print_r($parameters);die;
$xmlData = $this->arrayToXml($parameters);
$return = $this->http($url, 'POST', $xmlData);
return $this->xmlToArray($return);
}
//作用:產(chǎn)生隨機(jī)字符串糕韧,不長于32位
private function createNoncestr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str.= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//作用:生成簽名
private function getSign($params, $appkey, $separator = '&') {
if (isset($params['sign'])) {
unset($params['sign']);
}
ksort($params);
$str = "";
foreach ($params as $key => $value) {
if (empty($value) && $value !== 0) {
continue;
}
$str .= $key . "=" . $value . $separator;
}
$str = trim($str, $separator);
$raw = $str . $separator .'key='.$appkey;
return \strtoupper(md5($raw));
}
/**
* 作用:將xml轉(zhuǎn)為array
*/
public function xmlToArray($xml) {
//將XML轉(zhuǎn)為array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
function arrayToXml($arr) {
$xml = "<root>";
foreach ($arr as $key => $val) {
if (is_array($val)) {
$xml.="<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
} else {
$xml.="<" . $key . ">" . $val . "</" . $key . ">";
}
}
$xml.="</root>";
return $xml;
}
public function http($url, $method = 'GET', $postfields = null, $headers = array(), $debug = false) {
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
$this->postdata = $postfields;
}
break;
}
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ci);
$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
if ($debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo '=====info=====' . "\r\n";
print_r(curl_getinfo($ci));
echo '=====$response=====' . "\r\n";
print_r($response);
}
curl_close($ci);
return $response;
}
public function getIp() {
//獲取用戶IP
if (getenv('HTTP_CLIENT_IP')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('REMOTE_ADDR')) {
$onlineip = getenv('REMOTE_ADDR');
} else {
$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
}
return $onlineip;
}
}
例子:
$wapService = new WapPayService();
$wapService->init($paymentConf);
$return = $wapService->unifiedOrder();//統(tǒng)一下單