jsonSerialize(); $order = $this->orderRepository->create($data); Cart::deActivateCart(); session()->flash('order_id', $order->id); return view('AwxKlarna::standard-redirect', ['order' => $order]); } public function redirect() { $orderId = request()->input('orderId'); if (!$orderId) { return response()->json([ 'error' => 'Order ID is required' ], 400); } $order = $this->orderRepository->find($orderId); if (!$order) { return response()->json([ 'error' => 'Order not found' ], 404); } if ($order->status != Order::STATUS_PENDING) { return response()->json([ 'error' => 'No payment required for the order' ], 404); } $cartData = [ 'shipping_address' => $order->shipping_address ? $order->shipping_address->toArray() : [], 'billing_address' => $order->billing_address ? $order->billing_address->toArray() : [], 'grand_total' => $order->grand_total, 'order_currency_code' => $order->order_currency_code, 'customer_email' => $order->customer_email, 'customer_first_name' => $order->customer_first_name, 'customer_last_name' => $order->customer_last_name ]; try { $checkoutUrl = $this->awxKlarna->createPayment($cartData, [ 'merchantOrderId' => $order->id ]); if (!$checkoutUrl) { throw new \Exception('Failed to get checkout URL from Airwallex'); } return redirect()->away($checkoutUrl); } catch (\Exception $e) { return response()->json([ 'error' => 'Payment initialization failed: ' . $e->getMessage() ], 500); } } /** * Success payment. * * @return \Illuminate\Http\Response */ public function return() { $awxReturnResult = request()->input('awx_return_result'); $orderId = request()->input('orderId'); if ($awxReturnResult == 'success') { return redirect()->to('/paymentresult/success?orderId=' . $orderId); } else if ($awxReturnResult == 'cancel') { return redirect()->to('/paymentresult/cancel?orderId=' . $orderId); } else { return redirect()->to('/paymentresult/failure?orderId=' . $orderId); } } public function hook() { $param = @file_get_contents('php://input'); $header = $this->getAllHeaders(); $data = [ 'timestamp' => $header['x-timestamp'], 'signature' => $header['x-signature'], 'content' => $param ]; $result = $this->awxKlarna->checkSignature($data, 'secret'); if (!$result) { @header("HTTP/1.0 400 Bad Request"); exit; } $notifyData = json_decode($param, true); $orderNo = $notifyData['data']['object']['merchant_order_id'] ?: 0; if (empty($orderNo)) { @header("HTTP/1.0 400 Bad Request"); exit; } $prefix = $this->awxKlarna->getPrefix(); if (strpos($orderNo, $prefix) === false) { @header("HTTP/1.0 200 ok"); exit; } $type = $notifyData['name']; $orderId = str_replace($prefix, '', $orderNo); $transactionId = $notifyData['data']['object']['id'] ?: ''; $eventType = "payments_webhook_" . strtolower(str_replace(".", "_", $type)); $this->awxKlarna->$eventType($orderId, $transactionId); } }