first commit
This commit is contained in:
725
includes/Payment/BankTransfer.php
Normal file
725
includes/Payment/BankTransfer.php
Normal file
@@ -0,0 +1,725 @@
|
||||
<?php
|
||||
namespace Formipay\Payment;
|
||||
use Formipay\Traits\SingletonTrait;
|
||||
use Formipay\Order as Order;
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
class BankTransfer extends Payment {
|
||||
|
||||
use SingletonTrait;
|
||||
|
||||
private $gateway = 'bank_transfer';
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
parent::__construct();
|
||||
|
||||
add_action( 'init', [$this, 'create_db'] );
|
||||
add_action( 'formipay/order/new', [$this, 'add_transaction'], 99 );
|
||||
add_action( 'formipay/order/payment-timeout', [$this, 'auto_cancel_order_on_timeout'] );
|
||||
|
||||
add_filter( 'formipay/order/order-details', [$this, 'add_unique_code_details'], 89, 3 );
|
||||
add_filter( 'formipay/form-config/tab:payments/gateways', [$this, 'add_gateway'] );
|
||||
add_filter( 'formipay/form-config/tab:payments', [$this, 'add_bank_transfer_settings'] );
|
||||
add_filter( 'formipay/global-settings/tab:payment', [$this, 'add_settings'] );
|
||||
add_filter( 'formipay/order/payment-details/bank_transfer', [$this, 'render_payment_details'], 999, 4 );
|
||||
|
||||
add_action('wp_ajax_formipay_bank_transfer_confirmation', [$this, 'handle_formipay_bank_transfer_confirmation'] );
|
||||
add_action('wp_ajax_nopriv_formipay_bank_transfer_confirmation', [$this, 'handle_formipay_bank_transfer_confirmation'] );
|
||||
|
||||
add_filter('formipay/notification/order/email/action-button', [$this, 'render_email_action_button'], 99, 3 );
|
||||
|
||||
}
|
||||
|
||||
public function create_db() {
|
||||
|
||||
global $wpdb;
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$create[] = "CREATE TABLE `{$wpdb->base_prefix}formipay_bank_transfer_trx` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`created_date` datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
`form_id` int DEFAULT 0,
|
||||
`order_id` int DEFAULT 0,
|
||||
`channel` text NOT NULL,
|
||||
`total` float(10, 2) DEFAULT 0,
|
||||
`unique_code` int DEFAULT 0,
|
||||
`meta_data` text,
|
||||
PRIMARY KEY (`id`)
|
||||
) $charset_collate;";
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
dbDelta($create);
|
||||
|
||||
}
|
||||
|
||||
public function add_transaction($order_data) {
|
||||
global $wpdb;
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
$timeout = isset($formipay_settings['bank_transfer_timeout']) ? (int) $formipay_settings['bank_transfer_timeout'] : 1440;
|
||||
$table = $wpdb->prefix . 'formipay_bank_transfer_trx';
|
||||
|
||||
if ($order_data['payment_gateway'] == 'bank_transfer') {
|
||||
$submit_args = [
|
||||
'created_date' => formipay_date('Y-m-d H:i:s', strtotime($order_data['created_date'])),
|
||||
'form_id' => intval($order_data['form_id']),
|
||||
'order_id' => intval($order_data['id']),
|
||||
'channel' => sanitize_text_field(str_replace($order_data['payment_gateway'].':::', '', $order_data['form_data']['payment'])),
|
||||
'total' => floatval($order_data['total']),
|
||||
'unique_code' => sanitize_text_field($this->check_unique_code()),
|
||||
'meta_data' => maybe_serialize(array())
|
||||
];
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$inserted = $wpdb->insert($table, $submit_args);
|
||||
|
||||
wp_schedule_single_event(
|
||||
(int) formipay_date('timestamp') + $timeout, 'formipay/order/payment-timeout', [
|
||||
'order_id' => $order_data['id'],
|
||||
'payment_gateway' => 'bank_transfer'
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function auto_cancel_order_on_timeout($order_id, $payment_gateway) {
|
||||
|
||||
if($payment_gateway == 'bank_transfer'){
|
||||
|
||||
global $wpdb;
|
||||
$order = formipay_get_order($order_id);
|
||||
if($order['status'] !== 'completed'){
|
||||
Order::update($order_id, [
|
||||
'status' => 'cancelled'
|
||||
]);
|
||||
}
|
||||
$this->cancel_payment($order);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function cancel_payment($order_data){
|
||||
do_action('formipay/notification/order', $order_data);
|
||||
}
|
||||
|
||||
public function check_unique_code() {
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'formipay_bank_transfer_trx';
|
||||
|
||||
// Get the highest existing ID efficiently
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$max_id = $wpdb->get_var("SELECT MAX(id) FROM $table");
|
||||
|
||||
$unique_code = !is_null($max_id) ? ($max_id + 1) : 1;
|
||||
|
||||
$operation = get_option('bank_transfer_unique_code_operation');
|
||||
return ($operation == 'add') ? $unique_code : "-{$unique_code}";
|
||||
}
|
||||
|
||||
public function add_unique_code_details($details, $form_id, $order_data){
|
||||
|
||||
if( floatval(formipay_get_post_meta($form_id, 'product_price')) > 0 &&
|
||||
$order_data['payment_gateway'] == 'bank_transfer'
|
||||
){
|
||||
|
||||
$details[] = [
|
||||
'item' => __( 'Unique Code', 'formipay' ),
|
||||
'amount' => $this->check_unique_code(),
|
||||
'subtotal' => floatval($this->check_unique_code()),
|
||||
'context' => floatval($this->check_unique_code()) < 0 ? 'sub' : 'add'
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
return $details;
|
||||
}
|
||||
|
||||
public function add_gateway($gateways){
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
|
||||
if(isset($formipay_settings[$this->gateway.'_toggle']) && false !== $formipay_settings[$this->gateway.'_toggle']){
|
||||
|
||||
if(isset($formipay_settings[ $this->gateway.'_channels' ]) && !empty($formipay_settings[ $this->gateway.'_channels' ])){
|
||||
$channels = $formipay_settings[ $this->gateway.'_channels' ];
|
||||
$channel_options = [];
|
||||
if(!empty($channels)){
|
||||
foreach($channels as $index => $channel){
|
||||
$id = $this->gateway.':::'.$channel[$this->gateway.'_name'].'-'.$index;
|
||||
$gateways[] = [
|
||||
'id' => $id,
|
||||
'gateway' => __( 'Bank', 'formipay' ),
|
||||
'channel' => $channel[$this->gateway.'_name']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $gateways;
|
||||
|
||||
}
|
||||
|
||||
public function add_bank_transfer_settings($fields){
|
||||
|
||||
$banktransfer_field = array(
|
||||
$this->gateway.'_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Settings', 'formipay' ),
|
||||
'description' => __( 'This will be shown in thank-you page and implemented when buyer uses one of Bank Transfer\'s channels', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_text_section_intro' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Payment Details Intro', 'formipay' ),
|
||||
'value' => __( 'Please complete payment to:', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' )
|
||||
),
|
||||
$this->gateway.'_confirmation_submit_button' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Payment Confirmation Submit Button Text', 'formipay' ),
|
||||
'value' => __( 'Upload', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'group' => 'ended'
|
||||
),
|
||||
$this->gateway.'_confirmation_notice_message_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Confirmation Notice Messages', 'formipay' ),
|
||||
'description' => __(
|
||||
'Set notice message on every condition about attempting to confirm the payment', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_confirmation_update_to_status' => array(
|
||||
'type' => 'select',
|
||||
'label' => __( 'Change status order to', 'formipay' ),
|
||||
'options' => formipay_order_status_list(),
|
||||
'value' => 'payment-confirm',
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
),
|
||||
$this->gateway.'_confirmation_message_success' => array(
|
||||
'type' => 'hint_textarea',
|
||||
'label' => __( 'Successfully Confirmed Message', 'formipay' ),
|
||||
'hints' => array(
|
||||
'order_id' => __('Order ID', 'formipay')
|
||||
),
|
||||
'value' => __('Successfully confirmed payment for #{{order_id}}', 'formipay'),
|
||||
'description' => __('When successfully change the order status to the preferred one', 'formipay'),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
),
|
||||
$this->gateway.'_confirmation_message_already_confirmed' => array(
|
||||
'type' => 'hint_textarea',
|
||||
'label' => __( 'Has Been Confirmed Message', 'formipay' ),
|
||||
'hints' => array(
|
||||
'order_id' => __('Order ID', 'formipay')
|
||||
),
|
||||
'value' => __('#{{order_id}} has been confirmed', 'formipay'),
|
||||
'description' => __('When the order status has been the preferred one', 'formipay'),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
),
|
||||
$this->gateway.'_confirmation_message_error' => array(
|
||||
'type' => 'hint_textarea',
|
||||
'label' => __( 'Failed to Confirm Message', 'formipay' ),
|
||||
'hints' => array(
|
||||
'order_id' => __('Order ID', 'formipay'),
|
||||
'system_error_message' => __('System Error Message', 'formipay')
|
||||
),
|
||||
'value' => __('Failed to proceed. Error: {{system_error_message}}', 'formipay'),
|
||||
'description' => __('When error to change the order status to the preferred one', 'formipay'),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'group' => 'ended'
|
||||
)
|
||||
);
|
||||
|
||||
foreach($banktransfer_field as $key => $value){
|
||||
$fields[$key] = $value;
|
||||
}
|
||||
|
||||
return $fields;
|
||||
|
||||
}
|
||||
|
||||
public function add_settings($settings) {
|
||||
$bank_transfer = array(
|
||||
'my_code_field' => [
|
||||
'type' => 'ace_editor',
|
||||
'label' => __('Custom Code', 'formipay'),
|
||||
'lang' => 'javascript', // or 'css', 'html', etc.
|
||||
'theme' => 'chrome',
|
||||
'value' => '', // default value
|
||||
'required' => true,
|
||||
],
|
||||
$this->gateway.'_toggle' => array(
|
||||
'type' => 'checkbox',
|
||||
'label' => __('Activate Bank Transfer', 'formipay'),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' )
|
||||
),
|
||||
$this->gateway.'_general_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'General', 'formipay' ),
|
||||
'group' => 'started',
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' )
|
||||
),
|
||||
$this->gateway.'_timeout' => array(
|
||||
'type' => 'number',
|
||||
'label' => __( 'Payment Timeout (minute)', 'formipay' ),
|
||||
'description' => __( 'Set a timeout to wait for payment. After this timeout is up, the order status automatically changes to cancelled.', 'formipay' ),
|
||||
'value' => 120,
|
||||
'group' => 'ended',
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' )
|
||||
),
|
||||
$this->gateway.'_unique_codes' => array(
|
||||
'type' => 'repeater',
|
||||
'label' => esc_html__( 'Unique Codes', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'description' => __( 'Get unique total amount by adding unique code.', 'formipay' ),
|
||||
'fields' => array(
|
||||
'unique_code_currency' => array(
|
||||
'type' => 'select',
|
||||
'label' => __( 'Currency', 'formipay' ),
|
||||
'description' => __( 'This setting is only applied when the selected currency matches the form currency.', 'formipay' ),
|
||||
'options' => formipay_currency_as_options(),
|
||||
'is_group_title' => true,
|
||||
'searchable' => true,
|
||||
'required' => true
|
||||
),
|
||||
'unique_code_toggle' => array(
|
||||
'type' => 'checkbox',
|
||||
'label' => __( 'Activate', 'formipay' ),
|
||||
'description' => __( 'Generate unique code for this currency.', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
),
|
||||
'unique_code_increment_step' => array(
|
||||
'type' => 'number',
|
||||
'label' => esc_html__( 'Increment Step', 'formipay' ),
|
||||
'value' => 0.01,
|
||||
'step' => 0.001,
|
||||
'required' => true
|
||||
),
|
||||
'unique_code_max' => array(
|
||||
'type' => 'number',
|
||||
'label' => esc_html__( 'Max Amount', 'formipay' ),
|
||||
'value' => 0.01,
|
||||
'step' => 0.001,
|
||||
'required' => true
|
||||
),
|
||||
'unique_code_operation' => array(
|
||||
'type' => 'select',
|
||||
'label' => __('Operation', 'formipay'),
|
||||
'options' => array(
|
||||
'sub' => __( '(-) Decrease Total Amount by Unique Code', 'formipay' ),
|
||||
'add' => __( '(+) Increase Total Amount by Unique Code', 'formipay' ),
|
||||
),
|
||||
'value' => 'add',
|
||||
'dependency'=> array(
|
||||
array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
array(
|
||||
'key' => $this->gateway.'_unique_code_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
'required' => true,
|
||||
),
|
||||
)
|
||||
),
|
||||
$this->gateway.'_confirmation_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Confirmation Form', 'formipay' ),
|
||||
'description' => __( 'for order that use Bank Transfer gateway only.', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => 'bank_transfer_toggle',
|
||||
'value' => 'not_empty',
|
||||
),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_confirmation_dropzone_color' => array(
|
||||
'type' => 'color',
|
||||
'label' => __( 'Dropzone Box Background color', 'formipay' ),
|
||||
'value' => '#cccccc',
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => 'bank_transfer_toggle',
|
||||
'value' => 'not_empty',
|
||||
),
|
||||
),
|
||||
$this->gateway.'_confirmation_dropzone_text_color' => array(
|
||||
'type' => 'color',
|
||||
'label' => __( 'Dropzone Box Text Color', 'formipay' ),
|
||||
'value' => '#808080',
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => 'bank_transfer_toggle',
|
||||
'value' => 'not_empty',
|
||||
),
|
||||
),
|
||||
$this->gateway.'_confirmation_dropzone_instruction' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Dropzone Instruction', 'formipay' ),
|
||||
'value' => __( 'Drag & drop a file here or click to select one', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => 'bank_transfer_toggle',
|
||||
'value' => 'not_empty',
|
||||
),
|
||||
),
|
||||
$this->gateway.'_confirmation_submit_button' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Submit Button', 'formipay' ),
|
||||
'value' => __( 'Upload', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => 'bank_transfer_toggle',
|
||||
'value' => 'not_empty',
|
||||
),
|
||||
'group' => 'ended'
|
||||
),
|
||||
$this->gateway.'_settings_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Account List', 'formipay' ),
|
||||
'description' => __( 'Add all of your preferred bank accounts to receive the payment', 'formipay' ),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_channels' => array(
|
||||
'type' => 'repeater',
|
||||
'label' => __('Bank Accounts', 'formipay'),
|
||||
'fields' => array(
|
||||
$this->gateway.'_name' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Bank Name', 'formipay' ),
|
||||
'required' => true,
|
||||
'is_group_title' => true
|
||||
),
|
||||
$this->gateway.'_account' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Bank Account', 'formipay' ),
|
||||
'required' => true
|
||||
),
|
||||
$this->gateway.'_account_holder' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Bank Account Holder', 'formipay' ),
|
||||
'required' => true
|
||||
),
|
||||
$this->gateway.'_logo' => array(
|
||||
'type' => 'image',
|
||||
'label' => __( 'Bank Logo', 'formipay' ),
|
||||
'required' => true
|
||||
),
|
||||
),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'submenu' => __( 'Bank Transfer', 'formipay' ),
|
||||
'group' => 'ended'
|
||||
)
|
||||
);
|
||||
|
||||
foreach($bank_transfer as $key => $value){
|
||||
$settings[$key] = $value;
|
||||
}
|
||||
|
||||
return $settings;
|
||||
|
||||
}
|
||||
|
||||
public function render_payment_details($render, $form_id, $order_data, $submit_action_type) {
|
||||
|
||||
if(in_array($order_data['status'], ['completed', 'payment-confirm'])){
|
||||
return $render;
|
||||
}
|
||||
|
||||
$get_order = formipay_get_order($order_data['id']);
|
||||
|
||||
$selected_payment = ''; // bank_transfer:::BNI-0
|
||||
$payment_gateway = '';
|
||||
if(!empty($get_order['form_data'])){
|
||||
foreach($get_order['form_data'] as $_form_data){
|
||||
if($_form_data['name'] == 'payment') {
|
||||
$selected_payment = $_form_data['value'];
|
||||
}
|
||||
if($_form_data['name'] == 'payment_gateway') {
|
||||
$payment_gateway = $_form_data['value'];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if('' !== $selected_payment){
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
if($formipay_settings[$payment_gateway.'_toggle'] !== false){
|
||||
$gateway_channel = [];
|
||||
if(!empty($formipay_settings[$payment_gateway.'_channels'])){
|
||||
foreach($formipay_settings[$payment_gateway.'_channels'] as $channel){
|
||||
if(strpos($selected_payment, $channel[$payment_gateway.'_name']) !== false){
|
||||
$gateway_channel = $channel;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!empty($gateway_channel)){
|
||||
if($submit_action_type == 'thankyou'){
|
||||
ob_start();
|
||||
?>
|
||||
<span><?php echo esc_html(formipay_get_post_meta($form_id, 'bank_transfer_text_section_intro')); ?></span>
|
||||
<?php
|
||||
if(!empty($gateway_channel['bank_transfer_logo'])){
|
||||
echo '<br>'.wp_get_attachment_image( $gateway_channel['bank_transfer_logo'], 'thumbnail', false );
|
||||
}
|
||||
?>
|
||||
<div class="payment-bank_transfer payment-response-container">
|
||||
<table id="payment-details">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th><?php echo esc_html__( 'Order ID', 'formipay' ); ?></th>
|
||||
<td><?php echo '#'.intval($order_data['id']); ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?php echo esc_html__('Bank Name', 'formipay'); ?></th>
|
||||
<td><?php echo esc_html($gateway_channel['bank_transfer_name']) ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?php echo esc_html__('Bank Account', 'formipay'); ?></th>
|
||||
<td>
|
||||
<p class="bank-account" data-copy-value="<?php echo esc_attr($gateway_channel['bank_transfer_account']) ?>">
|
||||
<b><?php echo esc_html($gateway_channel['bank_transfer_account']) ?></b>
|
||||
</p>
|
||||
<button class="formipay-copy-button"
|
||||
data-copy-text="<?php echo esc_attr__( 'Copy', 'formipay' ); ?>"
|
||||
data-copied-text="<?php echo esc_attr__( 'Copied', 'formipay' ); ?>"
|
||||
data-not-copied-text="<?php echo esc_attr__( 'Not copied!', 'formipay' ); ?>"
|
||||
>
|
||||
<i class="bi bi-copy"></i> <?php echo esc_html__( 'Copy', 'formipay' ); ?>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?php echo esc_html__('Bank Account Holder', 'formipay'); ?></th>
|
||||
<td><?php echo esc_html($gateway_channel['bank_transfer_account_holder']); ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?php echo esc_html__('Payment Total', 'formipay'); ?></th>
|
||||
<td>
|
||||
<p class="payment-total" data-copy-value="<?php echo esc_attr($order_data['total']) ?>">
|
||||
<b><?php echo esc_html(formipay_price_format($order_data['total'], $form_id)); ?></b>
|
||||
</p>
|
||||
<button class="formipay-copy-button"
|
||||
data-copy-text="<?php echo esc_attr__( 'Copy', 'formipay' ); ?>"
|
||||
data-copied-text="<?php echo esc_attr__( 'Copied', 'formipay' ); ?>"
|
||||
data-not-copied-text="<?php echo esc_attr__( 'Not copied!', 'formipay' ); ?>"
|
||||
>
|
||||
<i class="bi bi-copy"></i> <?php echo esc_html__( 'Copy', 'formipay' ); ?>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<form id="uploadForm" class="dropzone">
|
||||
<input type="hidden" name="action" value="formipay_bank_transfer_confirmation">
|
||||
<input type="hidden" name="order_id" value="<?php echo esc_attr($order_data['id']); ?>">
|
||||
<input type="hidden" name="form_id" value="<?php echo esc_attr($order_data['form_id']); ?>">
|
||||
<input type="hidden" name="total_amount" value="<?php echo esc_attr($order_data['total']); ?>">
|
||||
<input type="hidden" name="nonce" value="<?php echo esc_attr(wp_create_nonce('formipay_bank_transfer_confirmation')); ?>">
|
||||
|
||||
<div class="dropzone-area" id="dropzoneArea">
|
||||
<input type="file" name="file" id="fileInput" style="display: none;" accept="image/jpeg, image/png" />
|
||||
<div id="thumbnailPreview" style="display: none;">
|
||||
<?php // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage -- This image is a placeholder and will be managed by JS. ?>
|
||||
<img id="previewImage" src="" alt="Image Preview"/>
|
||||
</div>
|
||||
<i class="bi bi-image"></i>
|
||||
<div><?php echo esc_html($formipay_settings['bank_transfer_confirmation_dropzone_instruction']); ?></div>
|
||||
</div>
|
||||
|
||||
<button type="submit" id="upload-transfer-receipt" class="formipay-submit-button"><?php echo esc_html(formipay_get_post_meta($form_id, 'bank_transfer_confirmation_submit_button')); ?></button>
|
||||
</form>
|
||||
<?php
|
||||
$render = ob_get_clean();
|
||||
|
||||
}elseif($submit_action_type == 'whatsapp'){
|
||||
|
||||
$slug = 'thankyou';
|
||||
if(!empty($formipay_settings['thankyou_link'])){
|
||||
$slug = $formipay_settings['thankyou_link'];
|
||||
}
|
||||
|
||||
// translators: %d is the order ID.
|
||||
$message = sprintf( esc_html__( 'Order ID: *%d*', 'formipay'), $order_data['id'] );
|
||||
// translators: %s is the chosen bank name.
|
||||
$message .= "%0A".sprintf( esc_html__( 'Bank Name: *%s*', 'formipay'), $gateway_channel['bank_transfer_name'] );
|
||||
// translators: %s is the chosen bank account.
|
||||
$message .= "%0A".sprintf( esc_html__( 'Bank Account: *%s*', 'formipay'), $gateway_channel['bank_transfer_account'] );
|
||||
// translators: %s is the chosen bank account holder name.
|
||||
$message .= "%0A".sprintf( esc_html__( 'Bank Account Holder: *%s*', 'formipay'), $gateway_channel['bank_transfer_account_holder'] );
|
||||
// translators: %s is the total amount of order.
|
||||
$message .= "%0A".sprintf( esc_html__( 'Payment Total: *%s*', 'formipay'), formipay_price_format($order_data['total'], $form_id) );
|
||||
// translators: %s is the payment confirmation URL.
|
||||
$message .= "%0A%0A> ".sprintf( esc_html__( 'Upload your transfer receipt to confirm the payment to %s', 'formipay' ), site_url('/'.$slug.'/'.base64_encode($form_id.':::'.$order_data['id'].':::'.$order_data['meta_data']['session_id'])));
|
||||
|
||||
$render = $message;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $render;
|
||||
|
||||
}
|
||||
|
||||
public function handle_formipay_bank_transfer_confirmation() {
|
||||
|
||||
// Capability check
|
||||
// if (!current_user_can('edit_posts')) {
|
||||
// wp_send_json_error(['message' => __('Permission denied', 'formipay')]);
|
||||
// exit;
|
||||
// }
|
||||
|
||||
// Nonce verification
|
||||
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'formipay_bank_transfer_confirmation')) {
|
||||
wp_send_json_error(['message' => __('Nonce verification failed', 'formipay')]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Parameter sanitization
|
||||
$form_id = isset($_POST['form_id']) ? sanitize_text_field(wp_unslash($_POST['form_id'])) : '';
|
||||
$order_id = isset($_POST['order_id']) ? sanitize_text_field(wp_unslash($_POST['order_id'])) : '';
|
||||
|
||||
// File validation
|
||||
if (empty($_FILES['file']['tmp_name']) || !is_uploaded_file(sanitize_text_field($_FILES['file']['tmp_name']))) {
|
||||
wp_send_json_error(['message' => __('Invalid file upload', 'formipay')]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Check for errors
|
||||
if (isset($_FILES['file']['error']) && $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
|
||||
wp_send_json_error(array('message' => __('File upload error', 'formipay')));
|
||||
exit;
|
||||
}
|
||||
|
||||
// Require the WordPress file system
|
||||
require_once(ABSPATH . 'wp-admin/includes/file.php');
|
||||
// Use wp_handle_upload() to save the file
|
||||
$movefile = wp_handle_upload($_FILES['file'], array('test_form' => false));
|
||||
|
||||
if ($movefile && !isset($movefile['error'])) {
|
||||
// File is uploaded successfully; now save it to media library
|
||||
$file_path = $movefile['file'];
|
||||
$file_type = wp_check_filetype(basename($file_path), null);
|
||||
$attachment = array(
|
||||
'post_mime_type' => $file_type['type'],
|
||||
'post_title' => 'receipt-order-'.$order_id.'-'.sanitize_file_name(basename($file_path)),
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit'
|
||||
);
|
||||
|
||||
// Insert the attachment in the media library
|
||||
$attach_id = wp_insert_attachment($attachment, $file_path);
|
||||
require_once(ABSPATH . 'wp-admin/includes/image.php');
|
||||
$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
|
||||
wp_update_attachment_metadata($attach_id, $attach_data);
|
||||
|
||||
$update = formipay_update_order_status([
|
||||
'form_id' => $form_id,
|
||||
'order_id' => $order_id,
|
||||
'status' => 'payment-confirm',
|
||||
'payment_gateway' => 'bank_transfer'
|
||||
]);
|
||||
|
||||
|
||||
if($update['valid']){
|
||||
// Edit TRX DB
|
||||
$meta_data = [
|
||||
'transfer_receipt' => [
|
||||
'time' => time(),
|
||||
'attachment_id' => $attach_id,
|
||||
'attachment_url' => $movefile['url']
|
||||
]
|
||||
];
|
||||
|
||||
global $wpdb;
|
||||
$trx_db = $wpdb->prefix.'formipay_bank_transfer_trx';
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$wpdb->update( $trx_db, ['meta_data' => maybe_serialize($meta_data)], ['order_id' => $order_id] );
|
||||
}else{
|
||||
wp_delete_attachment( $attach_id, true );
|
||||
}
|
||||
|
||||
wp_send_json( [
|
||||
'success' => boolval($update['valid']),
|
||||
'data' => [
|
||||
'message' => $update['message'],
|
||||
'icon' => (boolval($update['valid']) == true) ? 'success' : 'info'
|
||||
]
|
||||
] );
|
||||
}
|
||||
|
||||
// Send an error response back
|
||||
wp_send_json_error([
|
||||
'message' => 'File could not be saved. Error: ' . $movefile['error']
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function render_email_action_button($button, $recipient, $order_data){
|
||||
|
||||
if(
|
||||
$order_data['payment_gateway'] == $this->gateway &&
|
||||
$order_data['status'] == 'on-hold' &&
|
||||
$recipient !== 'admin'
|
||||
){
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
$slug = 'thankyou';
|
||||
if(!empty($formipay_settings['thankyou_link'])){
|
||||
$slug = $formipay_settings['thankyou_link'];
|
||||
}
|
||||
|
||||
$button = [
|
||||
'url' => site_url('/'.$slug.'/'.base64_encode($order_data['form_id'].':::'.$order_data['id'])),
|
||||
'label' => formipay_get_post_meta($order_data['form_id'], 'bank_transfer_confirmation_submit_button')
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
return $button;
|
||||
|
||||
}
|
||||
|
||||
public function process_payment($order_data) {
|
||||
return $order_data;
|
||||
}
|
||||
|
||||
}
|
||||
581
includes/Payment/CashOnDelivery.php
Normal file
581
includes/Payment/CashOnDelivery.php
Normal file
@@ -0,0 +1,581 @@
|
||||
<?php
|
||||
namespace Formipay\Payment;
|
||||
use Formipay\Traits\SingletonTrait;
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
class CashOnDelivery extends Payment {
|
||||
|
||||
use SingletonTrait;
|
||||
|
||||
private $gateway = 'cod';
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
parent::__construct();
|
||||
|
||||
add_filter( 'formipay/global-settings/tab:payment', [$this, 'add_settings'] );
|
||||
add_filter( 'formipay/form-config/tab:payments', [$this, 'add_cod_setings'] );
|
||||
add_filter( 'formipay/form-config/tab:payments/gateways', [$this, 'add_gateway'] );
|
||||
add_filter( 'formipay/payment-list/cod/logo', [$this, 'set_frontend_logo'] );
|
||||
add_filter( 'formipay/order/order-details', [$this, 'add_fee'], 999, 3 );
|
||||
add_filter( 'formipay/order/payment-details/cod', [$this, 'render_payment_details'], 999, 4 );
|
||||
|
||||
add_action( 'template_redirect', [$this, 'check_parse_query'], 999 );
|
||||
|
||||
add_filter( 'formipay/thankyou/shortcodes', [$this, 'add_order_shortcodes'], 999, 4 );
|
||||
add_action('wp_ajax_formipay_cod_confirmation', [$this, 'handle_formipay_cod_confirmation'] );
|
||||
add_action('wp_ajax_nopriv_formipay_cod_confirmation', [$this, 'handle_formipay_cod_confirmation'] );
|
||||
|
||||
}
|
||||
|
||||
public function add_transaction($order_data){
|
||||
return;
|
||||
}
|
||||
|
||||
public function cancel_payment($order_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function add_settings($settings) {
|
||||
|
||||
$cod = array(
|
||||
$this->gateway.'_toggle' => array(
|
||||
'type' => 'checkbox',
|
||||
'label' => __('Activate Cash On Delivery', 'formipay'),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' )
|
||||
),
|
||||
$this->gateway.'_settings_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'General', 'formipay' ),
|
||||
'description' => __( 'Set the rule of Cash On Delivery', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_label' => array(
|
||||
'type' => 'text',
|
||||
'label' => __('Label', 'formipay'),
|
||||
'value' => __( 'COD', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_item_name' => array(
|
||||
'type' => 'text',
|
||||
'label' => __('Item Name', 'formipay'),
|
||||
'value' => __( 'COD Fee', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_icon' => array(
|
||||
'type' => 'image',
|
||||
'label' => __('Icon', 'formipay'),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_fee_amount' => array(
|
||||
'type' => 'number',
|
||||
'label' => __('Fee Amount', 'formipay'),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_fee_type' => array(
|
||||
'type' => 'select',
|
||||
'label' => __('Fee Type', 'formipay'),
|
||||
'options' => array(
|
||||
'fixed' => __( 'Fixed Amount', 'formipay' ),
|
||||
'percentage' => __( 'Percentage', 'formipay' )
|
||||
),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'group' => 'ended'
|
||||
),
|
||||
$this->gateway.'_details_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Instruction', 'formipay' ),
|
||||
'description' => __( 'Set instruction to buyer how to pay with COD method.', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_instruction_thankyou' => array(
|
||||
'type' => 'tinymce',
|
||||
'label' => __( 'Thankyou Screen', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'hints' => array(
|
||||
'product_name' => __( 'Product Name', 'formipay' ),
|
||||
'order_id' => __( 'Order ID', 'formipay' ),
|
||||
'grand_total' => __( 'Order Total', 'formipay' ),
|
||||
'confirmation_page_link' => __( 'Confirmation Page Link', 'formipay' )
|
||||
),
|
||||
'value' => '<p>Please prepare your cash of <span style="text-align: var(--bs-body-text-align);">{{grand_total}} </span><span style="text-align: var(--bs-body-text-align);">and pay it to the courier who delivers your </span>{{product_name}}.</p>',
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
),
|
||||
$this->gateway.'_instruction_whatsapp' => array(
|
||||
'type' => 'hint_textarea',
|
||||
'label' => __( 'WhatsApp Message', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'hints' => array(
|
||||
'product_name' => __( 'Product Name', 'formipay' ),
|
||||
'order_id' => __( 'Order ID', 'formipay' ),
|
||||
'grand_total' => __( 'Order Total', 'formipay' ),
|
||||
'confirmation_page_link' => __( 'Confirmation Page Link', 'formipay' )
|
||||
),
|
||||
'value' => 'Please prepare your cash of {{grand_total}} and pay it to the courier who delivers your {{product_name}}.',
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'group' => 'ended'
|
||||
),
|
||||
$this->gateway.'_confirmation_page_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Confirmation Page', 'formipay' ),
|
||||
'description' => sprintf(
|
||||
// translators: %s is the site URL.
|
||||
__(
|
||||
'<p>Set confirmation page for buyer to confirm that they have receive the package and pay the order to courier.</p>
|
||||
<p>Confirmation magic URL: <code>%s/payment-confirm/cod/{{autogenerated_token}}</code></p>'
|
||||
, 'formipay' ), site_url()),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_confirmation_page_content' => array(
|
||||
'type' => 'tinymce',
|
||||
'label' => __( 'Page Content', 'formipay' ),
|
||||
'hints' => array(
|
||||
'order_details' => __( 'Order Details', 'formipay' ),
|
||||
'confirmation_form' => __( 'Confirmation Form', 'formipay' ),
|
||||
),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_confirmation_page_width' => array(
|
||||
'type' => 'number',
|
||||
'label' => __( 'Page Width', 'formipay' ),
|
||||
'value' => '600',
|
||||
'description' => __( 'in pixel (px)', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_confirmation_table_alignment' => array(
|
||||
'type' => 'radio',
|
||||
'label' => __('Table Content Alignment', 'formipay'),
|
||||
'value' => 'center',
|
||||
'options' => array(
|
||||
'left' => '<i class="bi bi-text-left"></i> Left',
|
||||
'center' => '<i class="bi bi-text-center"></i> Center',
|
||||
'right' => '<i class="bi bi-text-right"></i> Right',
|
||||
),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_confirmation_form_agreement' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Agreement', 'formipay' ),
|
||||
'value' => __( 'I have receipt the package and pay as billed to the courier.', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
)
|
||||
),
|
||||
$this->gateway.'_confirmation_form_button' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Button Text', 'formipay' ),
|
||||
'value' => __( 'Confirm', 'formipay' ),
|
||||
'submenu' => __( 'Cash On Delivery', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => $this->gateway.'_toggle',
|
||||
'value' => 'not_empty'
|
||||
),
|
||||
'group' => 'ended',
|
||||
)
|
||||
);
|
||||
|
||||
foreach($cod as $key => $value){
|
||||
$settings[$key] = $value;
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
public function add_cod_setings($fields) {
|
||||
|
||||
$cod_settings = array(
|
||||
$this->gateway.'_confirmation_notice_message_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Confirmation Notice Messages', 'formipay' ),
|
||||
'description' => __( 'Set notice message on every condition about attempting to confirm the payment', 'formipay' ),
|
||||
'submenu' => __( 'COD', 'formipay' ),
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->gateway.'_confirmation_update_to_status' => array(
|
||||
'type' => 'select',
|
||||
'label' => __( 'Change status order to', 'formipay' ),
|
||||
'options' => formipay_order_status_list(),
|
||||
'value' => 'payment-confirm',
|
||||
'submenu' => __( 'COD', 'formipay' ),
|
||||
),
|
||||
$this->gateway.'_confirmation_message_success' => array(
|
||||
'type' => 'hint_textarea',
|
||||
'label' => __( 'Successfully Confirmed Message', 'formipay' ),
|
||||
'hints' => array(
|
||||
'order_id' => __('Order ID', 'formipay')
|
||||
),
|
||||
'value' => __('Successfully confirmed COD #{{order_id}}', 'formipay'),
|
||||
'description' => __('When successfully change the order status to the preferred one', 'formipay'),
|
||||
'submenu' => __( 'COD', 'formipay' ),
|
||||
),
|
||||
$this->gateway.'_confirmation_message_already_confirmed' => array(
|
||||
'type' => 'hint_textarea',
|
||||
'label' => __( 'Has Been Confirmed Message', 'formipay' ),
|
||||
'hints' => array(
|
||||
'order_id' => __('Order ID', 'formipay')
|
||||
),
|
||||
'value' => __('#{{order_id}} has been confirmed', 'formipay'),
|
||||
'description' => __('When the order status has been the preferred one', 'formipay'),
|
||||
'submenu' => __( 'COD', 'formipay' ),
|
||||
),
|
||||
$this->gateway.'_confirmation_message_error' => array(
|
||||
'type' => 'hint_textarea',
|
||||
'label' => __( 'Failed to Confirm Message', 'formipay' ),
|
||||
'hints' => array(
|
||||
'order_id' => __('Order ID', 'formipay'),
|
||||
'system_error_message' => __('System Error Message', 'formipay')
|
||||
),
|
||||
'value' => __('Failed to proceed. Error: {{system_error_message}}', 'formipay'),
|
||||
'description' => __('When error to change the order status to the preferred one', 'formipay'),
|
||||
'submenu' => __( 'COD', 'formipay' ),
|
||||
'group' => 'ended'
|
||||
)
|
||||
);
|
||||
|
||||
foreach($cod_settings as $key => $value){
|
||||
$fields[$key] = $value;
|
||||
}
|
||||
|
||||
return $fields;
|
||||
|
||||
}
|
||||
|
||||
public function add_gateway($gateways) {
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
|
||||
if(isset($formipay_settings['cod_toggle']) && false !== $formipay_settings['cod_toggle']){
|
||||
// $gateways['cod'] = __('Cash On Delivery', 'formipay');
|
||||
$gateways[] = [
|
||||
'id' => $this->gateway,
|
||||
'gateway' => __( 'COD', 'formipay' ),
|
||||
];
|
||||
}
|
||||
|
||||
return $gateways;
|
||||
|
||||
}
|
||||
|
||||
public function set_frontend_logo($logo){
|
||||
|
||||
if($logo == false){
|
||||
$logo = FORMIPAY_URL . 'public/assets/img/cod_default_icon.png';
|
||||
}
|
||||
|
||||
return $logo;
|
||||
|
||||
}
|
||||
|
||||
public function add_fee($details, $form_id, $order_data){
|
||||
|
||||
if($order_data['payment_gateway'] == 'cod'){
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
$fee_amount = $formipay_settings['cod_fee_amount'];
|
||||
$fee_type = $formipay_settings['cod_fee_type'];
|
||||
if($fee_type == 'percentage'){
|
||||
$grand_total = 0;
|
||||
if(!empty($details)){
|
||||
foreach($details as $detail){
|
||||
$grand_total += $detail['subtotal'];
|
||||
}
|
||||
}
|
||||
$fee_amount = $grand_total * floatval($fee_amount) / 100;
|
||||
}
|
||||
|
||||
$details[] = [
|
||||
'item' => $formipay_settings['cod_item_name'],
|
||||
'amount' => $fee_amount,
|
||||
'subtotal' => $fee_amount
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
return $details;
|
||||
}
|
||||
|
||||
public function render_payment_details($render, $form_id, $order_data, $submit_action_type) {
|
||||
|
||||
if($order_data['payment_gateway'] == 'cod'){
|
||||
|
||||
$render = '';
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
|
||||
$render .= ($submit_action_type == 'thankyou') ? '<div class="cod-instruction" style="text-align:'.formipay_get_post_meta($form_id, 'thankyou_screen_table_alignment').';">' : '';
|
||||
|
||||
$replacements = [
|
||||
'{{product_name}}' => get_the_title($form_id),
|
||||
'{{order_id}}' => $order_data['id'],
|
||||
'{{grand_total}}' => formipay_price_format($order_data['total'], $form_id),
|
||||
'{{confirmation_page_link}}' => site_url('/payment-confirm/'.$this->gateway.'/'.base64_encode($form_id.':::'.$order_data['id']))
|
||||
];
|
||||
|
||||
$render .= strtr( $formipay_settings['cod_instruction_'.$submit_action_type], $replacements );
|
||||
$render .= ($submit_action_type == 'thankyou') ? '</div>' : '';
|
||||
|
||||
}
|
||||
|
||||
return $render;
|
||||
|
||||
}
|
||||
|
||||
public function check_parse_query() {
|
||||
|
||||
global $wp_query;
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
|
||||
if(is_admin()) :
|
||||
return;
|
||||
endif;
|
||||
|
||||
if(
|
||||
!is_admin() &&
|
||||
is_array($wp_query->query) &&
|
||||
array_key_exists('formipay-payment-confirm', $wp_query->query) &&
|
||||
true === boolval($wp_query->query['formipay-payment-confirm']) &&
|
||||
array_key_exists('gateway', $wp_query->query) &&
|
||||
'cod' === $wp_query->query['gateway']
|
||||
) :
|
||||
|
||||
$token = explode(':::', base64_decode($wp_query->query['formipay-token']));
|
||||
$form_id = $token[0];
|
||||
$order_id = $token[1];
|
||||
|
||||
$thankyou_slug = 'thankyou';
|
||||
if(!empty($formipay_settings['thankyou_link'])){
|
||||
$thankyou_slug = $formipay_settings['thankyou_link'];
|
||||
}
|
||||
|
||||
$order = formipay_get_order($order_id);
|
||||
if($order['payment_gateway'] !== 'cod'){
|
||||
wp_safe_redirect( site_url('/'.$thankyou_slug.'/'.$wp_query->query['formipay-token']) );
|
||||
}
|
||||
|
||||
$button_background_color = json_decode(formipay_get_post_meta($form_id, 'button_bg_color'), true );
|
||||
$button_text_color = json_decode(formipay_get_post_meta($form_id, 'button_text_color'), true );
|
||||
$button_border_color = json_decode( formipay_get_post_meta($form_id, 'button_border_color'), true );
|
||||
|
||||
get_header();
|
||||
?>
|
||||
<style>
|
||||
body {
|
||||
--formipay-confirmation-page-width: <?php echo esc_html($formipay_settings[$this->gateway.'_confirmation_page_width']); ?>px;
|
||||
--formipay-confirmation-page-table-alignment: <?php echo esc_html($formipay_settings[$this->gateway.'_confirmation_table_alignment']) ?>;
|
||||
--formipay-button-submit-bg-color: <?php echo esc_html($button_background_color['regular']) ?? '#000000'; ?>;
|
||||
--formipay-button-submit-bg-color-hover: <?php echo esc_html($button_background_color['hover']) ?? '#ffffff'; ?>;
|
||||
--formipay-button-submit-bg-color-active: <?php echo esc_html($button_background_color['active']) ?? '#ffffff'; ?>;
|
||||
--formipay-button-submit-text-color: <?php echo esc_html($button_text_color['regular']) ?? '#ffffff'; ?>;
|
||||
--formipay-button-submit-text-color-hover: <?php echo esc_html($button_text_color['hover']) ?? '#000000'; ?>;
|
||||
--formipay-button-submit-text-color-active: <?php echo esc_html($button_text_color['active']) ?? '#000000'; ?>;
|
||||
--formipay-button-submit-border-color: <?php echo esc_html($button_border_color['regular']) ?? '#000000;' ?>;
|
||||
--formipay-button-submit-border-color-hover: <?php echo esc_html($button_border_color['hover']) ?? '#ffffff'; ?>;
|
||||
--formipay-button-submit-border-color-active: <?php echo esc_html($button_border_color['active']) ?? '#ffffff'; ?>;
|
||||
}
|
||||
</style>
|
||||
<div class="formipay-container">
|
||||
<div class="formipay-content-wrapper">
|
||||
<?php
|
||||
echo wp_kses_post(str_replace(
|
||||
[
|
||||
'{{order_details}}',
|
||||
'{{confirmation_form}}'
|
||||
],
|
||||
[
|
||||
$this->render_order_details_table($form_id, $order_id),
|
||||
$this->render_confirmation_form($form_id, $order_id)
|
||||
],
|
||||
$formipay_settings[$this->gateway.'_confirmation_page_content']
|
||||
))
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
get_footer();
|
||||
|
||||
exit;
|
||||
|
||||
endif;
|
||||
|
||||
}
|
||||
|
||||
public function render_confirmation_form($form_id, $order_id){
|
||||
|
||||
$formipay_settings = get_option( 'formipay_settings' );
|
||||
$order = formipay_get_order($order_id);
|
||||
|
||||
ob_start();
|
||||
|
||||
if($order['status'] == 'on-hold') {
|
||||
?>
|
||||
<form id="formipay-cod-confirmation">
|
||||
<input type="hidden" id="form_id" value="<?php echo intval($form_id); ?>">
|
||||
<input type="hidden" id="order_id" value="<?php echo intval($order_id); ?>">
|
||||
<?php wp_nonce_field('formipay_cod_confirmation', 'formipay_nonce'); ?>
|
||||
<div class="formipay-agreement">
|
||||
<input id="cod_agreement" type="checkbox" class="formipay-input">
|
||||
<label for="cod_agreement"><?php echo esc_html($formipay_settings['cod_confirmation_form_agreement']); ?></label>
|
||||
</div>
|
||||
<div class="formipay-button-wrapper">
|
||||
<button type="button" id="confirm-cod-order" class="formipay-submit-button" disabled><?php echo esc_html($formipay_settings['cod_confirmation_form_button']); ?></button>
|
||||
</div>
|
||||
</form>
|
||||
<?php
|
||||
}
|
||||
|
||||
$content = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
public function render_order_details_table($form_id, $order_id) {
|
||||
ob_start();
|
||||
?>
|
||||
<table id="order-details">
|
||||
<tbody>
|
||||
<?php
|
||||
$order = formipay_get_order($order_id);
|
||||
if(false !== $order){
|
||||
$items = maybe_unserialize($order['items']);
|
||||
foreach($items as $item){
|
||||
$label = ucwords(str_replace('_', ' ', $item['item']));
|
||||
if(!in_array($label, ['payment', 'payment_gateway'])){
|
||||
$qty = '';
|
||||
if(isset($item['qty']) && !empty($item['qty'])){
|
||||
$qty = ' × '.$item['qty'];
|
||||
}
|
||||
?>
|
||||
<tr>
|
||||
<th><?php echo esc_html($label) . esc_html($qty); ?></th>
|
||||
<td><?php echo esc_html(formipay_price_format($item['subtotal'], $form_id)); ?></td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<tr class="formipay-grand-total-row">
|
||||
<th><?php echo esc_html__( 'Total', 'formipay' ); ?></th>
|
||||
<td><?php echo esc_html(formipay_price_format($order['total'], $form_id)); ?></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
$content = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
public function handle_formipay_cod_confirmation() {
|
||||
|
||||
// Check nonce
|
||||
$nonce_valid = check_ajax_referer('formipay_cod_confirmation', 'formipay_nonce', false);
|
||||
|
||||
// Handle the valid nonce case
|
||||
if ($nonce_valid) {
|
||||
// Retrieve the data
|
||||
$form_id = isset($_POST['form_id']) ? sanitize_text_field( wp_unslash($_POST['form_id']) ) : '';
|
||||
$order_id = isset($_POST['order_id']) ? sanitize_text_field( wp_unslash($_POST['order_id']) ) : '';
|
||||
|
||||
$update = formipay_update_order_status([
|
||||
'form_id' => $form_id,
|
||||
'order_id' => $order_id,
|
||||
'status' => 'payment-confirm',
|
||||
'payment_gateway' => 'cod'
|
||||
]);
|
||||
|
||||
wp_send_json( [
|
||||
'success' => boolval($update['valid']),
|
||||
'data' => [
|
||||
'message' => $update['message'],
|
||||
'icon' => (boolval($update['valid']) == true) ? 'success' : 'info'
|
||||
]
|
||||
] );
|
||||
|
||||
}
|
||||
|
||||
// Nonce is invalid, handle the error
|
||||
wp_send_json_error([
|
||||
'message' => 'Security check failed. Please try again.',
|
||||
'icon' => 'error'
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function add_order_shortcodes($shortcodes, $form_id, $order_data, $submit_action_type) {
|
||||
|
||||
if($order_data['payment_gateway'] == $this->gateway){
|
||||
|
||||
$confirmation_page_link = site_url('/payment-confirm/'.$this->gateway.'/'.base64_encode($form_id.':::'.$order_data['id']));
|
||||
$shortcodes['payment_confirmation_form'] = '<a href="'.$confirmation_page_link.'" target="_blank">C.O.D Confirmation</a>';
|
||||
|
||||
}
|
||||
|
||||
return $shortcodes;
|
||||
|
||||
}
|
||||
|
||||
public function process_payment($order_data) {
|
||||
return $order_data;
|
||||
}
|
||||
|
||||
}
|
||||
289
includes/Payment/Payment.php
Normal file
289
includes/Payment/Payment.php
Normal file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
namespace Formipay\Payment;
|
||||
use Formipay\Traits\SingletonTrait;
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
abstract class Payment {
|
||||
|
||||
use SingletonTrait;
|
||||
|
||||
abstract public function add_gateway($gateways);
|
||||
abstract public function add_transaction($order_data);
|
||||
abstract public function process_payment($order_data);
|
||||
abstract public function cancel_payment($order_data);
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
add_filter( 'formipay/global-settings', [$this, 'add_setting_payment_menu'], 20 );
|
||||
add_filter( 'formipay/form-config', [$this, 'add_form_payment_menu'], 79 );
|
||||
|
||||
add_filter( 'formipay/form-config/tab:payments', [$this, 'add_responsive_grid'], 1012 );
|
||||
add_filter( 'formipay/frontend/payment-list', [$this, 'add_payment_list'], 999, 2 );
|
||||
|
||||
add_filter( 'formipay/order/shortcodes', [$this, 'add_order_shortcodes'], 999, 4 );
|
||||
add_filter( 'formipay/thankyou/shortcodes', [$this, 'add_order_shortcodes'], 999, 4 );
|
||||
add_filter( 'formipay/order/process-data', [$this, 'add_payment_in_process_data'], 999, 2 );
|
||||
add_filter( 'formipay/order/payment-details', [$this, 'render_payment_details'], 999, 4 );
|
||||
|
||||
add_action( 'init', [$this, 'set_endpoint'], 999);
|
||||
add_filter( 'query_vars', [$this, 'set_query_vars'], 899);
|
||||
add_action( 'wp_enqueue_scripts', [$this, 'frontend_enqueue'] );
|
||||
add_filter( 'pre_get_document_title', [$this, 'payment_confirm_page_title'], 1000 );
|
||||
|
||||
}
|
||||
|
||||
public function add_setting_payment_menu($fields){
|
||||
|
||||
$general_payment_settings = array();
|
||||
|
||||
$payment_settings = apply_filters( 'formipay/global-settings/tab:payment', $general_payment_settings );
|
||||
|
||||
if(!empty($payment_settings)){
|
||||
$fields['payment'] = array(
|
||||
'name' => __('Payment', 'formipay'),
|
||||
'fields' => $payment_settings
|
||||
);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
|
||||
}
|
||||
|
||||
public function add_form_payment_menu($fields) {
|
||||
|
||||
$gateways = apply_filters( 'formipay/form-config/tab:payments/gateways', [] );
|
||||
|
||||
$payment_options = [];
|
||||
if(!empty($gateways)){
|
||||
foreach($gateways as $gateway){
|
||||
$id = $gateway['id'];
|
||||
$label = $gateway['gateway'];
|
||||
if(isset($gateway['channel'])){
|
||||
$label .= ' - '.$gateway['channel'];
|
||||
}
|
||||
$payment_options[] = [
|
||||
'id' => $id,
|
||||
'label' => $label
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$payment_fields = [
|
||||
'payment_section_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Payment Section', 'formipay' ),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
'group' => 'started'
|
||||
),
|
||||
'payment_section_title' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Payment Section Title', 'formipay' ),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
'value' => 'Payment Methods',
|
||||
),
|
||||
'payment_gateways' => array(
|
||||
'type' => 'sorter',
|
||||
'label' => __( 'Choose one or any', 'formipay' ),
|
||||
'options' => array(
|
||||
array(
|
||||
'id' => 'inactive',
|
||||
'name' => __( 'Inactive', 'formipay' ),
|
||||
'options' => $payment_options,
|
||||
),
|
||||
array(
|
||||
'id' => 'active',
|
||||
'name' => __( 'Active', 'formipay' ),
|
||||
'options' => [],
|
||||
)
|
||||
),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
),
|
||||
'payment_label' => array(
|
||||
'type' => 'checkbox',
|
||||
'label' => __( 'Show Payment Label', 'formipay' ),
|
||||
'description' => __( 'Show payment label in the payment options on the frontend.', 'formipay' ),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
'group' => 'ended'
|
||||
)
|
||||
];
|
||||
|
||||
$payment_fields = apply_filters( 'formipay/form-config/tab:payments', $payment_fields );
|
||||
|
||||
$fields['formipay_form_settings']['payments'] = array(
|
||||
'name' => __( 'Payments', 'formipay' ),
|
||||
'fields' => $payment_fields
|
||||
);
|
||||
|
||||
return $fields;
|
||||
|
||||
}
|
||||
|
||||
public function add_responsive_grid($channels) {
|
||||
$channels['payment_responsive_box'] = array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Responsive', 'formipay' ),
|
||||
'description' => __( 'Set responsive grid payment card options' , 'formipay' ),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
'group' => 'started',
|
||||
);
|
||||
|
||||
$channels['payment_desktop_columns'] = array(
|
||||
'type' => 'number',
|
||||
'label' => __( 'Desktop View', 'formipay' ),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
'value' => 4
|
||||
);
|
||||
|
||||
$channels['payment_tablet_columns'] = array(
|
||||
'type' => 'number',
|
||||
'label' => __( 'Tablet View', 'formipay' ),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
'value' => 3
|
||||
);
|
||||
|
||||
$channels['payment_mobile_columns'] = array(
|
||||
'type' => 'number',
|
||||
'label' => __( 'Mobile View', 'formipay' ),
|
||||
'submenu' => __( 'General', 'formipay' ),
|
||||
'value' => 2,
|
||||
'group' => 'ended'
|
||||
);
|
||||
|
||||
return $channels;
|
||||
}
|
||||
|
||||
public function add_payment_list($payment_list, $form_id) {
|
||||
|
||||
$formipay_settings = get_option('formipay_settings');
|
||||
|
||||
$payment_gateways = json_decode(formipay_get_post_meta($form_id, 'payment_gateways'), true);
|
||||
foreach($payment_gateways as $payment){
|
||||
if($payment['id'] == 'active' && !empty($payment['options'])){
|
||||
foreach($payment['options'] as $option){
|
||||
$id = $option['id'];
|
||||
$_pay = explode(':::', $id);
|
||||
$gateway = $_pay[0];
|
||||
if(formipay_get_post_meta($form_id, 'product_type') == 'digital' && $gateway == 'cod'){
|
||||
continue;
|
||||
}
|
||||
if(isset($formipay_settings[$gateway.'_channels']) && isset($_pay[1])){
|
||||
$channel = $_pay[1];
|
||||
if(strpos($channel, '-')){
|
||||
$channel = explode('-', $channel);
|
||||
$channel_index = $channel[1];
|
||||
$payment_list[$id] = $formipay_settings[$gateway.'_channels'][$channel_index];
|
||||
}
|
||||
}else{
|
||||
$label = $icon = '';
|
||||
if(isset($formipay_settings[$gateway.'_label']) && !empty($formipay_settings[$gateway.'_label'])){
|
||||
$label = $formipay_settings[$gateway.'_label'];
|
||||
}
|
||||
if(isset($formipay_settings[$gateway.'_icon']) && !empty($formipay_settings[$gateway.'_icon'])){
|
||||
$icon = $formipay_settings[$gateway.'_icon'];
|
||||
}
|
||||
$payment_list[$id] = array(
|
||||
$gateway.'_name' => apply_filters( 'formipay/payment-list/'.$gateway.'/label', $label),
|
||||
$gateway.'_logo' => apply_filters( 'formipay/payment-list/'.$gateway.'/logo', $icon)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $payment_list;
|
||||
|
||||
}
|
||||
|
||||
public function add_order_shortcodes($shortcodes, $form_id, $order_data, $submit_action_type) {
|
||||
|
||||
$shortcodes['payment_details'] = apply_filters( 'formipay/order/payment-details', '', $form_id, $order_data, $submit_action_type );
|
||||
|
||||
return $shortcodes;
|
||||
|
||||
}
|
||||
|
||||
public function render_payment_details($render, $form_id, $order_data, $submit_action_type) {
|
||||
|
||||
$payment_gateway = $order_data['payment_gateway'];
|
||||
|
||||
$render = apply_filters( 'formipay/order/payment-details/'.$payment_gateway, $render, $form_id, $order_data, $submit_action_type );
|
||||
|
||||
ob_start();
|
||||
echo wp_kses($render, formipay_thankyoupage_allowed_html());
|
||||
$content = ob_get_contents();
|
||||
ob_get_clean();
|
||||
|
||||
return $content;
|
||||
|
||||
}
|
||||
|
||||
public function add_payment_in_process_data($order_data, $form_id) {
|
||||
|
||||
$payment = explode(':::', $order_data['payment']);
|
||||
$payment_gateway = $payment[0];
|
||||
$order_data['payment_gateway'] = $payment_gateway;
|
||||
|
||||
return $order_data;
|
||||
|
||||
}
|
||||
|
||||
public function set_endpoint() {
|
||||
|
||||
add_rewrite_rule(
|
||||
'^payment-confirm/([^/]*)/([^/]*)/?',
|
||||
'index.php?formipay-payment-confirm=1&gateway=$matches[1]&formipay-token=$matches[2]',
|
||||
'top'
|
||||
);
|
||||
|
||||
flush_rewrite_rules();
|
||||
|
||||
}
|
||||
|
||||
public function set_query_vars($vars){
|
||||
|
||||
$vars[] = 'formipay-payment-confirm';
|
||||
$vars[] = 'gateway';
|
||||
$vars[] = 'formipay-token';
|
||||
return $vars;
|
||||
|
||||
}
|
||||
|
||||
public function frontend_enqueue() {
|
||||
|
||||
global $wp_query;
|
||||
|
||||
if(
|
||||
is_array($wp_query->query) &&
|
||||
array_key_exists('formipay-payment-confirm', $wp_query->query) &&
|
||||
true === boolval($wp_query->query['formipay-payment-confirm'])
|
||||
) :
|
||||
|
||||
wp_enqueue_style( 'formipay-payment-confirm', FORMIPAY_URL . 'public/assets/css/payment-confirm.css', [], FORMIPAY_VERSION, 'all' );
|
||||
wp_enqueue_script( 'formipay-payment-confirm', FORMIPAY_URL . 'public/assets/js/payment-confirm.js', ['jquery'], FORMIPAY_VERSION, true );
|
||||
|
||||
endif;
|
||||
|
||||
}
|
||||
|
||||
public function payment_confirm_page_title($title) {
|
||||
global $wp_query;
|
||||
|
||||
if(
|
||||
is_array($wp_query->query) &&
|
||||
array_key_exists('formipay-payment-confirm', $wp_query->query) &&
|
||||
true === boolval($wp_query->query['formipay-payment-confirm'])
|
||||
) :
|
||||
|
||||
$gateway = $wp_query->query['gateway'];
|
||||
$gateway = strlen($gateway) > 3 ? ucwords( str_replace('_', ' ', $gateway) ) : strtoupper($gateway);
|
||||
|
||||
$title = $gateway . ' Confirmation' . ' - ' . get_bloginfo('name');
|
||||
|
||||
endif;
|
||||
|
||||
return $title;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user