New payment method field for withdrawals

New payment method field for withdrawals

A payment gateway is a crucial part of a marketplace. It allows customers to purchase products and allows the admin to distribute the vendor earnings. In WordPress, Dokan is one of the popular multivendor solutions that provide two types of payment gateways supports, which are adaptive and non-adaptive.

Those payment gateways that support the automatic distribution of the vendor earnings and admin commission are called adaptive payment methods. On the other hand, those payment gateways do not support automatic distribution; instead, they require to withdraw vendor earnings by manual withdrawal requests are called non-adaptive payment methods.

By default, Dokan provides three payment methods to vendors for withdrawal. Those are the PayPal, Bank Transfer, and Skrill. However, many marketplace owners use different payment gateways so that customers can purchase products smoothly. At this stage, many Dokan users might fall into a dilemma on how to distribute vendor earnings if they use any other payment gateways on their marketplace.

As a result, I come up with a solution, and in this article, I will demonstrate how to add a new payment gateway field to the vendor dashboard so that marketplace owners can add a new convenient payment method for vendors to facilitate their withdrawal process.

So, without further due, let’s jump into the code…

At first, add the payment gateway option in the Dokan > Settings > Withdraw Options screen and a new field on the Vendor Dashboard > Settings > Payment page.

#-- Custom Payment Method --#
function new_withdraw_methods($methods){
  $methods['custom'] = [
    'title'     => __( 'Custom Payment', 'dokan-lite' ),
    'callback'  =>  'dokan_withdraw_method_custom'
  ];
  return $methods;
}
add_filter('dokan_withdraw_methods', 'new_withdraw_methods');

#-- Custom Withdraw Method Callback function --#
function dokan_withdraw_method_custom( $store_settings ) {
    global $current_user;

    $email = isset( $store_settings['payment']['custom']['email'] ) ? esc_attr( $store_settings['payment']['custom']['email'] ) : $current_user->user_email; ?>
    <div class="dokan-form-group">
        <div class="dokan-w8">
            <div class="dokan-input-group">
                <span class="dokan-input-group-addon"><?php esc_html_e( 'E-mail', 'dokan-lite' ); ?></span>
                <input value="<?php echo esc_attr( $email ); ?>" name="settings[custom][email]" class="dokan-form-control email" placeholder="you@domain.com" type="text">
            </div>
        </div>
    </div>
    <?php
}

After that, the payment method field value needs to save in the database. So, the below code needs to add after the above part.

#-- Save --#
function save_custom_payment_method( $store_id, $dokan_settings ) {
  $post_data = wp_unslash( $_POST );

  if ( isset( $post_data['settings']['custom'] ) ) {
      $dokan_settings['payment']['custom'] = array(
          'email' => sanitize_email( $post_data['settings']['custom']['email'] ),
      );
  }
  
  update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}
add_action( 'dokan_store_profile_saved', 'save_custom_payment_method', 10, 2 );

Next, the payment method needs to add to the Vendor Dashboard > Withdraw page’s Dropdown list so that vendors can send withdrawal requests using that method. So, adding the below code will resolve it.

#-- Dropdown--#
function seller_active_withdraw_methods($active_payment_methods, $vendor_id){
  $store_info = dokan_get_store_info($vendor_id);
  $custom = isset( $store_info['payment']['custom']['email'] ) && $store_info['payment']['custom']['email'] !== false ? 'custom' : '';
  $active_payment_methods[] = $custom;
  return $active_payment_methods;
}
add_filter('dokan_get_seller_active_withdraw_methods', 'seller_active_withdraw_methods', 10, 2);

Now, it comes to show the withdrawal request information to the admin screen to transfer the requested amount to the respective payment method account correctly. Adding the following code helps to appear on the Dokan > Withdraws screen.

#-- Details --#
function vue_admin_withdraw(){
  ?>
  <script>
    var hooks;
    function getCustomPaymentDetails(details, method, data){
      if (data[method] !== undefined) {
        if ('custom' === method) {
          details = data[method].email || '';
        }
      }

      return details;
    }
    dokan.hooks.addFilter('dokan_get_payment_details', 'getCustomPaymentDetails', getCustomPaymentDetails, 33, 3);
  </script>
  <?php
}
add_action('admin_print_footer_scripts', 'vue_admin_withdraw', 99);

P.S. – The code in the child theme’s functions.php file. If you do not have a child theme, you can add them using the Code Snippets plugin.

Screenshots –


4.3 4 votes
Article Rating
guest

8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
David cote
David cote
1 year ago

Hi, thanks for your good explanation. Hence, I want to add a phone number as custom payment method. I am not really an expert in programming. I tried to change “dokan-form-control email” I write email it save and works fine but if I enter number it does not work anymore. So please can you give a code that will work or tell me where to change to met this goal.

Thank you.

Alfred Wamugi
Reply to  Tanjir Al Mamun
1 year ago

Hello, where do i add the code?

Cat
Cat
5 months ago

The documentation is not clear on which files to update. You reference menu’s when advising what code needs to be modified. Can you please revise and share a specific folder & filename for each code change?

kay
kay
5 months ago

Can you do a phone number text field version of this tutorial. Some of us are not too techy. Good job helping out newbies like us.

8
0
Would love your thoughts, please comment.x
()
x