Process.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. class Laybuy_Processes_CreateOrder_CompatibilityMode_Process extends Laybuy_Processes_AbstractProcess
  3. {
  4. public $orderId;
  5. public function setOrderId($orderId)
  6. {
  7. $this->orderId = $orderId;
  8. }
  9. public function getOrderId()
  10. {
  11. return $this->orderId;
  12. }
  13. public function execute()
  14. {
  15. # Get posted data.
  16. $order_id = $this->getOrderId();
  17. if (!$order_id) {
  18. throw new InvalidArgumentException(
  19. 'Parameter order_id is not set. Please call setCheckout before executing the process'
  20. );
  21. }
  22. $order = new WC_Order( $order_id );
  23. $cart = WC()->cart;
  24. $nonce = wp_create_nonce( "laybuy_{$order_id}_create" );
  25. update_post_meta( $order_id, '_laybuy_order_nonce', $nonce );
  26. $apiData = array(
  27. 'amount' => $cart->get_total( 'edit' ),
  28. 'returnUrl' => $this->_buildReturnUrl($order_id, $nonce, ['compatibility' => 1]),
  29. 'merchantReference' => $this->_makeUniqueReference($order_id),
  30. 'customer' => array(
  31. 'firstName' => $order->get_billing_first_name(),
  32. 'lastName' => $order->get_billing_last_name(),
  33. 'email' => $order->get_billing_email(),
  34. 'phone' => $order->get_billing_phone()
  35. ),
  36. 'billingAddress' => array(
  37. "address1" => $order->get_billing_address_1(),
  38. "city" => $order->get_billing_city(),
  39. "postcode" => $order->get_billing_postcode(),
  40. "country" => $order->get_billing_country(),
  41. ),
  42. 'items' => array()
  43. );
  44. $discount_tax = $cart->get_discount_tax();
  45. $cart_tax = $cart->get_cart_contents_tax() + $cart->get_fee_tax();
  46. $shipping_tax = $cart->get_shipping_tax();
  47. $total = $cart->get_total( 'edit' );
  48. $shipping_total = $cart->get_shipping_total();
  49. $apiData['tax'] = floatval($cart_tax + $shipping_tax + $discount_tax);
  50. // items total
  51. $items_total = $total;
  52. // shipping
  53. if ($shipping_total > 0) {
  54. $apiData['items'][] = array(
  55. 'id' => 'shipping_fee_for_order#' . $order_id,
  56. 'description' => 'Shipping fee for this order',
  57. 'quantity' => '1',
  58. 'price' => $shipping_total
  59. );
  60. $items_total -= $shipping_total;
  61. }
  62. // tax
  63. if ($cart_tax) {
  64. $apiData['items'][] = array(
  65. 'id' => 'total_tax_amount_for_order#' . $order_id,
  66. 'description' => 'Tax amount for this order',
  67. 'quantity' => '1',
  68. 'price' => $cart_tax + $shipping_tax + $discount_tax
  69. );
  70. $items_total -= ($cart_tax + $shipping_tax + $discount_tax);
  71. }
  72. $apiData['items'][] = [
  73. 'id' => 'item_for_order___#' . $order_id,
  74. 'description' => 'Purchase from ' . get_bloginfo('name'),
  75. 'quantity' => 1,
  76. 'price' => $items_total
  77. ];
  78. WC_Laybuy_Logger::log("Sending a request to create a Laybuy Order");
  79. WC_Laybuy_Logger::log(print_r($apiData, true));
  80. $gateway = $this->getProcessManager()->getApiGateway();
  81. // Send the order data to Laybuy to get a token.
  82. $response = $gateway->createOrder($apiData);
  83. if ($response === false || $response->result === Laybuy_ApiGateway::PAYMENT_STATUS_ERROR) {
  84. # Log the error and return a truthy integer (otherwise WooCommerce will not bypass the standard order creation process).
  85. WC_Laybuy_Logger::error("Laybuy_ApiGateway::createOrder() returned -2 (Laybuy did not provide a token for this order.)");
  86. WC_Laybuy_Logger::error("API Payload: " . print_r($apiData, true));
  87. WC_Laybuy_Logger::error("API Response: " . var_export($response, true));
  88. return -2;
  89. }
  90. $result = array(
  91. 'result' => 'success',
  92. 'redirect' => $response->paymentUrl
  93. );
  94. if ( is_ajax() ) {
  95. wp_send_json( $result );
  96. } else {
  97. wp_redirect( $result['redirect'] );
  98. exit;
  99. }
  100. }
  101. }