0], $atts, 'formipay');
if ($atts['form'] == 0) {
return false;
}
$post_id = intval($atts['form']);
$currency = 'Rp. ';
if ($post_id > 0) {
$currency = formipay_post_currency($post_id);
self::$form_ids[] = $post_id;
}
$isDonation = formipay_is_donation($post_id);
$form_settings = get_post_meta($post_id, 'formipay_settings', true);
$point_symbol = formipay_get_post_meta($post_id, 'multistep_point_symbol');
$point_icons = formipay_get_post_meta($post_id, 'multistep_point_icons');
$page_breaks = [];
if(!empty($form_settings['fields'])){
foreach($form_settings['fields'] as $field){
if($field['field_type'] == 'page_break'){
if($point_symbol == 'icon'){
$icons = json_decode($point_icons, true);
foreach($icons as $icon){
if($icon['field_id'] == $field['field_id']){
$field['icon'] = $icon['script'];
}
}
}
$page_breaks[] = $field;
}
}
}
ob_start();
$timezone = wp_timezone_string();
$require_login = formipay_get_post_meta($post_id, 'require_login') === 'on';
$scheduled = formipay_get_post_meta($post_id, 'schedule_toggle') === 'on';
$render = true;
if ($require_login && !is_user_logged_in()) {
$render = false;
echo '
' . wp_kses_post(formipay_get_post_meta($post_id, 'require_login_message')) . '
';
} elseif ($scheduled) {
$date_range = explode(',', formipay_get_post_meta($post_id, 'daterange'));
$date_from = new DateTime($date_range[0], new DateTimeZone($timezone));
$date_to = new DateTime($date_range[1], new DateTimeZone($timezone));
$now = new DateTime('now', new DateTimeZone($timezone));
if ($date_from > $now) {
$render = false;
?>
$date_to) {
$render = false;
?>
0) {
$this->public_enqueue_scripts();
$button_background_color = json_decode(formipay_get_post_meta($post_id, 'button_bg_color'), true );
$button_text_color = json_decode(formipay_get_post_meta($post_id, 'button_text_color'), true );
$button_border_color = json_decode( formipay_get_post_meta($post_id, 'button_border_color'), true );
?>
$method($field);
} else {
// Optionally handle unknown field types
do_action('formipay/render/custom-field-type', $type, $field);
}
}
/**
* Build a simple cart from static products and static items meta
*/
private function build_cart_from_meta($post_id){
$currency_full = formipay_post_currency($post_id); // e.g., "IDR:::Indonesian rupiah:::Rp"
$parts = explode(':::', (string) $currency_full);
$currency_code = $parts[0] ?? 'IDR';
$lines = [];
$subtotal = 0.0;
// 1) Static PRODUCTS (IDs stored as comma-separated string)
$ids_raw = (string) formipay_get_post_meta($post_id, 'static_products');
$ids = array_filter(array_map('absint', explode(',', $ids_raw)));
foreach ($ids as $pid) {
$pname = get_the_title($pid);
$price = $this->get_product_price_for_currency($pid, $currency_code);
$qty = 1; // non-donation, no in-cart items => default 1
$line_total = (float) $price * $qty;
$subtotal += $line_total;
$lines[] = [
'type' => 'product',
'id' => $pid,
'name' => $pname,
'qty' => $qty,
'unit' => (float) $price,
'total'=> $line_total,
];
}
// 2) Static ITEMs (JSON array with currency-specific amounts)
$raw = formipay_get_post_meta($post_id, 'static_items');
if ($raw) {
$items = json_decode((string) $raw, true) ?: [];
foreach ($items as $it) {
$label = $it['label'] ?? 'Item';
$qty = (int) ($it['quantity'] ?? 1);
$k = 'amount_' . $currency_code;
$amt = (float) ($it[$k] ?? 0);
$line_total = $amt * $qty;
$subtotal += $line_total;
$lines[] = [
'type' => 'item',
'id' => null,
'name' => $label,
'qty' => $qty,
'unit' => $amt,
'total'=> $line_total,
];
}
}
// Placeholders for future rules
$discount = 0.0;
$shipping = 0.0;
$tax = 0.0;
$grand = max($subtotal - $discount + $shipping + $tax, 0);
return compact('currency_code','lines','subtotal','discount','shipping','tax','grand');
}
/**
* Resolve a product's price for a given currency code using common meta keys.
* Fallback order: product_price_{CODE} → price_{CODE} → product_price → price → 0
*/
private function get_product_price_for_currency($product_id, $currency_code){
$cands = [
'product_price_' . $currency_code,
'price_' . $currency_code,
'product_price',
'price',
];
foreach ($cands as $key) {
$val = formipay_get_post_meta($product_id, $key);
if ($val !== '' && $val !== null) {
return (float) $val;
}
}
return 0.0;
}
/**
* Render payment options
*/
private function render_payment_options($post_id) {
$payments = apply_filters( 'formipay/frontend/payment-list', [], $post_id );
?>
';
}
if(!empty($page_breaks)){
?>
admin_url('admin-ajax.php'),
'frontend_nonce' => wp_create_nonce('formipay-frontend-nonce'),
'nonce' => wp_create_nonce('formipay_order_submit'),
'forms' => $this->get_form_data()
];
wp_localize_script('formipay-form', 'formipay_form', $form_data);
// Localize shipping labels for checkout
$shipping_data = [
'labels' => [
'country' => __('Shipping Country', 'formipay'),
'selectCountry' => __('Select your country', 'formipay'),
'shippingMethod' => __('Shipping Method', 'formipay'),
]
];
wp_localize_script('formipay-checkout-shipping', 'formipay_shipping', $shipping_data);
}
/**
* Resolve currency UI/config for a given 3-letter currency code from wp_options.
*/
private function resolve_currency_config($currency_code){
$opts = get_option('formipay_settings', []);
// Defaults from global default_* settings
$default_full = isset($opts['default_currency']) ? (string)$opts['default_currency'] : 'IDR:::Indonesian rupiah:::Rp';
$def_parts = explode(':::', $default_full);
$def_symbol = $def_parts[2] ?? '';
$cfg = [
'currency' => $def_symbol,
'decimal_digits' => isset($opts['default_currency_decimal_digits']) ? (int)$opts['default_currency_decimal_digits'] : 2,
'decimal_symbol' => isset($opts['default_currency_decimal_symbol']) ? (string)$opts['default_currency_decimal_symbol'] : '.',
'thousand_separator' => isset($opts['default_currency_thousand_separator']) ? (string)$opts['default_currency_thousand_separator'] : ',',
];
if (!empty($opts['multicurrencies']) && is_array($opts['multicurrencies'])) {
foreach ($opts['multicurrencies'] as $mc) {
if (empty($mc['currency'])) continue;
$parts = explode(':::', (string)$mc['currency']);
$code = $parts[0] ?? '';
if (strtoupper($code) !== strtoupper($currency_code)) continue;
$symbol = $parts[2] ?? '';
if ($symbol !== '') $cfg['currency'] = $symbol;
if ($mc['decimal_digits'] !== '') $cfg['decimal_digits'] = (int)$mc['decimal_digits'];
if ($mc['decimal_symbol'] !== '') $cfg['decimal_symbol'] = (string)$mc['decimal_symbol'];
if ($mc['thousand_separator'] !== '') $cfg['thousand_separator'] = (string)$mc['thousand_separator'];
break;
}
}
return $cfg;
}
/**
* Build the allowed currency list for a form, including UI config per currency.
* Uses form meta 'allowed_currencies' (JSON array of "CODE:::Title:::Symbol")
* and 'default_currencies' (single string) to set default.
*/
private function resolve_allowed_currencies($post_id){
// Allowed on the form
$raw = formipay_get_post_meta($post_id, 'allowed_currencies');
$allowed = [];
if (!empty($raw)) {
$arr = json_decode((string)$raw, true);
if (is_array($arr)) $allowed = $arr;
}
// Fallback to all global currencies if the form has none
if (empty($allowed)) {
$opts = get_option('formipay_settings', []);
if (!empty($opts['multicurrencies']) && is_array($opts['multicurrencies'])) {
foreach ($opts['multicurrencies'] as $mc) {
if (!empty($mc['currency'])) $allowed[] = (string)$mc['currency'];
}
}
}
// Default for the form
$default_full = formipay_get_post_meta($post_id, 'default_currencies');
if (empty($default_full)) {
$opts = get_option('formipay_settings', []);
$default_full = $opts['default_currency'] ?? 'IDR:::Indonesian rupiah:::Rp';
}
$def_parts = explode(':::', (string)$default_full);
$default_code = $def_parts[0] ?? 'IDR';
// Compose structured list
$list = [];
foreach ($allowed as $cur_full) {
$parts = explode(':::', (string)$cur_full);
$code = $parts[0] ?? '';
$title = $parts[1] ?? '';
$symbol = $parts[2] ?? '';
if (!$code) continue;
$cfg = $this->resolve_currency_config($code);
if ($symbol !== '') $cfg['currency'] = $symbol; // prefer explicit symbol in the tuple
$list[] = [
'code' => $code,
'title' => $title,
'symbol' => $cfg['currency'],
'decimal_digits' => (int)$cfg['decimal_digits'],
'decimal_symbol' => (string)$cfg['decimal_symbol'],
'thousand_separator' => (string)$cfg['thousand_separator'],
];
}
return [
'default_code' => $default_code,
'list' => $list,
];
}
private function get_form_data(){
$form_data = [];
foreach (array_unique(self::$form_ids) as $post_id) {
$allowed_currency_pack = $this->resolve_allowed_currencies($post_id);
$currency_code = $allowed_currency_pack['default_code'];
$currency_cfg = $this->resolve_currency_config($currency_code);
// Get form shipping settings
$form_settings = get_post_meta($post_id, 'formipay_form_settings', true);
$shipping_enabled = $form_settings['shipping_enabled'] ?? 'no_shipping';
$form_data[$post_id] = [
'form_id' => $post_id,
'currency' => formipay_post_currency($post_id),
'currency_code' => $currency_code, // active on load
'currency' => $currency_cfg['currency'],
'decimal_digits' => $currency_cfg['decimal_digits'],
'decimal_symbol' => $currency_cfg['decimal_symbol'],
'thousand_separator' => $currency_cfg['thousand_separator'],
'allowed_currency_pack' => $allowed_currency_pack,
'buyer_phone_field' => formipay_get_post_meta($post_id, 'buyer_phone'),
'buyer_country_field' => formipay_get_post_meta($post_id, 'buyer_country'),
'buyer_phone_allow' => (bool) formipay_get_post_meta($post_id, 'buyer_allow_choose_country_code'),
'buyer_phone_country_code' => formipay_get_post_meta($post_id, 'buyer_phone_country_code'),
'notice_empty_text_message' => formipay_get_post_meta($post_id, 'empty_required_text_field'),
'notice_empty_select_message' => formipay_get_post_meta($post_id, 'empty_required_select_field'),
'notice_empty_agreement_message' => formipay_get_post_meta($post_id, 'empty_required_agreement_field'),
'quantity_toggle' => formipay_get_post_meta($post_id, 'product_quantity_toggle'),
'quantity_step' => formipay_get_post_meta($post_id, 'product_quantity_range'),
'quantity_min' => formipay_get_post_meta($post_id, 'product_minimum_purchase'),
'quantity_stock' => formipay_get_post_meta($post_id, 'product_stock'),
'button_text' => formipay_get_post_meta($post_id, 'button_text'),
'button_processing_text' => formipay_get_post_meta($post_id, 'button_processing_text'),
'isPopup' => formipay_isPopup($post_id),
'trigger_selector' => formipay_get_post_meta($post_id, 'popup_click_selector') ?
formipay_get_post_meta($post_id, 'popup_trigger_selector') :
'.formipay-open-popup-button',
'modal_selector' => '#formipay-popup-' . $post_id,
'static_products' => array_filter(array_map('absint', explode(',', (string) formipay_get_post_meta($post_id, 'static_products')))),
'static_items' => json_decode((string) formipay_get_post_meta($post_id, 'static_items'), true) ?: [],
'currency_code' => (function($c){ $p = explode(':::', (string)$c); return $p[0] ?? 'IDR'; })(formipay_post_currency($post_id)),
'shipping_enabled' => $shipping_enabled, // Form-level shipping setting
];
}
return $form_data;
}
}
?>