|
|
@@ -27,7 +27,7 @@ class Klarna extends Payment
|
|
|
protected $createSessionUlr = 'https://api-na.playground.klarna.com/payments/v1/sessions';
|
|
|
protected $createOrderUlr = 'https://api-na.playground.klarna.com/payments/v1/authorizations/{authorizationToken}/order';
|
|
|
protected $getOrderUlr = 'https://api-na.playground.klarna.com/ordermanagement/v1/orders/{order_id}';
|
|
|
-
|
|
|
+ protected $authorizationUrl = null;
|
|
|
public function __construct(
|
|
|
protected InvoiceRepository $invoiceRepository,
|
|
|
protected OrderRepository $orderRepository,
|
|
|
@@ -40,6 +40,7 @@ class Klarna extends Payment
|
|
|
}
|
|
|
$this->clientId = $this->getConfigData('client_id');
|
|
|
$this->apikey = $this->getConfigData('secret_id');
|
|
|
+ $this->authorizationUrl = $this->getBaseUlr() . '/payment/klarna/hook';
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -99,7 +100,7 @@ class Klarna extends Payment
|
|
|
'merchant_urls' => [
|
|
|
'confirmation_url' => $input->paymentSuccessUrl . '?orderId=' . $order->id,
|
|
|
'push_url' => $input->paymentSuccessUrl . '?orderId=' . $order->id,
|
|
|
- 'authorization' => $input->paymentSuccessUrl . '?orderId=' . $order->id
|
|
|
+ 'authorization' => $this->authorizationUrl . '?orderId=' . $order->id . '&secretToken=' . $this->generateSignature($order->id)
|
|
|
],
|
|
|
'order_lines' => $products,
|
|
|
'purchase_country' => $bill->country,
|
|
|
@@ -245,40 +246,54 @@ class Klarna extends Payment
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function fetchOrderStatus($gatewayOrderId = null)
|
|
|
+ //成功修改状态
|
|
|
+ public function paymentSucceeded($order, $transactionId)
|
|
|
{
|
|
|
- $order = $this->getOrder();
|
|
|
- if ($order) {
|
|
|
- $gatewayOrderId = $order->payment->additional['transaction_id'];
|
|
|
+ if (!$order->id) {
|
|
|
+ return false;
|
|
|
}
|
|
|
- if (empty($gatewayOrderId)) {
|
|
|
- return new \Exception('OrderId is empty');
|
|
|
+ if ($order->status != Order::STATUS_PENDING) {
|
|
|
+ return false;
|
|
|
}
|
|
|
- $client = new Client();
|
|
|
- $response = $client->request('GET', str_replace('{order_id}', $gatewayOrderId, $this->getOrderUlr), [
|
|
|
- '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("klarna fetchOrderStatus response", [
|
|
|
- 'gatewayOrderId' => $gatewayOrderId,
|
|
|
- 'status' => $response->getStatusCode(),
|
|
|
- 'body' => $responseBody
|
|
|
- ]);
|
|
|
- $resultObject = json_decode($responseBody);
|
|
|
- $paymentStatus = [];
|
|
|
- $paymentStatus['captures'] = null;
|
|
|
- $paymentStatus['status'] = null;
|
|
|
- if ($resultObject->fraud_status == 'ACCEPTED' && $resultObject->status == 'AUTHORIZED') {
|
|
|
- $paymentStatus['captures'] = true;
|
|
|
- $paymentStatus['status'] = 'COMPLETED';
|
|
|
+ try {
|
|
|
+ $this->createOrder($order, $transactionId);
|
|
|
+ $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();
|
|
|
+ }
|
|
|
+ if ($order->canInvoice()) {
|
|
|
+ $this->invoiceRepository->create($this->prepareInvoiceData($order));
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::channel('payment')->error('klarna paymentSucceeded error', [
|
|
|
+ 'order' => $order->increment_id ?? null,
|
|
|
+ 'message' => $e->getMessage(),
|
|
|
+ ]);
|
|
|
+ //抛出异常
|
|
|
+ throw $e;
|
|
|
}
|
|
|
- return $paymentStatus;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Prepares order's invoice data for creation.
|
|
|
+ */
|
|
|
+ protected function prepareInvoiceData($order): array
|
|
|
+ {
|
|
|
+ $invoiceData = [
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'invoice' => ['items' => []],
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach ($order->items as $item) {
|
|
|
+ $invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $invoiceData;
|
|
|
}
|
|
|
/**
|
|
|
* Get payment method image.
|
|
|
@@ -304,4 +319,23 @@ class Klarna extends Payment
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成签名
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function generateSignature($orderId): string
|
|
|
+ {
|
|
|
+ return md5($orderId . 'veryGood');
|
|
|
+ }
|
|
|
+ //验签
|
|
|
+ public function verifySignature($orderId, $signature)
|
|
|
+ {
|
|
|
+ return $this->generateSignature($orderId) == $signature;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getBaseUlr()
|
|
|
+ {
|
|
|
+ return config('app.url');
|
|
|
+ }
|
|
|
}
|