test_service d3170b4d1c 1
2023-12-01 15:43:29 +08:00

1 line
2.8 KiB
PHP

<?php
namespace app\api\controller\food\market;
use app\api\controller\food\Controller;
use app\api\model\Setting as SettingModel;
use hema\wechat\Pay as WxPay;
use hema\alipay\Driver as AlipayPay;
use think\facade\Cache;
/**
* 用户充值管理
*/
class Recharge extends Controller
{
private $user;
/**
* 构造方法
*/
public function initialize()
{
parent::initialize();
$this->user = $this->getUserDetail(); // 用户信息
}
/**
* 充值支付
* $type 30=微信 40=支付宝
*/
public function pay($money,$type=30,$recharge_plan_id = 0)
{
$order_no = order_no();
$order = [
'mode' => 10, //充值
'type' => $type,//支付类型
'order_no' => $order_no,
'money' => $money,
'recharge_plan_id' => $recharge_plan_id,
'remark' => '用户充值',
'user_id' => $this->user['user_id'],
'shop_id' => $this->shop_id,
'applet_id' => $this->applet_id
];
Cache::set($order_no, $order,7200);
//H5支付
if($this->mp == 'h5'){
//微信小程序支付
if($type == 30){
$wx = new WxPay(SettingModel::getItem('wxpay',$this->applet_id));
if(!$result = $wx->h5($order_no,$money,'api/food.notify/recharge/appletid/'.$this->applet_id,'用户充值')){
return $this->renderError($wx->getError());
}
}
//支付宝小程序支付
if($type == 40){
$alipay = new AlipayPay($this->applet_id);
if(!$result = $alipay->tradeWapPay($order_no,$money,'api/food.notify/alipayRecharge/appletid/'.$this->applet_id,'h5/food/#/pages/user/vip/wallet','用户充值')){
$this->error = $alipay->getError();
return false;
}
}
}else{
//微信小程序支付
if($type == 30){
$wx = new WxPay(SettingModel::getItem('wxpay',$this->applet_id));
if(!$result = $wx->jsapi($order_no,$money,$this->user['open_id'],'api/food.notify/recharge/appletid/'.$this->applet_id,'用户充值')){
return $this->renderError($wx->getError());
}
}
//支付宝小程序支付
if($type == 40){
$alipay = new AlipayPay($this->applet_id);
if(!$result = $alipay->tradeCreate($order_no,$money,$this->user['open_id'],'api/food.notify/alipayRecharge/appletid/'.$this->applet_id,'用户充值')){
return $this->renderError($alipay->getError());
}
}
}
return $this->renderSuccess($result);
}
}