|
|
@@ -27,6 +27,20 @@ class OrderService extends AlibabaAgentBaseService
|
|
27
|
27
|
*/
|
|
28
|
28
|
const LOGISTICS_NAMESPACE = 'com.alibaba.logistics';
|
|
29
|
29
|
|
|
|
30
|
+ const REFUND_GOOD_STATUS = [
|
|
|
31
|
+ // 售中等待卖家发货
|
|
|
32
|
+ 'waitsellersend' => 'refundWaitSellerSend',
|
|
|
33
|
+ // 售中等待买家收货
|
|
|
34
|
+ 'waitbuyerreceive'=>'refundWaitBuyerReceive',
|
|
|
35
|
+ // // 售中已收货(未确认完成交易)
|
|
|
36
|
+ // 'BuyerReceived'=>'refundBuyerReceived',
|
|
|
37
|
+ // // 售后未收货
|
|
|
38
|
+ // 'saleBuyerNotReceived'=>'aftersaleBuyerNotReceived',
|
|
|
39
|
+ // // 售后已收到货
|
|
|
40
|
+ // 'saleBuyerReceived'=>'aftersaleBuyerReceived',
|
|
|
41
|
+
|
|
|
42
|
+ ];
|
|
|
43
|
+
|
|
30
|
44
|
/**
|
|
31
|
45
|
* 预下单
|
|
32
|
46
|
*
|
|
|
@@ -509,33 +523,51 @@ class OrderService extends AlibabaAgentBaseService
|
|
509
|
523
|
}
|
|
510
|
524
|
|
|
511
|
525
|
/**
|
|
512
|
|
- * 查询退款退货原因列表
|
|
|
526
|
+ * 获取订单详情(买家视角)
|
|
513
|
527
|
*
|
|
514
|
|
- * API: com.alibaba.trade:alibaba.trade.getRefundReasonList-1
|
|
|
528
|
+ * API: com.alibaba.trade:alibaba.trade.get.buyerView-1
|
|
515
|
529
|
*
|
|
516
|
|
- * 接口文档: https://open.1688.com/api/apidocdetail.htm?id=com.alibaba.trade%3Aalibaba.trade.getRefundReasonList-1
|
|
|
530
|
+ * @param string $orderId 1688订单号
|
|
|
531
|
+ * @return array|false
|
|
|
532
|
+ */
|
|
|
533
|
+ public function getOrderDetailBuyerView(string $orderId)
|
|
|
534
|
+ {
|
|
|
535
|
+ if (empty($orderId)) {
|
|
|
536
|
+ Log::channel('alibaba')->error('[获取订单详情] orderId不能为空');
|
|
|
537
|
+ return false;
|
|
|
538
|
+ }
|
|
|
539
|
+
|
|
|
540
|
+ $businessParams = [
|
|
|
541
|
+ 'orderId' => $orderId,
|
|
|
542
|
+ 'webSite' => 1688,
|
|
|
543
|
+ ];
|
|
|
544
|
+
|
|
|
545
|
+ $result = $this->executeParam2(
|
|
|
546
|
+ self::NAMESPACE,
|
|
|
547
|
+ 'alibaba.trade.get.buyerView',
|
|
|
548
|
+ 1,
|
|
|
549
|
+ $businessParams
|
|
|
550
|
+ );
|
|
|
551
|
+
|
|
|
552
|
+ if ($result === false) {
|
|
|
553
|
+ Log::channel('alibaba')->error('[获取订单详情] API请求失败, orderId:' . $orderId);
|
|
|
554
|
+ return false;
|
|
|
555
|
+ }
|
|
|
556
|
+
|
|
|
557
|
+ Log::channel('alibaba')->info('[获取订单详情] API请求成功, orderId:' . $orderId . ', result:' . json_encode($result, JSON_UNESCAPED_UNICODE));
|
|
|
558
|
+
|
|
|
559
|
+ return $result;
|
|
|
560
|
+ }
|
|
|
561
|
+
|
|
|
562
|
+ /**
|
|
|
563
|
+ * 查询退款退货原因列表
|
|
517
|
564
|
*
|
|
518
|
|
- * 请求参数:
|
|
519
|
|
- * - orderId: 订单号(必填)
|
|
520
|
|
- * - orderEntryIds: 子订单号(可选,多个用逗号分隔)
|
|
|
565
|
+ * 流程:
|
|
|
566
|
+ * 1. 先调用 alibaba.trade.get.buyerView 获取订单详情
|
|
|
567
|
+ * 2. 从订单详情中提取子订单号(orderEntryIds)、货物状态、可退款金额、可退运费
|
|
|
568
|
+ * 3. 再调用 alibaba.trade.getRefundReasonList 获取退款原因
|
|
521
|
569
|
*
|
|
522
|
|
- * 返回示例:
|
|
523
|
|
- * {
|
|
524
|
|
- * "result": [
|
|
525
|
|
- * {
|
|
526
|
|
- * "refundId": "REFUND123456",
|
|
527
|
|
- * "refundType": "退货退款",
|
|
528
|
|
- * "refundStatus": "waitSellerAgree",
|
|
529
|
|
- * "refundReasonList": [
|
|
530
|
|
- * {
|
|
531
|
|
- * "reasonId": 1,
|
|
532
|
|
- * "reasonText": "质量问题"
|
|
533
|
|
- * }
|
|
534
|
|
- * ]
|
|
535
|
|
- * }
|
|
536
|
|
- * ],
|
|
537
|
|
- * "success": true
|
|
538
|
|
- * }
|
|
|
570
|
+ * API: com.alibaba.trade:alibaba.trade.getRefundReasonList-1
|
|
539
|
571
|
*
|
|
540
|
572
|
* @param array $params 请求参数(orderId 必填)
|
|
541
|
573
|
* @return array|false
|
|
|
@@ -548,20 +580,38 @@ class OrderService extends AlibabaAgentBaseService
|
|
548
|
580
|
return false;
|
|
549
|
581
|
}
|
|
550
|
582
|
|
|
551
|
|
- $businessParams = [
|
|
552
|
|
- 'orderId' => (string)$params['orderId'],
|
|
553
|
|
- ];
|
|
|
583
|
+ $orderId = (string)$params['orderId'];
|
|
|
584
|
+
|
|
|
585
|
+ // 1. 先获取订单详情,提取子订单号和货物状态
|
|
|
586
|
+ $orderDetail = $this->getOrderDetailBuyerView($orderId);
|
|
554
|
587
|
|
|
555
|
|
- // 子订单号
|
|
556
|
|
- if (!empty($params['orderEntryIds'])) {
|
|
557
|
|
- $businessParams['orderEntryIds'] = '[' . (string)$params['orderEntryIds'] . ']';
|
|
|
588
|
+ if ($orderDetail === false) {
|
|
|
589
|
+ Log::channel('alibaba')->error('[获取退款原因] 获取订单详情失败, orderId:' . $orderId);
|
|
|
590
|
+ return false;
|
|
558
|
591
|
}
|
|
559
|
592
|
|
|
560
|
|
- // 货物状态
|
|
561
|
|
- if (!empty($params['goodsStatus'])) {
|
|
562
|
|
- $businessParams['goodsStatus'] = (string)$params['goodsStatus'];
|
|
|
593
|
+ // 从订单详情中提取子订单ID列表
|
|
|
594
|
+ $orderEntryIds = $orderDetail['result']['productItems'][0]['subItemID'] ?: '';
|
|
|
595
|
+ $goodsStatus = self::REFUND_GOOD_STATUS[$orderDetail['result']['productItems'][0]['status']] ?: '';
|
|
|
596
|
+ if (!$goodsStatus) {
|
|
|
597
|
+ Log::channel('alibaba')->error('[获取退款原因] 获取货物状态失败, orderId:' . $orderId);
|
|
|
598
|
+ return false;
|
|
563
|
599
|
}
|
|
564
|
600
|
|
|
|
601
|
+ // 提取可退款金额和可退运费(从订单详情中)
|
|
|
602
|
+ // totalSuccessAmount: 实付款(分),carriage: 运费(分)
|
|
|
603
|
+ $applyPayment = (string)$orderDetail['result']['baseInfo']['totalAmount'] ?? 0;
|
|
|
604
|
+ $applyCarriage = (string)$orderDetail['result']['baseInfo']['shippingFee'] ?? 0;
|
|
|
605
|
+
|
|
|
606
|
+ // 2. 调用退款原因查询API
|
|
|
607
|
+ $businessParams = [
|
|
|
608
|
+ 'orderId' => $orderId,
|
|
|
609
|
+ 'orderEntryIds' => '[' . $orderEntryIds . ']',
|
|
|
610
|
+ 'goodsStatus' => $goodsStatus,
|
|
|
611
|
+ ];
|
|
|
612
|
+
|
|
|
613
|
+ Log::channel('alibaba')->info('[获取退款原因] 准备请求, orderId:' . $orderId . ', orderEntryIds:' . json_encode($orderEntryIds) . ', goodsStatus:' . $goodsStatus);
|
|
|
614
|
+
|
|
565
|
615
|
// --- 调用API ---
|
|
566
|
616
|
$result = $this->executeParam2(
|
|
567
|
617
|
self::NAMESPACE,
|
|
|
@@ -577,7 +627,7 @@ class OrderService extends AlibabaAgentBaseService
|
|
577
|
627
|
|
|
578
|
628
|
Log::channel('alibaba')->info('[获取退款原因] API请求成功, result:' . json_encode($result, JSON_UNESCAPED_UNICODE));
|
|
579
|
629
|
|
|
580
|
|
- return $this->formatRefundReasonListResult($result);
|
|
|
630
|
+ return $this->formatRefundReasonListResult($result, $orderId, $orderEntryIds, $goodsStatus, $applyPayment, $applyCarriage);
|
|
581
|
631
|
}
|
|
582
|
632
|
|
|
583
|
633
|
/**
|
|
|
@@ -595,10 +645,15 @@ class OrderService extends AlibabaAgentBaseService
|
|
595
|
645
|
* }
|
|
596
|
646
|
* }
|
|
597
|
647
|
*
|
|
598
|
|
- * @param array $result API原始返回数据
|
|
|
648
|
+ * @param array $result API原始返回数据
|
|
|
649
|
+ * @param string $orderId 1688订单号
|
|
|
650
|
+ * @param string $orderEntryIds 子订单ID列表
|
|
|
651
|
+ * @param string $goodsStatus 货物状态
|
|
|
652
|
+ * @param int $applyPayment 可退款金额(分)
|
|
|
653
|
+ * @param int $applyCarriage 可退运费(分)
|
|
599
|
654
|
* @return array
|
|
600
|
655
|
*/
|
|
601
|
|
- protected function formatRefundReasonListResult(array $result): array
|
|
|
656
|
+ protected function formatRefundReasonListResult(array $result, string $orderId = '', string $orderEntryIds = '', string $goodsStatus = '', string $applyPayment = '0', string $applyCarriage = '0'): array
|
|
602
|
657
|
{
|
|
603
|
658
|
$reasons = [];
|
|
604
|
659
|
if (isset($result['result']['result']['reasons']) && is_array($result['result']['result']['reasons'])) {
|
|
|
@@ -610,6 +665,13 @@ class OrderService extends AlibabaAgentBaseService
|
|
610
|
665
|
}
|
|
611
|
666
|
}
|
|
612
|
667
|
|
|
613
|
|
- return $reasons;
|
|
|
668
|
+ return [
|
|
|
669
|
+ 'orderId' => $orderId,
|
|
|
670
|
+ 'orderEntryIds' => $orderEntryIds,
|
|
|
671
|
+ 'goodsStatus' => $goodsStatus,
|
|
|
672
|
+ 'applyPayment' => $applyPayment,
|
|
|
673
|
+ 'applyCarriage' => $applyCarriage,
|
|
|
674
|
+ 'reasons' => $reasons,
|
|
|
675
|
+ ];
|
|
614
|
676
|
}
|
|
615
|
677
|
}
|