| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace Longyi\Pay\AwxKlarna\Http\Controllers;
- use Webkul\Checkout\Facades\Cart;
- use Webkul\Sales\Models\Order;
- use Webkul\Sales\Repositories\OrderRepository;
- use Webkul\Sales\Transformers\OrderResource;
- use Longyi\Pay\AwxKlarna\Payment\AwxKlarna;
- class KlarnaController extends Controller
- {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct(
- protected OrderRepository $orderRepository,
- protected AwxKlarna $awxKlarna,
- ) {}
- /**
- * Redirects to the klarna.
- *
- * @return \Illuminate\View\View
- */
- public function placeOrder()
- {
- $cart = Cart::getCart();
- $data = (new OrderResource($cart))->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);
- }
- }
|