jingcai-php/app/Http/Controllers/Api/Customer/WalletController.php

252 lines
7.7 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Api\Customer;
use App\Enums\PayState;
use App\Enums\PayType;
use App\Enums\WithdrawState;
use App\Exceptions\JingCaiException;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerRecharge;
use App\Model\Customer\CustomerWithdraw;
use App\Model\Order;
use App\Model\Seller\Shop;
use App\Providers\Pay\AgentAlipay;
use App\Providers\Pay\Alipay;
use App\Service\CustomerWalletService;
use App\Service\OrderService;
use App\Service\RechargeService;
use App\Service\SellerWalletService;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class WalletController extends BaseController
{
/**
* @api {POST} /api/customer/wallet/pay_order 支付-订单支付
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Float} order_sn 订单编号
* @apiParamExample {json} 请求示例
* {
* "order_sn":"xxxxx",
* }
*
* @apiSuccessExample {json} 返回结果
* { // 二维码
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function payOrder(Request $request, OrderService $orderService)
{
$orderSn = $request->input('order_sn');
$order = Order::sn($orderSn)->first();
if (!$order) {
JingCaiException::throwJingcai('订单号错误');
}
ThrowException::isTrue($order->customer_id != $this->customerId(), '非法操作');
if ($order->pay_state != PayState::UNPAID) {
JingCaiException::throwJingcai('订单错误,请重新下单!');
}
/** @var Customer $customer */
$customer = Customer::find($this->customerId());
$orderMoney = $order->getPayMoney();
if (!bc_gte($customer->balanceActive(), $orderMoney)) {
JingCaiException::throwJingcai('用户余额不足,请选择其他支付方式');
}
$orderService->setOrderPaySuccess($order);
return $this->jsonSuccess([]);
}
/**
* @api {POST} /api/customer/wallet/recharge 支付-充值(获取充值单号)
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Float} pay_money 支付金额
* @apiParam {String} pay_type 支付方式
* @apiParam {String} [order_sn] 订单号
* @apiParamExample {json} 请求示例
* {
* "order_sn":"xxxx",
* "pay_type":"alipay",
* "pay_money":2.0,
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": [
* "code" => "10000"
* "msg" => "Success"
* "out_trade_no" => "P2012321321432431322"
* "qr_code" => "https://qr.alipay.com/bax03864rcibb2pkssnb3082" // 二维码链接
* ]
* }
*/
public function recharge(Request $request, RechargeService $rechargeService)
{
$data = $request->all();
/** @var CustomerRecharge $recharge */
$recharge = $rechargeService->customerCreateRecharge($this->customer(), $data);
$shopPayChannel = $recharge->shopPayChannel;
$token = $shopPayChannel->ali_face_app_auth_token;
ThrowException::isTrue(!$token, '当前支付渠道繁忙,请切换支付渠道');
$ali = new AgentAlipay();
$money = $recharge->pay_money;
$subject = '充值';
$res = $ali->faceToFaceTradePreCreate($recharge->recharge_sn, $money, $subject, $token);
return $this->jsonSuccess($res);
}
/**
* @api {POST} /api/customer/wallet/redirect_pay 支付-跳转到第三方支付
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} recharge_sn 充值订单编号
* @apiParamExample {json} 请求示例
* {
* "pay_type":"alipay",
* "pay_money":2.0,
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": "xxxxxx" // 此处应该是个form
* }
*/
public function redirectPay(Request $request, RechargeService $rechargeService)
{
$rechargeSn = $request->input('recharge_sn');
$form = $rechargeService->customerRedirectPay($this->customer(), $rechargeSn);
return $this->jsonSuccess($form);
}
/**
* @api {POST} /api/customer/wallet/recharge_query 支付-查询充值结果
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {String} recharge_sn 充值订单编号
* @apiParamExample {json} 请求示例
* {
* "pay_type":"alipay",
* "pay_money":2.0,
* }
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": "xxxxxx" // 此处应该是个form
* }
*/
public function rechargeQuery(Request $request, RechargeService $rechargeService)
{
$rechargeSn = $request->input('recharge_sn');
$recharge = $rechargeService->customerRechargeQuery($this->customer(), $rechargeSn);
return $this->jsonSuccess($recharge);
}
/**
* @api {POST} /api/customer/wallet/withdraw_money 我的-提现
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiParam {Int} type 1支付宝2银行卡
* @apiParam {Float} money 提现金额
* @apiParam {String} ali_account 支付宝账号
* @apiParam {String} bank_no 银行卡号
* @apiParam {String} bank_area 银行卡所在地
* @apiParam {String} bank_master 开户行
* @apiParam {String} password_pay 支付密码
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function withdrawMoney(Request $request, CustomerWalletService $walletService)
{
$walletService->withdrawMoney($this->customer(), $request->all());
return $this->jsonSuccess();
}
/**
* @api {GET} /api/customer/wallet/withdraw_last 我的-最近提现信息
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "id":1,
* "shop_id":2000,
* "customer_id":100,
* "freeze_id":1,
* "money":"1.1100",
* "type":1, // 1支付宝、2银行卡
* "ali_account":"sssss",
* "bank_no":"",
* "bank_area":"",
* "bank_master":"",
* "bank_branch":"",
* "state":1,
* "seller_id":0,
* "seller_remark":"",
* "remark_at":null,
* "created_at":"2023-10-09 20:08:07",
* "updated_at":"2023-10-09 20:08:07",
* "deleted_at":null,
* }
* }
*/
public function withdrawLast(Request $request) {
$first = CustomerWithdraw::where('customer_id', $this->customerId())
->orderBy('id', 'desc')
->first();
return $this->jsonSuccess($first);
}
/**
* @api {GET} /api/customer/wallet/withdraw_list 我的-提现列表
* @apiVersion 0.1.0
* @apiGroup 客户端
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function withdrawList(Request $request) {
$size = $request->input('size');
$list = CustomerWithdraw::with('customer:id,name,nickname,avatar')
->where('customer_id', $this->customerId())
->orderBy('id', 'desc')
->paging($size);
return $this->jsonSuccess($list);
}
}