| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
require_once(__DIR__ . '/flutter-base.php'); |
| 8 |
|
| 9 |
/* |
| 10 |
* Base REST Controller for flutter |
| 11 |
* |
| 12 |
* @since 1.4.0 |
| 13 |
* |
| 14 |
* @package SmartCOD |
| 15 |
*/ |
| 16 |
class FlutterSmartCOD extends FlutterBaseController |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Endpoint namespace |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $namespace = 'api/flutter_smart_cod'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Register all routes related with stores |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function __construct() |
| 31 |
{ |
| 32 |
add_filter( 'flutter_woo_payment_method_response', array($this, 'check_smart_cod'), 10, 3 ); |
| 33 |
add_action( 'woocommerce_order_after_calculate_totals', array( $this, 'recalculate_order_risk_free_after_calculate_totals' ), 10, 2 ); |
| 34 |
add_action('rest_api_init', array($this, 'register_flutter_smart_cod_routes')); |
| 35 |
} |
| 36 |
|
| 37 |
public function check_smart_cod($result, $payment_gateway, $cart) |
| 38 |
{ |
| 39 |
if ( class_exists( 'Wc_Smart_Cod_Admin' ) ) { |
| 40 |
if ( $payment_gateway instanceof Wc_Smart_Cod_Admin ) { |
| 41 |
require_once(plugin_dir_path(__FILE__) . 'helpers/extensions/flutter-wc-smart-cod-public.php'); |
| 42 |
$obj = new Flutter_Wc_Smart_Cod_Public(); |
| 43 |
|
| 44 |
$extra_fee = $obj->get_extra_fee($cart); |
| 45 |
$risk_free_cod = $obj->get_risk_free_cod($cart, $extra_fee); |
| 46 |
|
| 47 |
if ($risk_free_cod != null) { |
| 48 |
if (!empty($risk_free_cod['description'])) { |
| 49 |
$result['description'] = str_replace("\r\n", "<br>", $result['description']).'<br /> <br />'.$risk_free_cod['description']; |
| 50 |
} |
| 51 |
if ( array_key_exists( 'description', $risk_free_cod ) ) { |
| 52 |
unset( $risk_free_cod[ 'description' ] ); |
| 53 |
} |
| 54 |
$result['smart_cod'] = array_merge([ |
| 55 |
'is_risk_free_enabled' => true, |
| 56 |
'extra_fee' => $extra_fee, |
| 57 |
], $risk_free_cod); |
| 58 |
} else { |
| 59 |
$result['smart_cod'] = [ |
| 60 |
'is_risk_free_enabled' => false, |
| 61 |
'extra_fee' => $extra_fee, |
| 62 |
]; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
public function recalculate_order_risk_free_after_calculate_totals( $and_taxes, $order ) { |
| 70 |
if ( ! is_a( $order, 'WC_Order' ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
$wsc_advance_total = $order->get_meta('wsc_advance_total'); |
| 74 |
$connected_order_id = $order->get_meta( 'wsc_advance_order_id' ); |
| 75 |
if (!empty($wsc_advance_total) && !$connected_order_id) { |
| 76 |
$order->set_total($wsc_advance_total); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function register_flutter_smart_cod_routes() |
| 81 |
{ |
| 82 |
register_rest_route($this->namespace, '/order_success', array( |
| 83 |
array( |
| 84 |
'methods' => "POST", |
| 85 |
'callback' => array($this, 'update_order_success'), |
| 86 |
'permission_callback' => function () { |
| 87 |
return parent::checkApiPermission(); |
| 88 |
} |
| 89 |
), |
| 90 |
)); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
public function update_order_success($request) |
| 95 |
{ |
| 96 |
$json = file_get_contents('php://input'); |
| 97 |
$body = json_decode($json, TRUE); |
| 98 |
$order_id = sanitize_text_field($body['order_id']); |
| 99 |
|
| 100 |
$order = wc_get_order( $order_id ); |
| 101 |
if ( ! $order ) { |
| 102 |
return new WP_Error('order_not_found', 'Order not found.', array('status' => 404)); |
| 103 |
} |
| 104 |
|
| 105 |
// These routes take an order id from an unauthenticated caller, so bind the |
| 106 |
// request to the order before acting on it. |
| 107 |
$key_check = mstore_api_check_payment_order_key($order, $body, true); |
| 108 |
if (is_wp_error($key_check)) { |
| 109 |
return $key_check; |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! $this->is_rf_order( $order ) ) { |
| 113 |
return ['success' => true]; |
| 114 |
} |
| 115 |
|
| 116 |
$settings = get_option( 'woocommerce_smartcod_riskfree_settings' ); |
| 117 |
|
| 118 |
if ( isset( $settings['disable_additional_order'] ) && $settings['disable_additional_order'] === 'yes' ) { |
| 119 |
$order->calculate_totals(); |
| 120 |
if ( !isset( $settings['use_advance_payment_method'] ) || $settings['use_advance_payment_method'] === 'no' ) { |
| 121 |
|
| 122 |
$order->add_meta_data( 'original_payment_method', $order->get_payment_method() ); |
| 123 |
$order->set_payment_method('cod'); |
| 124 |
} |
| 125 |
|
| 126 |
$order->save(); |
| 127 |
return ['success' => true]; |
| 128 |
} |
| 129 |
|
| 130 |
$connected_order_id = $order->get_meta( 'wsc_advance_order_id' ); |
| 131 |
if ( $connected_order_id ) { |
| 132 |
// processed |
| 133 |
return ['success' => true]; |
| 134 |
} |
| 135 |
|
| 136 |
$advance_amount = $order->get_total(); |
| 137 |
|
| 138 |
/** |
| 139 |
* Create risk free |
| 140 |
* advance amount order |
| 141 |
*/ |
| 142 |
|
| 143 |
$force_cod_method = isset( $settings['force_payment_method_cod'] ) && $settings['force_payment_method_cod'] === 'yes'; |
| 144 |
|
| 145 |
$_order_id = $this->create_wc_order( $order, $advance_amount, $force_cod_method ); |
| 146 |
|
| 147 |
/** |
| 148 |
* Subtract the advance |
| 149 |
*/ |
| 150 |
$item = new WC_Order_Item_Fee(); |
| 151 |
$item->set_name( __( 'Advance amount', 'mstore-api' ) ); |
| 152 |
$item->set_total( - $advance_amount ); |
| 153 |
$item->add_meta_data( 'is_wsc_advance_fee', true ); |
| 154 |
$item->set_tax_status( 'none' ); |
| 155 |
$item->set_tax_class( '' ); |
| 156 |
$item->save(); |
| 157 |
|
| 158 |
if ( class_exists( 'PaytabsHelper' ) ) { |
| 159 |
/** |
| 160 |
* Remove the previously added |
| 161 |
* negative Item Fee |
| 162 |
*/ |
| 163 |
foreach ( $order->get_items( 'fee' ) as $item_id => $item_fee ) { |
| 164 |
if ( $item_fee->get_meta( 'is_wsc_rf_ramount' ) === '1' ) { |
| 165 |
$order->remove_item( $item_id ); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
$order->add_item( $item ); |
| 172 |
$order->add_meta_data( 'wsc_advance_order_id', $_order_id ); |
| 173 |
$order->set_payment_method( 'cod' ); |
| 174 |
$order->calculate_totals( false ); |
| 175 |
|
| 176 |
if ( isset( $settings['change_order_status'] ) && $settings['change_order_status'] !== '0' ) { |
| 177 |
$new_status = str_replace( 'wc-', '', $settings['change_order_status'] ); |
| 178 |
$order->update_status( $new_status ); |
| 179 |
} |
| 180 |
|
| 181 |
$order->save(); |
| 182 |
return ['success' => true]; |
| 183 |
} |
| 184 |
|
| 185 |
private function is_rf_order( $order ) { |
| 186 |
return $order->get_meta( 'is_wsc_rf' ) === '1'; |
| 187 |
} |
| 188 |
|
| 189 |
private function create_wc_order( $original_order, $advance_amount, $force_cod_method ) { |
| 190 |
$order = new WC_Order(); |
| 191 |
|
| 192 |
$data = $original_order->get_data(); |
| 193 |
|
| 194 |
$billing = $data['billing']; |
| 195 |
$shipping = $data['shipping']; |
| 196 |
foreach ( $billing as $key => $value ) { |
| 197 |
if ( is_callable( array( $order, "set_billing_{$key}" ) ) ) { |
| 198 |
$order->{"set_billing_{$key}"}( $value ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
foreach ( $shipping as $key => $value ) { |
| 203 |
if ( is_callable( array( $order, "set_shipping_{$key}" ) ) ) { |
| 204 |
$order->{"set_shipping_{$key}"}( $value ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
$payment_method = $data['payment_method']; |
| 209 |
|
| 210 |
$order->set_payment_method( $force_cod_method ? 'cod' : $payment_method ); |
| 211 |
$order->set_created_via( 'programatically' ); |
| 212 |
$order->set_customer_id( get_current_user_id() ); |
| 213 |
$order->set_currency( get_woocommerce_currency() ); |
| 214 |
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); |
| 215 |
|
| 216 |
$item = new WC_Order_Item_Fee(); |
| 217 |
$item->set_name( "Advance amount for cash on delivery order through $payment_method" ); |
| 218 |
$item->set_total( $advance_amount ); |
| 219 |
$item->set_tax_class( '' ); |
| 220 |
$item->set_tax_status( 'none' ); |
| 221 |
$item->save(); |
| 222 |
|
| 223 |
$order->add_item( $item ); |
| 224 |
$order->calculate_totals( false ); |
| 225 |
$order->add_meta_data( 'is_wsc_advance', 1 ); |
| 226 |
$order->add_meta_data( 'original_payment_method', $data['payment_method'] ); |
| 227 |
if ( $original_order->is_paid() ) { |
| 228 |
$order->set_status( 'completed' ); |
| 229 |
} |
| 230 |
$order_id = $order->save(); |
| 231 |
|
| 232 |
// Returns the order ID |
| 233 |
return $order_id; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
new FlutterSmartCOD; |
| 238 |
|