//生成订单调用支付
$re = db('order')->insertGetId($data);//生成订单 // $this->pay($re); // die; $result = $this->getPaySign($data, 'char'); $result['id'] = $re; if($result['status'] == 200){ echo_info(['status' => 200, 'message' => '获取成功', 'data' => $result]); }else { echo_info(['status' => 500, 'message' => '请检测支付参数', 'data' => $result]); }
//获取支付需要的参数
public function getPaySign($order_info, $type = 'char',$out_trade_no = true)
{
if(!$order_info){
echo_info(['status' => -1, 'message' => '数据为空,请检查参数']);
}
$ip = $_SERVER["REMOTE_ADDR"];
$notify_url = 'https://' . $_SERVER['HTTP_HOST'] . '/index.php/home/Index/payok';
$appid = Config::get('appid');
$mch_id = Config::get('mch_id');
$shopsecret = Config::get('shopsecret');
$data = [
'appid' =>$appid,
'body' => '按摩椅支付',
'mch_id' => $mch_id,
'nonce_str' => md5(date('YmdHis') . time() . rand(1000, 9999)),
'notify_url' => $notify_url,
'openid' => $order_info['openid'],
'out_trade_no' => $order_info['orderno'],
'spbill_create_ip' => $ip,
'total_fee' => (int)((float)$order_info['total'] * 100),
'attach' => $type,
'trade_type' => 'JSAPI'
];
//形成sign
$string = $this->getSign($data) . 'key=' . $shopsecret;
$stringTemp = MD5($string);
$sign = strtoupper($stringTemp);
//准备请求prepay接口
$data['sign'] = $sign;
$str_xml = "<xml>";
foreach ($data as $key => $val) {
$str_xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
$str_xml .= "</xml>";
$prepay_url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
//请求接口并且解析xml
//$result = $this->xmlDecode($this->httpCurl($prepay_url,$str_xml));
$result = http_request_curl($prepay_url, $str_xml); //POST方式请求http
$result = xml2array($result);
if ($result['return_code'] !== 'SUCCESS' || $result['result_code'] !== 'SUCCESS') {
// return response()->json(['status'=>500,'msg'=>$result]);
$result['status'] = 500;
return $result;
}
$params = [
'appId' => $data['appid'],
'nonceStr' => $data['nonce_str'],
'package' => 'prepay_id=' . $result['prepay_id'],
'signType' => 'MD5',
'timeStamp' => (string)time(),
// 'orderno' => $data['out_trade_no'],订单号
];
//拼接前端需要参数
$stringxml = $this->getSign($params) . 'key=' . $shopsecret;
$stringxml = MD5($stringxml);
$sign2 = strtoupper($stringxml);
$pay_result['paySign'] = $sign2;
$pay_result['timeStamp'] = $params['timeStamp'];
$pay_result['nonceStr'] = $params['nonceStr'];
$pay_result['package'] = $params['package'];
$pay_result['signType'] = 'MD5';
$pay_result['status'] = 200;
// $pay_result['out_trade_no'] = $params['orderno'];//订单号
$pay_result['total_fee'] = $order_info['total'];
return $pay_result;
// return response()->json(['status'=>200,'msg'=>$pay_result]);
}
protected function getSign($arr)
{
ksort($arr);
$string = '';
foreach ($arr as $k => $v) {
if ($k != "sign" && $v != "" && !is_array($v)) {
$string .= $k . "=" . $v . "&";
}
}
return $string;
}
/****************支付成功 回调封装(改变支付状态****************/
public function open_char($array)
{
$out_trade_no = $array['out_trade_no']; //商户订单号
$transaction_id = $array['transaction_id']; //微信订单支付订单号
$result = DB::name('order')->where('orderno', $out_trade_no)->find();
// $this->pay_success($result['scene']);//短信通知
if ($result['status'] != 1) {
$datas = array(
'status' => 1,
'transid' => $transaction_id,
'pay_time' => time(),//支付时间
'start' => time() // 开启按摩椅 时间
);
$res = DB::name('order')->where('orderno', $out_trade_no)->update($datas);
}
}
//付款回调 public function payok() { error_reporting(0); define('IN_MOBILE', true); libxml_disable_entity_loader(true); $xml = '<?xml version="1.0" encoding="utf-8"?>'; $xml .= $GLOBALS['HTTP_RAW_POST_DATA']; $obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $arrayinfo = xmlToArray($obj); if (($arrayinfo['result_code'] = 'SUCCESS' || $arrayinfo['return_code'] = 'SUCCESS')) { if ($arrayinfo['attach'] == 'char') { $bool = $this->open_char($arrayinfo); } if ($bool) { return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'; } else { return '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[NO]]></return_msg></xml>'; } } else { return '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[NO]]></return_msg></xml>'; } }