您的位置:首页 > 移动开发 > 微信开发

微信小程序支付功能--优化

2017-11-09 17:45 543 查看
//增加订单
public function order_pay(Request $request)
{
$data = [
'express' => $request->input('express'),
'express_id' => $request->input('express_id'),
'openid' => $request->input('openid'),
'express_sn' => $request->input('express_sn'),
'take_number' => $request->input('take_number'),
'price' => $request->input('price'),
];
is_empty_array_data($data);
$data['out_trade_no'] =md5(uniqid() . microtime());
$order_info= OrderModel::create($data);
$order_id=$order_info->getQueueableId();
$this->getPaySign($order_id);

}

//获取支付需要的参数
public function getPaySign($order_id)
{
//$order_id = 54 ;
$order_info = OrderModel::find($order_id);
$ip = $_SERVER["REMOTE_ADDR"];
$notify_url = 'http://'.$_SERVER['HTTP_HOST'].'/api/payok';
$pay_data= PayModel::find(1);
$data = [
//'appid' => 'wxf0d27e41678c566c',
'appid'            => $pay_data->app_id,
'body'             => '快递下单消费',
//'mch_id'         => '1484855592',
'mch_id'           => $pay_data->med_id,
'nonce_str'        => md5(date('YmdHis') . time() . rand(1000, 9999)),
'notify_url'       => $notify_url,
'openid'           => $order_info->openid,
'out_trade_no'     => $order_info->out_trade_no,
'spbill_create_ip' => $ip,
'total_fee'        => ($order_info->price)*100,
'trade_type'       => 'JSAPI'
];
//形成sign
$string = $this->getSign($data).'key='.$pay_data->med_secect;
$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));
if ($result['return_code'] !== 'SUCCESS' || $result['result_code'] !== 'SUCCESS') {
return response()->json(['status'=>200,'msg'=>$result]);
}
$params = [
'appId'     => $data['appid'],
'nonceStr'  => $data['nonce_str'],
'package'   => 'prepay_id='.$result['prepay_id'],
'signType'  => 'MD5',
'timeStamp' => (string)time()
];
//拼接前端需要参数
$stringxml = $this->getSign($params).'key='.$pay_data->med_secect;
$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';
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;
}
protected function xmlDecode($xml)
{
$message = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $message;
}
protected function httpCurl($url,$data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt(
$ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: text'
)
);
$data = curl_exec($ch);
curl_close($ch);
return ($data);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: