Jsapi支付
This commit is contained in:
@@ -22,7 +22,7 @@ class Payment extends Controller
|
||||
* APP支付
|
||||
*/
|
||||
public function app_pay() {
|
||||
$type = input('type', 0); //1-微信 2-支付宝 4-通联支付宝 5-通联微信 6-苹果支付
|
||||
$type = input('type', 0); //1-微信 2-支付宝 4-通联支付宝 5-通联微信 6-苹果支付 7-JSAPI支付
|
||||
$user_id = input('user_id', 0);
|
||||
$money = input('money', 0);
|
||||
$coin = input('coin', 0);
|
||||
@@ -430,4 +430,62 @@ class Payment extends Controller
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//微信JSAPI支付
|
||||
public function pay_jsapi(){
|
||||
$user_code = input('user_code');
|
||||
$money = input('money', 0);
|
||||
$coin = $money * get_system_config_value('rmb_coin_ratio');
|
||||
|
||||
if (!$user_code) {
|
||||
return V(0, '请选择充值用户', null);
|
||||
}
|
||||
if (!is_numeric($money) || floor($money) != $money || $money <= 0 || $money > 5000) {
|
||||
return V(0, '请选择正确的充值金额', null);
|
||||
}
|
||||
|
||||
|
||||
$user_id = db::name('user')->where(["user_code" => $user_code])->value('id');
|
||||
|
||||
//获取用户的手机号
|
||||
$user_phone = db::name('user')->where(["id" => $user_id])->value('mobile');
|
||||
if(!$user_phone){
|
||||
return V(0, '请先绑定手机号!', null);
|
||||
}
|
||||
//获取用户的实名信息
|
||||
$real_name = db::name('user_auth')->where(["mobile" => $user_phone,'is_real' => 1])->find();
|
||||
if(!$real_name){
|
||||
return V(0, '请先实名认证', null);
|
||||
}
|
||||
//获取用户的年龄
|
||||
if(!getAgeId($real_name['card_id'])){
|
||||
return V(0, '未成年不可充值!', null);
|
||||
}
|
||||
|
||||
$title = '充值到app用户'. $user_code ."余额!";
|
||||
|
||||
$order_number = $this->createOrderSn();
|
||||
$data['order_sn'] = $order_number;
|
||||
$data['money'] = $money;
|
||||
$data['user_id'] = $user_id;
|
||||
$data['pay_type'] = 7; //JSAPI支付
|
||||
$data['coin'] = $coin;
|
||||
$data['createtime'] = time();
|
||||
$data['remarke'] = $title;
|
||||
|
||||
$re = db::name('vs_user_recharge')->insert($data);
|
||||
if (!$re) {
|
||||
return V(0, '充值失败', null);
|
||||
}
|
||||
|
||||
//引用微信sdk
|
||||
Loader::import('WxPay.WxPay', EXTEND_PATH, '.php');
|
||||
$wx = new \WxPay();
|
||||
// $result = $wx->WxPayMp($data);
|
||||
$result = $wx->WxPayJsapi($data);
|
||||
return V(1, 'app支付', $result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use addons\epay\library\Service;
|
||||
use think\Log;
|
||||
|
||||
class WxPay
|
||||
{
|
||||
@@ -475,4 +477,97 @@ class WxPay
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取支付参数 (统一下单)
|
||||
* @ApiMethod (POST)
|
||||
* @ApiRoute (/api/pay/create)
|
||||
*/
|
||||
public function WxPayMp($d)
|
||||
{
|
||||
$total_fee = abs(floatval($d['money'])) * 100;// 微信支付 单位为分
|
||||
// 1. 接收参数
|
||||
|
||||
$order_no = $d['order_sn'];
|
||||
$amount = $total_fee;
|
||||
$openid = $d['openid'];
|
||||
$pay_type = 'wechat'; // wechat 或 alipay
|
||||
|
||||
if (!$order_no || !$amount || !$openid) {
|
||||
$this->error('参数不完整');
|
||||
}
|
||||
|
||||
// 3. 生成支付参数
|
||||
try {
|
||||
$params = [
|
||||
'amount' => sprintf('%.2f', $amount), // 单位:元
|
||||
'orderid' => $order_no,
|
||||
'type' => $pay_type,
|
||||
'title' => '商品支付-' . $order_no,
|
||||
'notifyurl' => url('/api/pay/notify', '', true, true), // 异步通知
|
||||
'returnurl' => url('/index/index', '', true, true), // 支付后跳转
|
||||
'method' => 'mp', // mp=公众号支付, miniapp=小程序支付
|
||||
'openid' => $openid, // 必须
|
||||
];
|
||||
|
||||
// 4. 调用 epay 插件统一下单
|
||||
$result = Service::submitOrder($params);
|
||||
|
||||
// 5. 返回前端所需参数
|
||||
$this->success('成功', [
|
||||
'order_no' => $order_no,
|
||||
'pay_params' => $result, // 插件返回的支付参数
|
||||
'timestamp' => time(),
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('支付下单失败: ' . $e->getMessage());
|
||||
$this->error('支付创建失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付Jsapa
|
||||
* @param string $data 业务参数 body out_trade_no total_fee
|
||||
* @param string $data ['out_trade_no'] 订单号 必填
|
||||
* @param string $data ['total_fee'] 订单金额 必填
|
||||
* @param string $data ['body'] 订单详情 必填
|
||||
* @return $response 返回app所需字符串
|
||||
*/
|
||||
public function WxPayJsapi($d) {
|
||||
$total_fee = abs(floatval($d['money'])) * 100;// 微信支付 单位为分
|
||||
|
||||
$ip = $this->get_client_ip();
|
||||
if ($ip == '::1')
|
||||
$ip = '1.1.1.1';
|
||||
|
||||
$data ["appid"] = \think\Env::get('wechatpay.appid');
|
||||
$data ["mch_id"] = \think\Env::get('wechatpay.mch_id');
|
||||
$data ["nonce_str"] = $this->getRandChar(32);
|
||||
$data ["body"] = $d['remarke'];
|
||||
$data ["out_trade_no"] = $d['order_sn'];
|
||||
$data ["total_fee"] = $total_fee;
|
||||
$data ["spbill_create_ip"] = $ip;
|
||||
$data ["notify_url"] = get_system_config_value("web_site")."/api/Payment/notify_wx";
|
||||
$data ["trade_type"] = "JSAPI";
|
||||
$data ["openid"] = $d['openid'];// 必须
|
||||
|
||||
$s = $this->getSign($data);
|
||||
$data ["sign"] = $s;
|
||||
|
||||
$xml = $this->arrayToXml($data);
|
||||
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
|
||||
$response = $this->postXmlCurl($xml, $url);
|
||||
$re = $this->xmlstr_to_array($response);
|
||||
|
||||
if ($re ['return_code'] == 'FAIL') {
|
||||
return array(-1, '订单创建失败!'.\think\Env::get('wechatpay.api_v3_key'), $re['return_msg']);
|
||||
}
|
||||
// 二次签名
|
||||
$reapp = $this->getOrder($re['prepay_id']);
|
||||
return $reapp;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user