|
|
@@ -20,111 +20,22 @@ class ApplepayController extends Controller
|
|
|
protected Applepay $applepay,
|
|
|
) {}
|
|
|
|
|
|
- /**
|
|
|
- * 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') {
|
|
|
- session()->flash('order_id', $orderId);
|
|
|
- return redirect()->route('shop.checkout.onepage.success');
|
|
|
- } else {
|
|
|
- session()->flash('error', 'afterpay cancel');
|
|
|
- return redirect()->route('shop.checkout.cart.index');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
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;
|
|
|
+ request()->all();
|
|
|
+ $orderId = $event['resource']['invoice_id'];
|
|
|
+ $transactionId = $event['resource']['id'];
|
|
|
+ $type = $event['event_type'];
|
|
|
+ switch ($type) {
|
|
|
+ case 'PAYMENT.CAPTURE.COMPLETED':
|
|
|
+ $this->applepay->paymentSucceeded($orderId, $transactionId);
|
|
|
+ @header("HTTP/1.0 200 ok");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ // 对于未知事件类型,记录日志但仍返回成功
|
|
|
+ @header("HTTP/1.0 200 ok");
|
|
|
+ break;
|
|
|
}
|
|
|
- $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);
|
|
|
}
|
|
|
|
|
|
}
|