cmbYouku_Api/app/service/RechargeService.php

136 lines
4.9 KiB
PHP
Raw Normal View History

2024-07-01 15:57:07 +08:00
<?php
namespace app\service;
use app\model\Order;
use app\model\Sign;
use app\service\util\BlueBrothersClientUtil;
use BlueBrothers\Openapi\Util;
use think\facade\Log;
class RechargeService
{
private $merchantId;
private $secretKey;
public function __construct()
{
$this->merchantId = env('blue_brother.merchant_id', 23329);
$this->secretKey = env('blue_brother.merchant_key', '8db16e8cc8363ed4eb4c14f9520bcc32');
}
/**
* 直连天下提交充值处理
* @param string $orderNumber
* @return true
* @throws \BlueBrothers\Openapi\OpenapiException
* @throws \app\exception\BusinessException
*/
public static function rechargeOrder(string $orderNumber)
{
$order = Order::getByOrderNumber($orderNumber);
if ($order['pay_status'] == Order::PAY_STATUS_PAID && $order['order_status'] == Order::STATUS_WAIT_RECHARGE) { // 直连天下充值
$rechargeOrder = [
'outTradeNo' => $order["order_number"],// 合作商系统内部订单号,同一商户下不可重复, 同微信、支付宝的out_trade_no类似
'productId' => $order["product_id"],// 商品编码
'number' => 1,
'notifyUrl' => config('cmb.recharge_notify_url'),// 异步通知地址
'rechargeAccount' => $order["account"],// 手机号,非必传
'accountType' => 1,// 充值账号类型,1手机号2QQ号0其他
];
$res = BlueBrothersClientUtil::getClient()->RechargeOrder($rechargeOrder);
if ($res['code'] == 2000) {
$order->order_status = Order::STATUS_RECHARGE_ING;
$order->save();
}
}
return true;
}
/**
* 充值回调
* @param $params
* @return string
* @throws \BlueBrothers\Openapi\OpenapiException
* @throws \app\exception\BusinessException
*/
public function rechargeNotify($params)
{
Log::info('充值回调:' . json_encode($params));
$sign = Util::getSign($this->secretKey, $params);
if ($params['sign'] != $sign) {
return '';
}
$orderNumber = $params['outTradeNo'];
$status = $params['status'];
$order = Order::getByOrderNumber($orderNumber);
if ($order->order_status == Order::STATUS_RECHARGE_SUCCESS) {
return 'success';
}
switch ($status) {
case '01':
$orderStatus = Order::STATUS_RECHARGE_SUCCESS;
break;
case '03':
$orderStatus = Order::STATUS_RECHARGE_FAIL;
break;
default:
$orderStatus = Order::STATUS_RECHARGE_ING;
break;
}
if ($status == '01' && $order->order_status == Order::STATUS_RECHARGE_ING) { // 充值成功更新协议扣款时间
$sign = Sign::getByAgreementId($order->agreement_id);
$sign->agree_recharge_time = strtotime(date('Y-m-d'));
$sign->save();
}
$order->order_status = $orderStatus;
$order->save();
if ($status == '03' && $order->refund_status == 0) { // 充值失败,退款
CmbService::refund($orderNumber);
}
if ($status == '04') { // 执行手动拉取
$this->queryRechargeOrder($orderNumber);
}
return 'success';
}
/**
* 主动拉取充值订单状态
* @param string $orderNumber
* @return string|true
* @throws \BlueBrothers\Openapi\OpenapiException
* @throws \app\exception\BusinessException
*/
public function queryRechargeOrder(string $orderNumber)
{
$res = BlueBrothersClientUtil::getClient()->RechargeQuery($orderNumber);
if ($res['code'] !== '0000') {
return '查询失败';
}
$status = $res['status'];
switch ($status) {
case '01':
$orderStatus = Order::STATUS_RECHARGE_SUCCESS;
break;
case '03':
$orderStatus = Order::STATUS_RECHARGE_FAIL;
break;
default:
$orderStatus = Order::STATUS_RECHARGE_ING;
break;
}
$order = Order::getByOrderNumber($orderNumber);
if ($order->order_status == Order::STATUS_RECHARGE_SUCCESS) {
return true;
}
if ($status == '01' && $order->order_status == Order::STATUS_RECHARGE_ING) { // 充值成功更新协议扣款时间
$sign = Sign::getByAgreementId($order->agreement_id);
$sign->agree_recharge_time = strtotime(date('Y-m-d'));
$sign->save();
}
$order->order_status = $orderStatus;
$order->save();
if ($status == '03' && $order->refund_status == 0) {
CmbService::refund($orderNumber);
}
return true;
}
}