KlarnaController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Longyi\Pay\AwxKlarna\Http\Controllers;
  3. use Webkul\Checkout\Facades\Cart;
  4. use Webkul\Sales\Models\Order;
  5. use Webkul\Sales\Repositories\OrderRepository;
  6. use Webkul\Sales\Transformers\OrderResource;
  7. use Longyi\Pay\AwxKlarna\Payment\AwxKlarna;
  8. class KlarnaController extends Controller
  9. {
  10. /**
  11. * Create a new controller instance.
  12. *
  13. * @return void
  14. */
  15. public function __construct(
  16. protected OrderRepository $orderRepository,
  17. protected AwxKlarna $awxKlarna,
  18. ) {}
  19. /**
  20. * Redirects to the klarna.
  21. *
  22. * @return \Illuminate\View\View
  23. */
  24. public function placeOrder()
  25. {
  26. $cart = Cart::getCart();
  27. $data = (new OrderResource($cart))->jsonSerialize();
  28. $order = $this->orderRepository->create($data);
  29. Cart::deActivateCart();
  30. session()->flash('order_id', $order->id);
  31. return view('AwxKlarna::standard-redirect', ['order' => $order]);
  32. }
  33. public function redirect()
  34. {
  35. $orderId = request()->input('orderId');
  36. if (!$orderId) {
  37. return response()->json([
  38. 'error' => 'Order ID is required'
  39. ], 400);
  40. }
  41. $order = $this->orderRepository->find($orderId);
  42. if (!$order) {
  43. return response()->json([
  44. 'error' => 'Order not found'
  45. ], 404);
  46. }
  47. if ($order->status != Order::STATUS_PENDING) {
  48. return response()->json([
  49. 'error' => 'No payment required for the order'
  50. ], 404);
  51. }
  52. $cartData = [
  53. 'shipping_address' => $order->shipping_address ? $order->shipping_address->toArray() : [],
  54. 'billing_address' => $order->billing_address ? $order->billing_address->toArray() : [],
  55. 'grand_total' => $order->grand_total,
  56. 'order_currency_code' => $order->order_currency_code,
  57. 'customer_email' => $order->customer_email,
  58. 'customer_first_name' => $order->customer_first_name,
  59. 'customer_last_name' => $order->customer_last_name
  60. ];
  61. try {
  62. $checkoutUrl = $this->awxKlarna->createPayment($cartData, [
  63. 'merchantOrderId' => $order->id
  64. ]);
  65. if (!$checkoutUrl) {
  66. throw new \Exception('Failed to get checkout URL from Airwallex');
  67. }
  68. return redirect()->away($checkoutUrl);
  69. } catch (\Exception $e) {
  70. return response()->json([
  71. 'error' => 'Payment initialization failed: ' . $e->getMessage()
  72. ], 500);
  73. }
  74. }
  75. /**
  76. * Success payment.
  77. *
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function return()
  81. {
  82. $awxReturnResult = request()->input('awx_return_result');
  83. $orderId = request()->input('orderId');
  84. if ($awxReturnResult == 'success') {
  85. return redirect()->to('/paymentresult/success?orderId=' . $orderId);
  86. } else if ($awxReturnResult == 'cancel') {
  87. return redirect()->to('/paymentresult/cancel?orderId=' . $orderId);
  88. } else {
  89. return redirect()->to('/paymentresult/failure?orderId=' . $orderId);
  90. }
  91. }
  92. public function hook()
  93. {
  94. $param = @file_get_contents('php://input');
  95. $header = $this->getAllHeaders();
  96. $data = [
  97. 'timestamp' => $header['x-timestamp'],
  98. 'signature' => $header['x-signature'],
  99. 'content' => $param
  100. ];
  101. $result = $this->awxKlarna->checkSignature($data, 'secret');
  102. if (!$result) {
  103. @header("HTTP/1.0 400 Bad Request");
  104. exit;
  105. }
  106. $notifyData = json_decode($param, true);
  107. $orderNo = $notifyData['data']['object']['merchant_order_id'] ?: 0;
  108. if (empty($orderNo)) {
  109. @header("HTTP/1.0 400 Bad Request");
  110. exit;
  111. }
  112. $prefix = $this->awxKlarna->getPrefix();
  113. if (strpos($orderNo, $prefix) === false) {
  114. @header("HTTP/1.0 200 ok");
  115. exit;
  116. }
  117. $type = $notifyData['name'];
  118. $orderId = str_replace($prefix, '', $orderNo);
  119. $transactionId = $notifyData['data']['object']['id'] ?: '';
  120. $eventType = "payments_webhook_" . strtolower(str_replace(".", "_", $type));
  121. $this->awxKlarna->$eventType($orderId, $transactionId);
  122. }
  123. }