Files

134 lines
3.9 KiB
PHP

<?php
namespace app\api\controller\order;
use app\common\controller\Api;
use app\common\model\order\Order;
use think\Db;
use app\common\exception\Exception;
use app\admin\model\config\Pay as PayConfigModel;
use addons\epay\library\Service as PayService;
/**
* 支付
* 暂时废弃
*/
class Pay extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['*'];
/**
* 支付成功回调
*/
public function notifyx()
{
$pay_type = $this->request->param('pay_type');
$platform = $this->request->param('platform');
//验签
$pay = \addons\epay\library\Service::checkNotify($pay_type);
if (!$pay) {
echo '签名错误';
return;
}
$data = $pay->verify();
$pay_fee = $pay_type == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
$out_trade_no = $data['out_trade_no'];
//你可以在此编写订单逻辑
$order = Order::where('order_no', $out_trade_no)->find();
if (!$order || $order->status != \app\common\constant\order\Status::STATUS_UNPAID) {
// 订单不存在,或者订单已支付
return $pay->success()->send();
}
Db::transaction(function () use ($order, $data, $pay_type, $platform, $pay_fee) {
$notify = [
'orderid' => $data['out_trade_no'],
'transaction_id' => $pay_type == 'alipay' ? $data['trade_no'] : $data['transaction_id'],
'notify_time' => date('Y-m-d H:i:s', strtotime($data['time_end'])),
'buyer_email' => $pay_type == 'alipay' ? $data['buyer_logon_id'] : $data['openid'],
'pay_type_json' => json_encode($data->all()),
'pay_fee' => $pay_fee,
'pay_type' => $pay_type // 支付方式
];
$order->paySuccess($order, $notify);
});
return $pay->success()->send();
}
/**
* 拉起支付
*/
public function handle()
{
$orderNo = $this->request->post('order_no');
$pay_type = $this->request->post('pay_type');
$platform = request()->header('platform');
$order = Order::where([
'order_no'=>$orderNo,
'status'=>'unpaid'
])->find();
if (!$order) {
throw new Exception("订单不存在");
}
if (!$pay_type || !in_array($pay_type, ['wechat', 'douyin', 'alipay', 'balance'])) {
throw new Exception("支付类型不能为空");
}
$result = PayService::submitOrder($order['real_price'],$order['order_no'],$pay_type,$order);
$this->success("操作成功",$result);
}
// 余额支付
public function balancePay ($order, $type, $method) {
$order = Db::transaction(function () use ($order, $type, $method) {
$total_fee = $order->total_fee;
// 扣除余额
$user = User::info();
if (is_null($user)) {
// 没有登录,请登录
$this->error(__('Please login first'), null, 401);
}
User::moneyReduce($user->id, $total_fee, 'balance_pay', $order->id, [
'order_id' => $order->id,
'order_sn' => $order->order_sn,
]);
// 支付后流程
$notify = [
'order_sn' => $order['order_sn'],
'transaction_id' => '',
'notify_time' => date('Y-m-d H:i:s'),
'buyer_email' => $user->id,
'pay_fee' => $order->total_fee,
'pay_type' => 'balance' // 支付方式
];
$notify['pay_type_json'] = json_encode($notify);
$order->paySuccess($order, $notify);
return $order;
});
$this->success('支付成功', $order);
}
}