Ver código fonte

修改支付方式

llp 2 semanas atrás
pai
commit
3ac5ef1495

+ 10 - 2
packages/Longyi/Pay/Airwallex/src/Http/Controllers/AirwallexController.php

@@ -50,8 +50,16 @@ class AirwallexController extends Controller
         $type = $notifyData['name'];
         $type = $notifyData['name'];
         $orderId = str_replace($prefix, '', $orderNo);
         $orderId = str_replace($prefix, '', $orderNo);
         $transactionId = $notifyData['data']['object']['id'] ?: '';
         $transactionId = $notifyData['data']['object']['id'] ?: '';
-        $eventType = "payments_webhook_" . strtolower(str_replace(".", "_", $type));
-        $this->airwallex->$eventType($orderId, $transactionId);
+        switch ($type) {
+            case 'payment_intent.succeeded':
+            case 'payment_intent.requires_capture':
+                $this->airwallex->paymentSucceeded($orderId, $transactionId);
+                break;
+            default:
+                // 对于未知事件类型,记录日志但仍返回成功
+                @header("HTTP/1.0 200 ok");
+                break;
+        }
     }
     }
 
 
 }
 }

+ 23 - 21
packages/Longyi/Pay/Airwallex/src/Payment/Airwallex.php

@@ -8,6 +8,7 @@ use Webkul\Checkout\Facades\Cart;
 use Webkul\Payment\Payment\Payment;
 use Webkul\Payment\Payment\Payment;
 use Webkul\Sales\Models\Order;
 use Webkul\Sales\Models\Order;
 use Webkul\Sales\Repositories\OrderRepository;
 use Webkul\Sales\Repositories\OrderRepository;
+use Webkul\Sales\Repositories\InvoiceRepository;
 
 
 class Airwallex extends Payment
 class Airwallex extends Payment
 {
 {
@@ -29,7 +30,10 @@ class Airwallex extends Payment
     protected $captureIntentsUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/{id}/capture';
     protected $captureIntentsUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/{id}/capture';
     protected $paymentIntentsUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/';
     protected $paymentIntentsUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/';
 
 
-    public function __construct()
+    public function __construct(
+        protected InvoiceRepository $invoiceRepository,
+        protected OrderRepository $orderRepository,
+    )
     {
     {
         if ($this->getConfigData('mode') == 'live') {
         if ($this->getConfigData('mode') == 'live') {
             $this->tokenUrl = 'https://api.airwallex.com/api/v1/authentication/login';
             $this->tokenUrl = 'https://api.airwallex.com/api/v1/authentication/login';
@@ -155,11 +159,11 @@ class Airwallex extends Payment
     }
     }
 
 
     //成功修改状态
     //成功修改状态
-    public function payments_webhook_payment_intent_succeeded($orderId, $transactionId)
+    public function paymentSucceeded($orderId, $transactionId)
     {
     {
-        $order = app(OrderRepository::class)->find($orderId);
+        $order = $this->orderRepository->find($orderId);
 
 
-        if (!$order) {
+        if (!$order->id) {
             return false;
             return false;
         }
         }
         if ($order->status != Order::STATUS_PENDING) {
         if ($order->status != Order::STATUS_PENDING) {
@@ -173,28 +177,26 @@ class Airwallex extends Payment
             $order->payment->additional = $additional;
             $order->payment->additional = $additional;
             $order->payment->save();
             $order->payment->save();
         }
         }
+        if ($order->canInvoice()) {
+            $this->invoiceRepository->create($this->prepareInvoiceData($order));
+        }
     }
     }
-    //授权成功修改状态
-    public function payments_webhook_payment_intent_requires_capture($orderId, $transactionId)
+    /**
+     * Prepares order's invoice data for creation.
+     */
+    protected function prepareInvoiceData($order): array
     {
     {
-        $order = app(OrderRepository::class)->find($orderId);
+        $invoiceData = [
+            'order_id' => $order->id,
+            'invoice'  => ['items' => []],
+        ];
 
 
-        if (!$order) {
-            return false;
-        }
-        if ($order->status != Order::STATUS_PENDING) {
-            return false;
-        }
-        $order->status = Order::STATUS_PROCESSING;
-        $order->save();
-        if ($order->payment) {
-            $additional = $order->payment->additional;
-            $additional['transaction_id'] = $transactionId;
-            $order->payment->additional = $additional;
-            $order->payment->save();
+        foreach ($order->items as $item) {
+            $invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
         }
         }
-    }
 
 
+        return $invoiceData;
+    }
     /**
     /**
      * 获取一个唯一的id
      * 获取一个唯一的id
      */
      */