// Render Custom Bundle Form Shortcode
function ssv_render_custom_bundle_form() {
ob_start();
// Check if the current user has the VIP or Admin role
$ssv_user = wp_get_current_user();
$ssv_is_vip_or_admin = in_array('vip', (array) $ssv_user->roles) || in_array('administrator', (array) $ssv_user->roles);
// Define prices for different roles
$ssv_prices = [
'vip' => [
"2000" => "9.6",
"3000" => "14.5",
"4000" => "19.5",
"5000" => "23.5",
"6000" => "30.3",
"7000" => "35",
"8000" => "37",
"10000" => "43.5",
"12000" => "51.2",
"15000" => "64",
"20000" => "83",
"25000" => "102",
"30000" => "123.5",
"35000" => "139.5",
"40000" => "161",
"50000" => "198.5",
"100000" => "398",
],
'customer' => [
"2000" => "9.6",
"3000" => "14.5",
"4000" => "19.5",
"5000" => "23.5",
"6000" => "30.3",
"7000" => "35",
"8000" => "37",
"10000" => "43.5",
"12000" => "51.2",
"15000" => "64",
"20000" => "83",
"25000" => "102",
"30000" => "123.5",
"35000" => "139.5",
"40000" => "161",
"50000" => "198.5",
"100000" => "398",
],
'wholesaler' => [
"2000" => "9.00",
"3000" => "13",
"4000" => "18",
"5000" => "22",
"6000" => "26.3",
"7000" => "30.3",
"8000" => "34.4",
"10000" => "40",
"12000" => "48.2",
"15000" => "60",
"20000" => "79",
"25000" => "98.5",
"30000" => "118.5",
"40000" => "156",
"50000" => "194.5",
"100000" => "388",
]
];
// Determine the appropriate pricing based on user role
$ssv_current_role = $ssv_is_vip_or_admin ? 'vip' : (in_array('wholesaler', (array) $ssv_user->roles) ? 'wholesaler' : 'customer');
$ssv_selected_prices = $ssv_prices[$ssv_current_role];
?>
wallet->get_wallet_balance($ssv_user_id);
$ssv_wallet_balance = floatval(preg_replace('/[^0-9.]/', '', strip_tags($ssv_wallet_balance))); // Ensure numeric value
if ($ssv_wallet_balance < $ssv_package_price) {
wp_send_json(array('success' => false, 'message' => 'Insufficient balance in your wallet.'));
return;
}
// Deduct amount from wallet
$ssv_result = woo_wallet()->wallet->debit($ssv_user_id, $ssv_package_price, 'Bundle purchase deduction for ' . $ssv_package_size . ' MB');
if (!$ssv_result) {
wp_send_json(array('success' => false, 'message' => 'Your balance is low, kindly top up your account and try again.'));
return;
}
// Create WooCommerce Order
$ssv_order = wc_create_order();
$ssv_order->set_customer_id($ssv_user_id);
// Add product to order
$ssv_product_id = 34869 ; // Replace with your WooCommerce product ID
$ssv_order->add_product(wc_get_product($ssv_product_id), 1, array(
'subtotal' => $ssv_package_price,
'total' => $ssv_package_price,
));
// Set billing details
$ssv_user_meta = get_user_meta($ssv_user_id);
$ssv_order->set_address(array(
'first_name' => $ssv_user_meta['first_name'][0] ?? '',
'last_name' => $ssv_user_meta['last_name'][0] ?? '',
'email' => wp_get_current_user()->user_email,
'phone' => $ssv_recipient,
), 'billing');
// Set payment method to wallet payment
$ssv_order->set_payment_method('wallet'); // Ensure 'wallet' matches the payment method ID in WooCommerce
$ssv_order->set_payment_method_title('Wallet Payment'); // This is optional, for display purposes
// Add custom attribute to order meta
$ssv_bundle_size_attribute = ($ssv_package_size / 1000);
$ssv_order->update_meta_data('gb_size', $ssv_bundle_size_attribute);
$ssv_order->update_meta_data('network', 'mtn');
// Finalize order
$ssv_order->calculate_totals();
$ssv_order->update_status('processing', 'Order created after successful bundle purchase.');
// Get order ID
$ssv_order_id = $ssv_order->get_id();
// Respond with success message including order details
wp_send_json(array(
'success' => true,
'reference' => $ssv_order_id,
'order_id' => $ssv_order_id,
));
}
add_action('wp_ajax_ssv_handle_custom_bundle_form_submission', 'ssv_handle_custom_bundle_form_submission');
add_action('wp_ajax_nopriv_ssv_handle_custom_bundle_form_submission', 'ssv_handle_custom_bundle_form_submission');
// Register the API endpoint
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/bundle', array(
'methods' => 'POST',
'callback' => 'ssv_handle_api_request',
'permission_callback' => '__return_true', // Adjust permissions as needed
));
});
/// Register the API endpoint
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/bundle', array(
'methods' => 'POST',
'callback' => 'ssv_handle_api_request',
'permission_callback' => '__return_true', // Adjust permissions as needed
));
});
// Handle API request
function ssv_handle_api_request(WP_REST_Request $request) {
// Get parameters from the request
$auth_code = sanitize_text_field($request->get_param('auth_code'));
$beneficiary_number = sanitize_text_field($request->get_param('beneficiary_number'));
$account_ref = sanitize_text_field($request->get_param('account_ref'));
$amount = floatval($request->get_param('amount'));
$products = $request->get_param('products'); // Assuming products is an array
$transaction_id = sanitize_text_field($request->get_param('transaction_id')); // New key
$timestamp = sanitize_text_field($request->get_param('timestamp')); // New key
// Validate input
if (empty($auth_code) || empty($beneficiary_number) || empty($account_ref) || $amount <= 0 || empty($products) || empty($transaction_id) || empty($timestamp)) {
return new WP_Error('invalid_input', 'Invalid input parameters.', array('status' => 400));
}
// Here you can add your logic to process the request
// For example, check the auth_code, process the payment, etc.
// Example response
return new WP_REST_Response(array(
'success' => true,
'message' => 'Bundle sent successfully.',
'data' => array(
'beneficiary_number' => $beneficiary_number,
'account_ref' => $account_ref,
'amount' => $amount,
'products' => $products,
'transaction_id' => $transaction_id, // Include new key in response
'timestamp' => $timestamp, // Include new key in response
),
), 200);
}