All the code found in this article is considered a customisation. As such, it's outside the scope of our support service. Should you need assistance implementing the code, please feel free to contact us to request a consultation.


Overview

PeachPay uses its own, custom checkout process, which takes the order data, such as totals and their currency, from the WooCommerce environment. The values, as well as the currency code, depend on the active currency, which is set by the Aelia Currency Switcher. Changing the active currency results in WooCommerce returning prices in the new currency.


We ran some tests to see how PeachPay could leverage this flexibility, and the results indicate that it should be possible to use some filters to alter the active currency and, therefore, the prices, just before the PeachPay initialisation. Although PeachPay doesn't offer dedicated filters for that purpose, we were able to reverse-engineer some of its code, to find some suitable entry points.


Example 1 - Initialise PeachPay to process payments in a specific currency

The filters you will find below show how to make PeachPay load data in a specific currency.


Important: in addition to these filters, it's important that PeachPay is enabled for all the currencies configured in the Aelia Currency Switcher. This is crucial to allow WooCommerce to see the gateway as available during the payment process. You can enable PeachPay for all the currencies by going to WooCommerce > Currency Switcher > Payment Gateways.

/**
 * Returns the currency code that should be used by PeachPay.
 * 
 * @return string
 */
function get_peachpay_currency(): string {
    return 'USD';
}

/**
 * Set the active currency just before PeachPay is initialised. This will
 * cause product prices to be returned in that currency, and PeachPay will
 * collect them automatically, together with the currency code.
 * 
 * @return string
 */
add_filter('peachpay_script_data', function($script_data) {
    add_filter('wc_aelia_cs_selected_currency', 'get_peachpay_currency', 99);
}, 1);

/**
 * Removes the filter that set the active currency for PeachPay, after the
 * initialisation. This is to prevent interfering with the rest of the site.
 * 
 * @return string
 */
add_filter('peachpay_script_data', function($script_data) {
    remove_filter('wc_aelia_cs_selected_currency', 'get_peachpay_currency', 99);
}, 9999);

/**
 * Sets the active currency that will be used by PeachPay to create an order.
 * This is to ensure that the order is in the same currency used to process the
 * payment.
 * 
 * @return string
 */
add_action('wc_ajax_wc_peachpay_create_order', function() {
    add_filter('wc_aelia_cs_selected_currency', 'get_peachpay_currency', 99);
}, 1);

These filters intercept the initialisation step of PeachPay, as well as its checkout process, setting the active currency to the one specified by function get_peachpay_currency(). This causes PeachPay to fetch amounts, such product prices, in that currency, and use them to create the WooCommerce order and to process the transaction. The rest of the site remains unaffected, and keeps showing prices in the currency that was last selected by the customer.


Important - Read before using the code

This code is an example of how to customise PeachPay. That plugin is not developed or supported by us, and we couldn't run extensive tests to verify that the filters cover all cases. As indicated at the top of this article, the code is provided as is, without guarantees, and it's not covered by our support service. We strongly recommend to test the code on a staging copy of your site, to ensure that it works as you would expect, before using it on a live site.


You can purchase the Currency Switcher from our online shop.