Ver código fonte

修改金额

llp 1 dia atrás
pai
commit
fb94814360

+ 41 - 1
packages/Longyi/Pay/Afterpay/src/Payment/Afterpay.php

@@ -21,6 +21,7 @@ class Afterpay extends Payment
     protected $createPaymentUlr = 'https://global-api-sandbox.afterpay.com/v2/checkouts';
     protected $capturePaymentUlr = 'https://global-api-sandbox.afterpay.com/v2/payments/capture';
     protected $configurationUlr = 'https://global-api-sandbox.afterpay.com/v2/configuration';
+    protected $orderStatusUlr = 'https://global-api-sandbox.afterpay.com/v2/payments/token:';
 
     public function __construct()
     {
@@ -28,6 +29,7 @@ class Afterpay extends Payment
             $this->createPaymentUlr = 'https://global-api.afterpay.com/v2/checkouts';
             $this->capturePaymentUlr = 'https://global-api.afterpay.com/v2/payments/capture';
             $this->configurationUlr = 'https://global-api.afterpay.com/v2/configuration';
+            $this->orderStatusUlr = 'https://global-api-sandbox.afterpay.com/v2/payments/token:';
         }
         $this->clientId = $this->getConfigData('client_id_us');
         $this->apikey = $this->getConfigData('secret_id_us');
@@ -204,7 +206,7 @@ class Afterpay extends Payment
         ]);
 
         $resultObject = json_decode($responseBody);
-        if ($resultObject->status == 'APPROVED') {
+        if ($resultObject->status == 'APPROVED' && $resultObject->paymentState == 'CAPTURED') {
             return ['transaction_id' => $resultObject->token, 'amount' => $resultObject->events[0]->amount->amount];
         } else {
             throw new \Exception("{$methodType} capture is error");
@@ -287,6 +289,44 @@ class Afterpay extends Payment
         }
     }
 
+    public function fetchOrderStatus($gatewayOrderId = null)
+    {
+        $order = $this->getOrder();
+        if ($order) {
+            $gatewayOrderId = $order->payment->additional['gateway_order_id'];
+        }
+        if (empty($gatewayOrderId)) {
+            return new \Exception('gatewayOrderId is empty');
+        }
+        $countryCode = $order->billing_address->country ?? null;
+        $this->resolveCredentials($countryCode);
+        $client = new Client();
+        $response = $client->request('GET', $this->orderStatusUlr . $gatewayOrderId, [
+            'headers' => [
+                'Accept' => 'application/json',
+                'Content-Type' => 'application/json',
+                'Authorization' => 'Basic ' . base64_encode("{$this->clientId}:{$this->apikey}")
+            ],
+            'timeout' => 30,
+            'verify'  => false,
+        ]);
+        $responseBody = $response->getBody()->getContents();
+        Log::channel('payment')->info("{$order->payment->method} fetchOrderStatus response", [
+            'gatewayOrderId' => $gatewayOrderId,
+            'status' => $response->getStatusCode(),
+            'body' => $responseBody
+        ]);
+        $resultObject = json_decode($responseBody);
+        $paymentStatus = [];
+        $paymentStatus['captures'] = null;
+        $paymentStatus['status'] = null;
+        if ($resultObject->status == 'APPROVED' && $resultObject->paymentState == 'CAPTURED') {
+            $paymentStatus['captures'] = true;
+            $paymentStatus['status'] = 'COMPLETED';
+        }
+        return $paymentStatus;
+    }
+
     public function isAvailable($isShow = false)
     {
         if (! parent::isAvailable()) {

+ 17 - 9
packages/Longyi/Pay/Klarna/src/Payment/Klarna.php

@@ -84,13 +84,13 @@ class Klarna extends Payment
         foreach ($order->items as $item) {
             $data = [
                 'name' => $item->name,
-                'unit_price' => (float) $item->price,
-                'quantity' => $item->qty_ordered,
-                'total_amount' => $item->qty_ordered * $item->price
+                'unit_price' => $this->toMinorUnits((float) $item->price),
+                'quantity' => (int) $item->qty_ordered,
+                'total_amount' => $this->toMinorUnits((float) $item->price * (float) $item->qty_ordered),
             ];
             array_push($products, $data);
         }
-        $grandTotal = round($order->grand_total, 2);
+        $grandTotal = $this->toMinorUnits((float) $order->grand_total);
         $data = [
             'order_amount' => $grandTotal,
             'merchant_reference1' => $override['merchantOrderId'],
@@ -151,6 +151,14 @@ class Klarna extends Payment
         return $prefix . $uuid;
     }
 
+    /**
+     * Convert a decimal currency amount to minor units (cents).
+     */
+    protected function toMinorUnits(float $amount): int
+    {
+        return (int) round($amount * 100);
+    }
+
     public function captureAndVerify($order, $authorizationToken)
     {
         if ($order->status != Order::STATUS_PENDING) {
@@ -165,9 +173,9 @@ class Klarna extends Payment
         foreach ($order->items as $item) {
             $data = [
                 'name' => $item->name,
-                'unit_price' => (float) $item->price,
-                'quantity' => $item->qty_ordered,
-                'total_amount' => $item->qty_ordered * $item->price
+                'unit_price' => $this->toMinorUnits((float) $item->price),
+                'quantity' => (int) $item->qty_ordered,
+                'total_amount' => $this->toMinorUnits((float) $item->price * (float) $item->qty_ordered),
             ];
             array_push($products, $data);
         }
@@ -176,7 +184,7 @@ class Klarna extends Payment
             'billing_address' => [
                 'given_name' => $bill->first_name,
                 'family_name' => $bill->last_name,
-                'email' => $bill->customer_email,
+                'email' => $order->customer_email,
                 'phone' => $bill->phone,
                 'street_address' => $bill->address,
                 'city' => $bill->city,
@@ -184,7 +192,7 @@ class Klarna extends Payment
                 'region' => $bill->state,
                 'country' => $bill->country
             ],
-            'order_amount' => round($order->grand_total, 2),
+            'order_amount' => $this->toMinorUnits((float) $order->grand_total),
             'purchase_country' => $bill->country,
             'purchase_currency' => $order->order_currency_code,
             'order_lines' => $products

+ 10 - 6
packages/Webkul/BagistoApi/src/Jobs/ReconcilePendingPaymentJob.php

@@ -11,6 +11,7 @@ use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Event;
 use Illuminate\Support\Facades\Log;
 use Webkul\Paypal\Payment\SmartButton;
+use Webkul\BagistoApi\Services\PaymentService;
 use Webkul\Sales\Models\Order;
 use Webkul\Sales\Models\OrderProxy;
 use Webkul\Sales\Repositories\OrderRepository;
@@ -52,7 +53,7 @@ class ReconcilePendingPaymentJob implements ShouldQueue
         if (! $order) {
             return;
         }
-     
+
         if (! in_array($order->status, [Order::STATUS_PENDING, Order::STATUS_PENDING_PAYMENT], true)) {
             return;
         }
@@ -68,10 +69,14 @@ class ReconcilePendingPaymentJob implements ShouldQueue
         }
 
         try {
-            $gatewayOrder = app(SmartButton::class)->getOrder($gatewayOrderId);
-            $status = (string) ($gatewayOrder->result->status ?? '');
-            $captures = $gatewayOrder->result->purchase_units[0]->payments->captures ?? [];
-
+            $gatewayHandler = app(PaymentService::class)->resolveGatewayHandler($method, $order);
+            if ($gatewayHandler && method_exists($gatewayHandler, 'fetchOrderStatus')) {
+                list($captures, $status) = $gatewayHandler->fetchOrderStatus($gatewayOrderId);
+            } else {
+                $gatewayOrder = app(SmartButton::class)->getOrder($gatewayOrderId);
+                $status = (string) ($gatewayOrder->result->status ?? '');
+                $captures = $gatewayOrder->result->purchase_units[0]->payments->captures ?? [];
+            }
             if (! empty($captures) || in_array(strtoupper($status), ['COMPLETED', 'CAPTURED'], true)) {
                 /*
                  * Capture exists but Bagisto is still pending - a
@@ -89,7 +94,6 @@ class ReconcilePendingPaymentJob implements ShouldQueue
             if (in_array(strtoupper($status), ['VOIDED', 'EXPIRED', 'PAYER_ACTION_REQUIRED'], true)) {
                 Event::dispatch('bagistoapi.payment.reconcile.voided', $order);
             }
-
             $cancelled = DB::transaction(function () use ($orderRepository, $order, $gatewayOrderId): bool {
                 /*
                  * Re-lock and re-check inside a transaction right before

+ 1 - 1
packages/Webkul/BagistoApi/src/Services/PaymentService.php

@@ -479,7 +479,7 @@ class PaymentService
             return (string) $gatewayOrderId;
         }
     }
-    protected function resolveGatewayHandler(string $method, $order = null, $input = null): ?object
+    public function resolveGatewayHandler(string $method, $order = null, $input = null): ?object
     {
         $config = config("payment_methods.{$method}");