Jump to content
Code-Storm

Pre Select Payment Gateway For Each Currency

Recommended Posts

Hi There

Here is what i need:

The user could select the currency on the shopping cart while they ordering something from cart page. After click on the checkout button, only the payment gateways set to that currency should be available.

Example, if an user select currency USD it will show / Marked Paypal Gateway, other Payment Gateway goes hidden , if user select GBP,CAD  it will show Stripe and Paypal goes hidden

same things for the invoices. and user can only see the selected payment gateway on their invoice for duepayment.

 

Does anyone know, how can i accomplish this. i am using WHMCS 7.5.2

 

Thanks in Advance!!

 

 

Share this post


Link to post
Share on other sites
On 28/11/2018 at 07:46, Code-Storm said:

Does anyone know, how can i accomplish this. i am using WHMCS 7.5.2

possibly a question better suited to posting in Service Offers & Requests, but basically it's going to be two hooks... can't think of an existing addon that does this, and a hook solution would work in v7.5.2 and later because there have not been any significant changes to that page since that release.

On 28/11/2018 at 07:46, Code-Storm said:

The user could select the currency on the shopping cart while they ordering something from cart page. After click on the checkout button, only the payment gateways set to that currency should be available.

that would require a clientareapagecart hook - it wouldn't be a million miles away from the one I posted below to change the payment options when the cart balance is zero...

On 28/11/2018 at 07:46, Code-Storm said:

same things for the invoices. and user can only see the selected payment gateway on their invoice for duepayment

again, that's going to be another hook (clientareapageviewinvoice this time) - you know the client is going to be logged in, so it should just be a case of getting their currency from the template and modifying the dropdown array accordingly.. i've posted CAPVI hooks before, but the one you need wouldn't be too far from the above hook method wise, e.g the hook will determine the valid gateway(s) and remove the others from the dropdown.

Share this post


Link to post
Share on other sites
1 hour ago, brian! said:

possibly a question better suited to posting in Service Offers & Requests, but basically it's going to be two hooks... can't think of an existing addon that does this, and a hook solution would work in v7.5.2 and later because there have not been any significant changes to that page since that release.

that would require a clientareapagecart hook - it wouldn't be a million miles away from the one I posted below to change the payment options when the cart balance is zero...

so you'd have to modify it to make two checks - one to get the current currency if the user isn't logged in (e.g they've chosen it from a dropdown etc) and one if they are logged in (where they will be locked into using the same currency forever).

again, that's going to be another hook (clientareapageviewinvoice this time) - you know the client is going to be logged in, so it should just be a case of getting their currency from the template and modifying the dropdown array accordingly.. i've posted CAPVI hooks before, but the one you need wouldn't be too far from the above hook method wise, e.g the hook will determine the valid gateway(s) and remove the others from the dropdown.

Hello Brian,

Thanks for your suggestion, But i saw some previous thread about same option: here one  Payment Gateway selection by Currency

i want the same work ! like if my client choose USD/CAD/AUD/GBP it will pre select "Paypal, if he/she choose BDT it will mark another. i have done some work but nothing comes up!

Will you please help me with those code & in viewcart.tpl & checkout.tpl where i exactly put those codes!

 

 

 

 

Share this post


Link to post
Share on other sites
19 hours ago, Code-Storm said:

Thanks for your suggestion, But i saw some previous thread about same option: here one  Payment Gateway selection by Currency

but that thread is two and a half years old, written for the "Modern" template and edits a template - as we're virtually in 2019, you shouldn't be editing templates if/when there is a better solution (e.g an action hook) - not least because a) it removes the need to edit the template after every WHMCS release, and b) in this instance, the hook should work on both cart templates options (e.g Modern and Standard_cart). thanks.png

19 hours ago, Code-Storm said:

i want the same work ! like if my client choose USD/CAD/AUD/GBP it will pre select "Paypal, if he/she choose BDT it will mark another. i have done some work but nothing comes up!

then do it as a hook and get it to remove the gateways that you don't want by modifying the previous linked hook - although what it won't do (neither would a template edit) is pre-select PayPal or Stripe (as written each would be the only option available) - with PayPal, I think you could pre-select it in the hook without issue; but for Stripe, it's the pressing of the Stripe radio option that triggers the slide event to allow for card entry submission - which if already pre-selected, you can't do... you'd then have to use JS/Jquery in the hook to trigger that slide to occur automatically.

19 hours ago, Code-Storm said:

Will you please help me with those code & in viewcart.tpl & checkout.tpl where i exactly put those codes!

it would take me longer to write/test this in the template than it would to write a hook - especially as you haven't said which orderform template you're using.... and unless there's something glaringly obvious that i'm missing, I don't really see any advantage to editing the template for this issue.

besides, after i've done that, you'll then want the same on the invoices page! 🙄

for trivial navbar/sidebar hooks, I don't mind posting code... but when you're into the realms of customisation that likely needs testing time, then you should be thinking about paying a developer... it's not good for the WHMCS developer community, if i'm sat here posting solutions to issues that developers should be paid to solve... i've already told you how to accomplish this (as per your original question) - short of writing the hooks for you, there's not much more I can do.

Share this post


Link to post
Share on other sites
29 minutes ago, brian! said:

you shouldn't be editing templates if/when there is a better solution (e.g an action hook) - not least because a) it removes the need to edit the template after every WHMCS release

Well, until WHMCS changes how (and if) they work, that is. 😉

Share this post


Link to post
Share on other sites
29 minutes ago, bear said:

Well, until WHMCS changes how (and if) they work, that is. 😉

true, but if you try to use two hooks with the same name, you're asking for trouble! 🙂

Share this post


Link to post
Share on other sites
1 hour ago, brian! said:

but that thread is two and a half years old, written for the "Modern" template and edits a template - as we're virtually in 2019, you shouldn't be editing templates if/when there is a better solution (e.g an action hook) - not least because a) it removes the need to edit the template after every WHMCS release, and b) in this instance, the hook should work on both cart templates options (e.g Modern and Standard_cart). thanks.png

then do it as a hook and get it to remove the gateways that you don't want by modifying the previous linked hook - although what it won't do (neither would a template edit) is pre-select PayPal or Stripe (as written each would be the only option available) - with PayPal, I think you could pre-select it in the hook without issue; but for Stripe, it's the pressing of the Stripe radio option that triggers the slide event to allow for card entry submission - which if already pre-selected, you can't do... you'd then have to use JS/Jquery in the hook to trigger that slide to occur automatically.

it would take me longer to write/test this in the template than it would to write a hook - especially as you haven't said which orderform template you're using.... and unless there's something glaringly obvious that i'm missing, I don't really see any advantage to editing the template for this issue.

besides, after i've done that, you'll then want the same on the invoices page! 🙄

for trivial navbar/sidebar hooks, I don't mind posting code... but when you're into the realms of customisation that likely needs testing time, then you should be thinking about paying a developer... it's not good for the WHMCS developer community, if i'm sat here posting solutions to issues that developers should be paid to solve... i've already told you how to accomplish this (as per your original question) - short of writing the hooks for you, there's not much more I can do.

Hello Brian,

Thanks for the hook!

to be honest, I don't need the same things for Invoice too!.  it's pretty much enough for me, if i get this only for cart pages!. currently i am using 4 templates for 4 different product category.

Standard, modern, pure comparison, universal slider 

Share this post


Link to post
Share on other sites
3 minutes ago, Code-Storm said:

Thanks for the hook!

i'm glad you liked it - don't think i'll be making a habit of it though! headshake.gif

4 minutes ago, Code-Storm said:

to be honest, I don't need the same things for Invoice too!.  it's pretty much enough for me, if i get this only for cart pages!. currently i am using 4 templates for 4 different product category.

Standard, modern, pure comparison, universal slider  

forget about slider and comparison, by the time you get to checkout, you'll be using modern or sc.

Share this post


Link to post
Share on other sites
8 minutes ago, brian! said:

i'm glad you liked it - don't think i'll be making a habit of it though! headshake.gif

forget about slider and comparison, by the time you get to checkout, you'll be using modern or sc.

Well then it's only possible for specific templates! so is it possible to get those codes Brian as an example?? Thanks is Advance

Share this post


Link to post
Share on other sites
4 minutes ago, Code-Storm said:

Well then it's only possible for specific templates! so is it possible to get those codes Brian as an example??

that hook works in both Modern and Standard_cart... if you're using Pure Comparison or Universal Slider, after the opening products page, you are using Standard_cart anyway, so the hook will work with them too.

Share this post


Link to post
Share on other sites
2 minutes ago, brian! said:

that hook works in both Modern and Standard_cart... if you're using Pure Comparison or Universal Slider, after the opening products page, you are using Standard_cart anyway, so the hook will work with them too.

where i need to put the hooks then???

Share this post


Link to post
Share on other sites
Just now, Code-Storm said:

where i need to put the hooks then???

/includes/hooks

Share this post


Link to post
Share on other sites
18 hours ago, Code-Storm said:

then

Hello Brian,

For USD,AUD,CAD,GBP paypal pre select perfectly but for local currency which is BDT it's not showing the Local Payment Method which is Cash Payment and on checkoutpage http://prntscr.com/lovr4z

Edited by Code-Storm
Wrong info

Share this post


Link to post
Share on other sites
2 hours ago, Code-Storm said:

For USD,AUD,CAD,GBP paypal pre select perfectly but for local currency which is BDT it's not showing the Local Payment Method which is Cash Payment and on checkoutpage

have you updated the hook to account for this change? I coded it to use Stripe for BDT as per your question...

		elseif (in_array($currency['code'],array('BDT'))) {
			$allowed = ['stripe'];
		}

you'd need to change that to the internal name used for your Cash Payment gateway - if it's "Mail In Payment", it would be 'mailin'... if it's something else, and you don't know what it is, you'll need to disable the hook (e.g remove the above lines) and view the source of the checkout page in the browser and that will tell you the name...

PPgkWaE.png

then put those lines back in, change stripe to whatever this gateway name is and then save the hook.

Share this post


Link to post
Share on other sites
1 hour ago, brian! said:

have you updated the hook to account for this change? I coded it to use Stripe for BDT as per your question...


		elseif (in_array($currency['code'],array('BDT'))) {
			$allowed = ['stripe'];
		}

you'd need to change that to the internal name used for your Cash Payment gateway - if it's "Mail In Payment", it would be 'mailin'... if it's something else, and you don't know what it is, you'll need to disable the hook (e.g remove the above lines) and view the source of the checkout page in the browser and that will tell you the name...

PPgkWaE.png

then put those lines back in, change stripe to whatever this gateway name is and then save the hook.

Hello Brian,

I apologize for the mistakes! i just wrote "Stripe" as an example for BDT payment. i am really sorry

I understand! i just need to replace my getaway  name instead Stripe!

what about if i want to keep an option like. For USD/AUD/CAD/GBP/ paypal is pre-selected besides there will be an option like Bank Transfer or Payza

Share this post


Link to post
Share on other sites
17 hours ago, Code-Storm said:

what about if i want to keep an option like. For USD/AUD/CAD/GBP/ paypal is pre-selected besides there will be an option like Bank Transfer or Payza

then you add the gateways that you want to show for a currency as a list in the hook...

		if (in_array($currency['code'],array('AUD','CAD','GBP','USD'))) {
			$allowed = ['paypal','banktransfer','payza'];
		}

... so for AUD/CAD/GBP/USD, the only gateway options shown will be PayPal, Bank Transfer and Payza... all other gateways will be removed.

Share this post


Link to post
Share on other sites
4 hours ago, brian! said:

then you add the gateways that you want to show for a currency as a list in the hook...


		if (in_array($currency['code'],array('AUD','CAD','GBP','USD'))) {
			$allowed = ['paypal','banktransfer','payza'];
		}

... so for AUD/CAD/GBP/USD, the only gateway options shown will be PayPal, Bank Transfer and Payza... all other gateways will be removed.

Hello Brian,

Hat's off to you! and also grateful to you!

Share this post


Link to post
Share on other sites
On 12/1/2018 at 6:35 PM, brian! said:

then you add the gateways that you want to show for a currency as a list in the hook...


		if (in_array($currency['code'],array('AUD','CAD','GBP','USD'))) {
			$allowed = ['paypal','banktransfer','payza'];
		}

... so for AUD/CAD/GBP/USD, the only gateway options shown will be PayPal, Bank Transfer and Payza... all other gateways will be removed.

Hello Brian,

everythings looks good. but need a little help, according to the code client can pay their bill by using paypal but we want they can pay bill via their credit card without login to paypal. for this paypal express checkout is good option but paypal express checkout option not working when they select their currency USD/AUD/CAD

here is the code

<?php

/**
* Modify Payment Gateway Options Based On Currency
* @author Brian!
*/
 
function cart_gateway_removal_based_on_currency_hook($vars)
{
    if ($vars['templatefile']=='viewcart'){
        $gateways = $vars['gateways'];
        $currency = $vars['currency'];
        if (in_array($currency['code'],array('AUD','CAD','GBP','USD'))) {
            $allowed = ['paypalexpress','paypal','directdebit'];
        }
        elseif (in_array($currency['code'],array('BDT'))) {
            $allowed = ['walletmixgateway','directdebit'];
        }
        if (!empty($allowed)) {
            foreach ($gateways as $k => $item) {
                if (!in_array($item['sysname'],$allowed)) {
                    unset($gateways[$k]);
                }
            }
            return array("gateways" => $gateways);
        }
    }
}
add_hook("ClientAreaPageCart", 1, "cart_gateway_removal_based_on_currency_hook");

 

Share this post


Link to post
Share on other sites
10 hours ago, Code-Storm said:

everythings looks good. but need a little help, according to the code client can pay their bill by using paypal but we want they can pay bill via their credit card without login to paypal. for this paypal express checkout is good option but paypal express checkout option not working when they select their currency USD/AUD/CAD

the problem is that PayPal Express Checkout isn't a payment gateway option shown at checkout, it's shown at the View Cart stage..

https://docs.whmcs.com/PayPal_Express_Checkout

Quote

The PayPal Express checkout module allows your clients to check out in a single click without the need to enter their details into the WHMCS order form. The client's name, address, email and phone number will be taken from those stored in their PayPal account and a one-time payment sent when the Express Checkout button is clicked on the order form.

9PQ2rOV.png

so the hook is working fine - it's just that you're asking it to do something that it wasn't designed to do. 🙂

you could try another hook to cover PayPal Express Checkout and when it is or isn't shown..

<?php

/**
* PayPal Express Checkout Options Based On Currency
* @author Brian!
*/
 
function cart_paypal_express_checkout_based_on_currency_hook($vars)
{
    if ($vars['templatefile']=='viewcart'){
        $currency = $vars['currency'];
        if (!in_array($currency['code'],array('AUD','CAD','GBP','USD'))) {
            return array("gatewaysoutput" => null);
        }
    }
}
add_hook("ClientAreaPageCart", 1, "cart_paypal_express_checkout_based_on_currency_hook"); 

which basically says that if the current currency is AUD, CAD, GBP or USD, then do nothing (as you want PEC as an option for those currencies) - for any other currency, the PayPal Express option will be removed from View Cart.

you might run into issues if multiple gateways were used at view cart like this, and you wanted to discriminate between them for certain currencies, but I think PEC is the only one so far that does it...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated