微信 h5登录 php,H5微信登录

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'];

}

//统一下单接口

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(), //随机字符串

'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": "携逸订房"}}'

);

//统一下单签名

$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);

}

//作用:产生随机字符串,不长于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转为array

*/

public function xmlToArray($xml) {

//将XML转为array

$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

return $array_data;

}

function arrayToXml($arr) {

$xml = "";

foreach ($arr as $key => $val) {

if (is_array($val)) {

$xml.="" . arrayToXml($val) . "" . $key . ">";

} else {

$xml.="" . $val . "" . $key . ">";

}

}

$xml.="";

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();//统一下单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值