86 lines
2.7 KiB
PHP
86 lines
2.7 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace app\common\library;
|
||
|
||
use app\common\model\MallItem;
|
||
use app\common\model\MallOrder;
|
||
use GuzzleHttp\Client;
|
||
|
||
/**
|
||
* 红利订单调用 PlayX bonus/grant(与定时任务、后台手动推送共用)
|
||
*/
|
||
final class MallBonusGrantPush
|
||
{
|
||
/**
|
||
* @return array{ok: bool, message: string, playx_transaction_id: string}
|
||
*/
|
||
public static function push(MallOrder $order): array
|
||
{
|
||
$baseUrl = rtrim(strval(config('playx.api.base_url', '')), '/');
|
||
if ($baseUrl === '') {
|
||
return [
|
||
'ok' => false,
|
||
'message' => 'PlayX base_url not configured',
|
||
'playx_transaction_id' => '',
|
||
];
|
||
}
|
||
|
||
$path = strval(config('playx.api.bonus_grant_url', '/api/v1/bonus/grant'));
|
||
$url = $baseUrl . $path;
|
||
|
||
$item = MallItem::where('id', $order->mall_item_id)->find();
|
||
$rewardName = $item ? strval($item->title) : '';
|
||
$category = $item ? strval($item->category) : 'daily';
|
||
$categoryTitle = $item ? strval($item->category_title) : '';
|
||
$multiplier = intval($order->multiplier ?? 0);
|
||
if ($multiplier <= 0) {
|
||
$multiplier = 1;
|
||
}
|
||
|
||
$client = new Client([
|
||
'timeout' => 20,
|
||
'http_errors' => false,
|
||
]);
|
||
|
||
$requestId = 'mall_bonus_' . uniqid();
|
||
|
||
try {
|
||
$res = $client->post($url, [
|
||
'json' => [
|
||
'request_id' => $requestId,
|
||
'externalTransactionId' => $order->external_transaction_id,
|
||
'user_id' => $order->user_id,
|
||
'amount' => $order->amount,
|
||
'rewardName' => $rewardName,
|
||
'category' => $category,
|
||
'categoryTitle' => $categoryTitle,
|
||
'multiplier' => $multiplier,
|
||
],
|
||
]);
|
||
|
||
$data = json_decode(strval($res->getBody()), true) ?? [];
|
||
if ($res->getStatusCode() === 200 && ($data['status'] ?? '') === 'accepted') {
|
||
return [
|
||
'ok' => true,
|
||
'message' => '',
|
||
'playx_transaction_id' => strval($data['playx_transaction_id'] ?? ''),
|
||
];
|
||
}
|
||
|
||
return [
|
||
'ok' => false,
|
||
'message' => strval($data['message'] ?? 'PlayX bonus grant not accepted'),
|
||
'playx_transaction_id' => '',
|
||
];
|
||
} catch (\Throwable $e) {
|
||
return [
|
||
'ok' => false,
|
||
'message' => $e->getMessage(),
|
||
'playx_transaction_id' => '',
|
||
];
|
||
}
|
||
}
|
||
}
|