AirwallexController.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Longyi\Pay\Airwallex\Http\Controllers;
  3. use Longyi\Pay\Airwallex\Payment\Airwallex;
  4. use Webkul\Checkout\Facades\Cart;
  5. use Webkul\Sales\Models\Order;
  6. use Webkul\Sales\Repositories\OrderRepository;
  7. use Webkul\Sales\Transformers\OrderResource;
  8. use Longyi\Pay\AwxKlarna\Payment\AwxKlarna;
  9. class AirwallexController extends Controller
  10. {
  11. /**
  12. * Create a new controller instance.
  13. *
  14. * @return void
  15. */
  16. public function __construct(
  17. protected OrderRepository $orderRepository,
  18. protected Airwallex $airwallex,
  19. ) {}
  20. public function hook()
  21. {
  22. $param = @file_get_contents('php://input');
  23. $header = $this->getAllHeaders();
  24. $data = [
  25. 'timestamp' => $header['x-timestamp'],
  26. 'signature' => $header['x-signature'],
  27. 'content' => $param
  28. ];
  29. $result = $this->airwallex->checkSignature($data, 'secret');
  30. if (!$result) {
  31. @header("HTTP/1.0 400 Bad Request");
  32. exit;
  33. }
  34. $notifyData = json_decode($param, true);
  35. $orderNo = $notifyData['data']['object']['merchant_order_id'] ?: 0;
  36. if (empty($orderNo)) {
  37. @header("HTTP/1.0 400 Bad Request");
  38. exit;
  39. }
  40. $prefix = $this->airwallex->getPrefix();
  41. if (strpos($orderNo, $prefix) === false) {
  42. @header("HTTP/1.0 200 ok");
  43. exit;
  44. }
  45. $type = $notifyData['name'];
  46. $orderId = str_replace($prefix, '', $orderNo);
  47. $transactionId = $notifyData['data']['object']['id'] ?: '';
  48. $eventType = "payments_webhook_" . strtolower(str_replace(".", "_", $type));
  49. $this->airwallex->$eventType($orderId, $transactionId);
  50. }
  51. }