127 lines
4.1 KiB
PHP
127 lines
4.1 KiB
PHP
![]() |
<?php
|
||
|
|
||
|
namespace app\common\service;
|
||
|
|
||
|
use app\common\model\Requirement;
|
||
|
use app\common\model\RequirementOrder;
|
||
|
use app\constant\ResponseCode;
|
||
|
use think\Exception;
|
||
|
|
||
|
class RequirementOrderService
|
||
|
{
|
||
|
/**
|
||
|
* 回调地址
|
||
|
*/
|
||
|
const NOTIFY_URL = '/notify/WechatPayNotify/requirement_notify';
|
||
|
const NOTIFY_REQUIREMENT_REFUND_URL = '/notify/WechatPayNotify/requirement_refund_notify';//需求退款
|
||
|
|
||
|
/**
|
||
|
* @desc 用户支付需求金额
|
||
|
* @param $require
|
||
|
* @return array|void
|
||
|
*/
|
||
|
public function payRequirement($request)
|
||
|
{
|
||
|
try {
|
||
|
$request_data = $request->post();
|
||
|
$requirement = Requirement::where(['id' => $request_data['requirement_id']])->findOrEmpty();
|
||
|
if ($requirement->status != 2) {
|
||
|
throw new Exception('需求订单状态错误,支付失败');
|
||
|
}
|
||
|
$requirement_order = RequirementOrder::where(['requirement_trade_no' => $requirement->requirement_trade_no])->findOrEmpty();
|
||
|
|
||
|
if ($requirement_order->isEmpty()) {
|
||
|
throw new Exception('需求订单不存在');
|
||
|
}
|
||
|
if ($requirement_order->pay_status != 0) {
|
||
|
throw new Exception('订单状态校验失败,支付失败');
|
||
|
}
|
||
|
$out_trade_no = generate_order_no('R');
|
||
|
$requirement_order->save([
|
||
|
'user_id' => $request->user->id,
|
||
|
'out_trade_no' => $out_trade_no,
|
||
|
'requirement_detail' => json_encode($requirement->toArray()),
|
||
|
'amount' => $requirement->final_price
|
||
|
]);
|
||
|
|
||
|
$pay_data = [
|
||
|
'out_trade_no' => $out_trade_no,
|
||
|
// 'total_amount' => $requirement_order->amount,
|
||
|
'total_amount' => 0.01,
|
||
|
'desc' => $requirement->requirement_trade_no . '-支付',
|
||
|
'notify_url' => getenv('SERVER_DOMAIN') . self::NOTIFY_URL,//回调地址
|
||
|
'openid' => $request->user->openid,
|
||
|
];
|
||
|
$result = (new PayService())->payment($pay_data);
|
||
|
|
||
|
if (isset($result['code'])) {
|
||
|
throw new Exception($result['msg']);
|
||
|
}
|
||
|
|
||
|
return [
|
||
|
'code' => ResponseCode::SUCCESS,
|
||
|
'data' => $result,
|
||
|
];
|
||
|
} catch (Exception $e) {
|
||
|
return [
|
||
|
'code' => ResponseCode::FAIL,
|
||
|
'msg' => $e->getMessage()
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @desc 需求退款
|
||
|
* @param $request
|
||
|
* @return array|void
|
||
|
*/
|
||
|
public function requirementRefund($request)
|
||
|
{
|
||
|
try {
|
||
|
//查找订单
|
||
|
$order = RequirementOrder::where(['out_trade_no' => $request['out_trade_no']])->findOrEmpty();
|
||
|
if ($order->isEmpty()) {
|
||
|
throw new Exception('未找到订单信息');
|
||
|
}
|
||
|
if ($order->pay_status != 1) {
|
||
|
throw new Exception('订单状态错误,退款失败');
|
||
|
}
|
||
|
if ($order->is_refund) {
|
||
|
throw new Exception('订单已退款');
|
||
|
}
|
||
|
|
||
|
$refund_trade_no = generate_order_no('T');
|
||
|
$order->save([
|
||
|
'refund_trade_no' => $refund_trade_no,
|
||
|
'refund_amount' => $order->amount,
|
||
|
]);
|
||
|
|
||
|
$refund_data = [
|
||
|
'out_trade_no' => $order->out_trade_no,
|
||
|
'out_refund_no' => $refund_trade_no,
|
||
|
'refund_desc' => '正常退款',
|
||
|
'notify_url' => getenv('SERVER_DOMAIN') . self::NOTIFY_REQUIREMENT_REFUND_URL,
|
||
|
'total' => $order->amount,
|
||
|
'refund_amount' => $order->amount,
|
||
|
];
|
||
|
|
||
|
$result = (new PayService())->refundPayment($refund_data);
|
||
|
if(isset($result['code'])){
|
||
|
throw new Exception($result['msg']);
|
||
|
}
|
||
|
|
||
|
return [
|
||
|
'code' => ResponseCode::SUCCESS,
|
||
|
'msg' => '已申请退款',
|
||
|
];
|
||
|
|
||
|
} catch (Exception $e) {
|
||
|
return [
|
||
|
'code' => ResponseCode::FAIL,
|
||
|
'msg' => $e->getMessage()
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|