//执行支付
public function wechat($code,$title,$amount,$order)
{
//$code = '';前端传过来
$openid = '';//用户的openid
$type = 'wechat';//支付方式
$method = 'miniapp';//支付类型
$appid = ''; //平台获取
$secret = ''; //平台获取
if ($info = $this->userOpenId(appid,$secret,$code)){
//用户的openid
$openid = $info['openid'];
}
if ($openid == ''){
return $this->err('未获取到用户openid');
}
if (!$amount || $amount < 0) {
return $this->err("支付金额必须大于0");
}
if (!$type || !in_array($type, ['alipay', 'wechat'])) {
return $this->err("支付类型不能为空");
}
if (in_array($method, ['miniapp', 'mp']) && !$openid) {
return $this->err("openid不能为空");
}
//回调链接
$notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
$returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $order;
$response = Service::submitOrder('0.01', $order, $type, $title, $notifyurl, $returnurl, $method, $openid);
$array = json_decode($response,true);
$array['order'] = $order;
return $this->succ('成功',$array);
}
//获取用户的openId
public function userOpenId($appid,$secret,$code)
{
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
if (curl_errno($curl)) {
return false;
}
curl_close($curl);
$data = json_decode($output,true);
if (isset($data['openid'])){
return $data;
}
return false;
}