Process.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. class Laybuy_Processes_CreateQuote_WC_GT_3_6_Process extends Laybuy_Processes_CreateQuote_CreateQuoteAbstract
  3. {
  4. public $checkout;
  5. public function setCheckout($checkout)
  6. {
  7. $this->checkout = $checkout;
  8. }
  9. public function getCheckout()
  10. {
  11. return $this->checkout;
  12. }
  13. public function execute()
  14. {
  15. # Get posted data.
  16. $checkout = $this->getCheckout();
  17. if (!$checkout) {
  18. throw new InvalidArgumentException(
  19. 'Parameter checkout is not set. Please call setCheckout before executing the process'
  20. );
  21. }
  22. $data = $checkout->get_posted_data();
  23. if ($data['payment_method'] != self::PAYMENT_METHOD_LAYBUY) {
  24. return;
  25. }
  26. $quote_id = wp_insert_post( array(
  27. 'post_content' => 'Thank you for your order. Now redirecting to Laybuy to complete your payment...',
  28. 'post_title' => 'Laybuy Order',
  29. 'post_status' => 'publish',
  30. 'post_type' => 'laybuy_quote'
  31. ), true );
  32. if (is_wp_error($quote_id)) {
  33. $errors_str = implode($quote_id->get_error_messages(), ' ');
  34. WC_Laybuy_Logger::error("WC_Payment_Gateway_Laybuy::create_order_quote() returned -1 (Could not create laybuy_quote post. WordPress threw error(s): {$errors_str})");
  35. return -1;
  36. }
  37. WC_Laybuy_Logger::log("New WP Quote created with ID:{$quote_id} and permalink:\"" . get_permalink( $quote_id ) . "\"");
  38. $cart = WC()->cart;
  39. $cart_hash = $cart->get_cart_hash();
  40. $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
  41. $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
  42. $shipping_packages = WC()->shipping()->get_packages();
  43. $customer_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() );
  44. $order_vat_exempt = ( $cart->get_customer()->get_is_vat_exempt() ? 'yes' : 'no' );
  45. $currency = get_woocommerce_currency();
  46. $prices_include_tax = ( get_option( 'woocommerce_prices_include_tax' ) === 'yes' || get_woocommerce_currency() === WC_Laybuy_Helper::CURRENCY_CODE_GB);
  47. $customer_ip_address = WC_Geolocation::get_ip_address();
  48. $customer_user_agent = wc_get_user_agent();
  49. $customer_note = ( isset( $data['order_comments'] ) ? $data['order_comments'] : '' );
  50. $payment_method = ( isset( $available_gateways[ $data['payment_method'] ] ) ? $available_gateways[ $data['payment_method'] ] : $data['payment_method'] );
  51. $shipping_total = $cart->get_shipping_total();
  52. $discount_total = $cart->get_discount_total();
  53. $discount_tax = $cart->get_discount_tax();
  54. $cart_tax = $cart->get_cart_contents_tax() + $cart->get_fee_tax();
  55. $shipping_tax = $cart->get_shipping_tax();
  56. $total = $cart->get_total( 'edit' );
  57. $nonce = wp_create_nonce( "laybuy_quote_{$quote_id}_create" );
  58. update_post_meta( $quote_id, '_laybuy_quote_nonce', $nonce );
  59. $settings = (array) get_option( 'woocommerce_laybuy_settings', true );
  60. $defaultPhoneKey = 'billing_phone';
  61. $billingPhone = null;
  62. if (isset($data[$defaultPhoneKey])) {
  63. $billingPhone = $data[$defaultPhoneKey];
  64. }
  65. if (!$billingPhone && isset($data[$settings['laybuy_billing_phone_field']])) {
  66. $billingPhone = $data[$settings['laybuy_billing_phone_field']];
  67. }
  68. $apiData = array(
  69. 'amount' => $total,
  70. 'returnUrl' => $this->_buildReturnUrl($quote_id, $nonce),
  71. 'merchantReference' => $this->_makeUniqueReference($quote_id),
  72. 'customer' => array(
  73. 'firstName' => $data['billing_first_name'],
  74. 'lastName' => $data['billing_last_name'],
  75. 'email' => $data['billing_email'],
  76. 'phone' => $billingPhone
  77. ),
  78. 'billingAddress' => array(
  79. "address1" => $data['billing_address_1'],
  80. "city" => $data['billing_city'],
  81. "postcode" => $data['billing_postcode'],
  82. "country" => $data['billing_country'],
  83. ),
  84. 'items' => array()
  85. );
  86. $apiData['tax'] = floatval($cart_tax + $shipping_tax + $discount_tax);
  87. // items total
  88. $items_total = $total;
  89. // shipping
  90. if ($shipping_total > 0) {
  91. $apiData['items'][] = array(
  92. 'id' => 'shipping_fee_for_order#' . $quote_id,
  93. 'description' => 'Shipping fee for this order',
  94. 'quantity' => '1',
  95. 'price' => $shipping_total
  96. );
  97. $items_total -= $shipping_total;
  98. }
  99. // tax
  100. if ($cart_tax) {
  101. $apiData['items'][] = array(
  102. 'id' => 'total_tax_amount_for_order#' . $quote_id,
  103. 'description' => 'Tax amount for this order',
  104. 'quantity' => '1',
  105. 'price' => $cart_tax + $shipping_tax + $discount_tax
  106. );
  107. $items_total -= ($cart_tax + $shipping_tax + $discount_tax);
  108. }
  109. $apiData['items'][] = [
  110. 'id' => 'item_for_order___#' . $quote_id,
  111. 'description' => 'Purchase from ' . get_bloginfo('name'),
  112. 'quantity' => 1,
  113. 'price' => $items_total
  114. ];
  115. WC_Laybuy_Logger::log("Sending a request to create a Laybuy Order");
  116. WC_Laybuy_Logger::log(print_r($apiData, true));
  117. $gateway = $this->getProcessManager()->getApiGateway();
  118. // Send the order data to Laybuy to get a token.
  119. $response = $gateway->createOrder($apiData);
  120. if ($response === false || $response->result === Laybuy_ApiGateway::PAYMENT_STATUS_ERROR) {
  121. $this->getProcessManager()->cancelQuote($quote_id, 'failed');
  122. # Log the error and return a truthy integer (otherwise WooCommerce will not bypass the standard order creation process).
  123. WC_Laybuy_Logger::error("Laybuy_ApiGateway::createOrder() returned -2 (Laybuy did not provide a token for this order.)");
  124. WC_Laybuy_Logger::error("API Payload: " . print_r($data, true));
  125. WC_Laybuy_Logger::error("API Response: " . var_export($response, true));
  126. return -2;
  127. }
  128. # Process Get Laybuy Token End
  129. WC_Laybuy_Logger::log("Laybuy Order Created");
  130. WC_Laybuy_Logger::log("Response Data: " . print_r($response, true));
  131. # Add the meta data to the Afterpay_Quote post record.
  132. add_post_meta( $quote_id, 'status', 'pending' );
  133. add_post_meta( $quote_id, 'token', $response->token );
  134. add_post_meta( $quote_id, 'posted', $this->_encode($data) );
  135. add_post_meta( $quote_id, '$_POST', $this->_encode($_POST) );
  136. add_post_meta( $quote_id, 'cart', $this->_encode($cart) );
  137. add_post_meta( $quote_id, 'cart_hash', $this->_encode($cart_hash) );
  138. add_post_meta( $quote_id, 'chosen_shipping_methods', $this->_encode($chosen_shipping_methods) );
  139. add_post_meta( $quote_id, 'shipping_packages', $this->_encode($shipping_packages) );
  140. add_post_meta( $quote_id, 'customer_id', $this->_encode($customer_id) );
  141. add_post_meta( $quote_id, 'order_vat_exempt', $this->_encode($order_vat_exempt) );
  142. add_post_meta( $quote_id, 'currency', $this->_encode($currency) );
  143. add_post_meta( $quote_id, 'prices_include_tax', $this->_encode($prices_include_tax) );
  144. add_post_meta( $quote_id, 'customer_ip_address', $this->_encode($customer_ip_address) );
  145. add_post_meta( $quote_id, 'customer_user_agent', $this->_encode($customer_user_agent) );
  146. add_post_meta( $quote_id, 'customer_note', $this->_encode($customer_note) );
  147. add_post_meta( $quote_id, 'payment_method', $this->_encode($payment_method) );
  148. add_post_meta( $quote_id, 'shipping_total', $this->_encode($shipping_total) );
  149. add_post_meta( $quote_id, 'discount_total', $this->_encode($discount_total) );
  150. add_post_meta( $quote_id, 'discount_tax', $this->_encode($discount_tax) );
  151. add_post_meta( $quote_id, 'cart_tax', $this->_encode($cart_tax) );
  152. add_post_meta( $quote_id, 'shipping_tax', $this->_encode($shipping_tax) );
  153. add_post_meta( $quote_id, 'total', $this->_encode($total) );
  154. $result = array(
  155. 'result' => 'success',
  156. 'redirect' => $response->paymentUrl
  157. );
  158. if ( is_ajax() ) {
  159. wp_send_json( $result );
  160. } else {
  161. wp_redirect( $result['redirect'] );
  162. exit;
  163. }
  164. }
  165. }