Paypal currency convert upon checking out - php

I was trying setup a paypal gateway but I was getting an error:
Gateway Disabled: PayPal does not support your store currency
By default, I have an AED Currency so I'm trying to convert it to USD when checking out to paypal but right now it is not working.
I have this in my theme's functons.php file:
add_filter( 'woocommerce_currencies', 'add_my_currency' );
function add_my_currency( $currencies ) {
$currencies['AED'] = __( 'Emirati Dirham', 'woocommerce' );
return $currencies;
}
add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);
function add_my_currency_symbol( $currency_symbol, $currency ) {
switch( $currency ) {
case 'AED': $currency_symbol = 'AED'; break;
}
return $currency_symbol;
}
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ), 20 );
add_filter( 'woocommerce_billing_fields', 'wc_billing_fields_state_filter', 10, 1 );
function wc_billing_fields_state_filter( $address_fields ) {
$address_fields['billing_state']['label'] = 'Emirate';
$address_fields['billing_state']['placeholder'] = 'Emirate';
return $address_fields;
}
EDIT: I've deactivated the plugin "All Currencies for WooCommerce" and the settings menu is now showing. But PayPal Gateway is still not allowed since AED is not a supported currency. How do I convert it to USD upon paying with PayPal?
The code above doesn't seem to work

The code you have adds AED to the list of the currencies "supported" by the PayPal gateway, but it doesn't perform any conversion to USD. To perform a conversion, you will need a couple of things:
A currency conversion function. WooCommerce, by itself, is "single currency" and it doesn't perform conversions, nor it provides or handles exchange rates. For that, you will need a multi-currency solution.
Once you have a multi-currency solution in place, you will have to add the code to invoke the currency conversion to transform AED to USD before the PayPal gateway sends the data to the PayPal servers.
Last, but not least, you will have to intercept the payment notification sent by PayPal, so that the cross checks are successful. That is, you will have an order for an amount in AED in your database, and the payment confirmation will send you an amount in USD. You will need a way to prevent such discrepancy from causing the payment validation to fail. Without this step, the orders will be left pending, or on hold, and you will have to "unblock" them manually.
Depending on the multi-currency solution in use, the exact approach could be slightly different. As the developer of the Currency Switcher for WooCommerce, I solved all of the above for my clients, using the plugins I developed and some custom code.
You can find the custom code that applies to our plugins in our knowledge base: Is it possible to use the Currency Switcher to allow Users to select a currency, but force payment in base currency, or an arbitrary currency?.
Whether you decide to use our plugins or not, the code should be a good starting point to implement a solution.

Related

Add custom fields to Stripe checkout information and send via webhook

I am using wordpress to build a website that uses Stripe as its payment gateway.
Through the Paid Membership Pro plugin I have configured my Stripe connection.
The issue I am faced with is that I need to append a custom field, in this case a unique identifier for the customer, to the payment information for it to be available to send via my webhook to my server for processing.
I have come across a load of answers but none seem to be doing exactly what I am looking for.
I noticed in the PMPro plugin it allows you to add User Fields which I presumed would be a way to add custom data to the information but after checking the JSON payloads in Stripe none of the user field information is available.
I then tried adding the code from this answer to my functions.php file in word press just using a test meta key but again this information was not available in the payloads.
I am not using woocommerce.
How can I achieve this?
So I was able to crack this using a php script that intercepts the Stripe checkout event.
I added this as a snippet to my Wordpress site and to read in a custom field from the post submit.
Stripe allows you to add custom filed data to the metadata block and once populated this was successfully sent to my webhook.
function my_pmpro_stripe_params_send_user_id( $params, $order = null ) {
global $my_pmpro_checkout_order;
if ( empty( $params['metadata'] ) ) {
$params['metadata'] = array();
}
if ( empty ( $order ) ) {
// Save order while creating payment intent.
$order = $my_pmpro_checkout_order;
} else {
// Use saved order while creating subscription.
$my_pmpro_checkout_order = $order;
}
$params['metadata']['my_custom_field'] = $_POST['my_custom_value'];
return $params;
}
add_filter( 'pmpro_stripe_payment_intent_params', 'my_pmpro_stripe_params_send_user_id', 10, 2 );
add_filter( 'pmpro_stripe_create_subscription_array', 'my_pmpro_stripe_params_send_user_id', 10, 1 );

WooCommerce setting COD orders to On-Hold [duplicate]

I need help with a problem-related to plugin "WooCommerce Pay for Payment" which counting some extra fee in shipping. Problem is, that this plugin sets automatically "processing" status in order which causes thanking email for payment (in case of local payment) and don't send email notification about a new order, so customer is confused (I didn't send any money and I received email "thanks for your payment").
I tried this solution: Set WooCommerce order status when order is created from processing to pending
But it only changes order status back to "on-hold" but sends email thank for payment anyway.
Only one thing that I need is to send to the customer in every new order email about a new order, nothing more (I would like to change status to "processing" manually).
Thank you for help, I have no idea how to resolve because I couldn't find PHP file causing a change of status in the plugin.
EDIT: Sorry to all. This was problem of COD in woocommerce plugin. Not Pay for payment as I mentioned. Woocommerce COD automatically set "processing" status.
I found solution for this on github:here
Its the first code.
Based on the answer to this question, this code worked fine for me:
function sv_wc_cod_order_status( $status ) {
return 'on-hold';
}
add_filter( 'woocommerce_cod_process_payment_order_status', 'sv_wc_cod_order_status', 15 );
Updated: The code that you found in Github is outdated, clumsy and complicated, since there is a dedicated filter hook now. You should better try this lightweight and effective code, that will set the default order status for "Cash on delivery" payment gateway (COD) to "On Hold":
add_filter( 'woocommerce_cod_process_payment_order_status', 'change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
So the default order status set by the payment gateway is now "On Hold" instead of "Processing"
In my case,
add_filter( 'woocommerce_cod_process_payment_order_status','change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Worked great in WC 4.42 + WP 5.4.1
Thx!
two solution above are same except:
the solution by #LoicTheAztek has 2 arguments in the core function and have a '10' hook priority
the solution by #Jiří-Prek has an arguments in the core function and have a '15' hook priority
but for my WP5.1.1 and WC3.5.7
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
generating an error
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to
function change_cod_payment_order_status()
so I prefer use the code with only one argument in a main function

WooCommerce - Order Status for Pending Payment Reducing Stock

I use WooCommerce to manually take orders via the telephone / email etc. I use the backend to record the orders manually adding the orders.
At present, when generating an order as 'pending payment' as the status the stock is automatically reduced / deducted. I do not want this to happen. Ideally I only want stock to be reduced when the order is marked as in 'processing' as then payment would of then been taken.
I understand this is how WooCommerce works back is there a way to avoid stock being reduced until a certain status has been selected?
I have tried the below code within functions.php and used the 'on-hold' status to test but the stock is still reduced.
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' )) {
$reduce_stock = false;
}
return $reduce_stock;
}
I wonder if it because I am adding the order as an admin? I know this is how WooCommerce works but ideally I need a method of overriding the 'pending payment' status when creating an order both within the draft and creation stage until marked as 'processing'.
Any help would be fantastic.
Try this adding to functions.php
Note: not tested.
function reduce_stock_processing($order_id) {
wc_reduce_stock_levels($order_id);
}
add_action('woocommerce_order_status_processing', 'reduce_stock_processing');

Change COD default order status to "On Hold" instead of "Processing" in Woocommerce

I need help with a problem-related to plugin "WooCommerce Pay for Payment" which counting some extra fee in shipping. Problem is, that this plugin sets automatically "processing" status in order which causes thanking email for payment (in case of local payment) and don't send email notification about a new order, so customer is confused (I didn't send any money and I received email "thanks for your payment").
I tried this solution: Set WooCommerce order status when order is created from processing to pending
But it only changes order status back to "on-hold" but sends email thank for payment anyway.
Only one thing that I need is to send to the customer in every new order email about a new order, nothing more (I would like to change status to "processing" manually).
Thank you for help, I have no idea how to resolve because I couldn't find PHP file causing a change of status in the plugin.
EDIT: Sorry to all. This was problem of COD in woocommerce plugin. Not Pay for payment as I mentioned. Woocommerce COD automatically set "processing" status.
I found solution for this on github:here
Its the first code.
Based on the answer to this question, this code worked fine for me:
function sv_wc_cod_order_status( $status ) {
return 'on-hold';
}
add_filter( 'woocommerce_cod_process_payment_order_status', 'sv_wc_cod_order_status', 15 );
Updated: The code that you found in Github is outdated, clumsy and complicated, since there is a dedicated filter hook now. You should better try this lightweight and effective code, that will set the default order status for "Cash on delivery" payment gateway (COD) to "On Hold":
add_filter( 'woocommerce_cod_process_payment_order_status', 'change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
So the default order status set by the payment gateway is now "On Hold" instead of "Processing"
In my case,
add_filter( 'woocommerce_cod_process_payment_order_status','change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Worked great in WC 4.42 + WP 5.4.1
Thx!
two solution above are same except:
the solution by #LoicTheAztek has 2 arguments in the core function and have a '10' hook priority
the solution by #Jiří-Prek has an arguments in the core function and have a '15' hook priority
but for my WP5.1.1 and WC3.5.7
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
generating an error
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to
function change_cod_payment_order_status()
so I prefer use the code with only one argument in a main function

Opencart Paypal unsupported currency

Im trying to support the checkout of ZAR (South African Rand).
So far i have enabled $, this enables the paypal module however the conversion isn't being completed.
The site simply checkout to the value. Eg: R1500.00 = $1500.00 when checking out through paypal.
What is the correct way to do the currency conversion using the built in convertor?
Ok found the solution:
Taken from Opencart Forum by user Qphoria
Q: How can I use paypal if my currency isn't supported?
Q: How can I use a payment gateway that doesn't support my currency?
Q: Paypal doesn't support my currency?
A:
You are limited to what the payment gateway supports. However, you can add code to auto-convert your currency to the current exchange rate of a supported currency fairly easy.
(v1.5.x)
1. EDIT: catalog/controller/payment/.php
FIND (FIRST INSTANCE ONLY):
Code: Select all
$order_info = $this->model_checkout_order->getOrder
AFTER, ADD (Replace USD with your choice of valid currency):
Code: Select all
$order_info['currency_code'] = 'USD';
Whatever currency you choose to use, be sure you have it in your list of currencies on your store in the Admin->System->Localisation->Currency page. It doesn't need to be enabled, just has to exist so that the conversion calculation can be done.
This will then auto convert the amount before it is sent to the gateway. The customer won't likely notice this.
They will see, for example, 1000 AED on the checkout page
But you will see $272.25 USD (based on the current conversion rate) in your paypal account.
Up til 1.5.1.3, Paypal Standard did this automatically
In 1.5.2, it was changed (not for the better) to simply disable itself from the list of payments if using an unsupported currency. So that will need special instruction and maybe should be changed back in the core.
For now:
1. EDIT: catalog/model/payment/pp_standard.php
FIND AND REMOVE:
Code: Select all
if (!in_array(strtoupper($this->currency->getCode()), $currencies)) {
$status = false;
}
EDIT: catalog/controller/payment/pp_standard.php
FIND (THE FIRST INSTANCE ONLY):
Code: Select all
$order_info = $this->model_checkout_order->getOrder
AFTER, ADD:
Code: Select all
$currencies = array('AUD','CAD','EUR','GBP','JPY','USD','NZD','CHF','HKD','SGD','SEK','DKK','PLN','NOK','HUF','CZK','ILS','MXN','MYR','BRL','PHP','TWD','THB','TRY');
if (!in_array(strtoupper($this->currency->getCode()), $currencies)) {
$order_info['currency_code'] = 'USD';
}
Change "USD" with your choice of supported currency.
for opencart 2 or 3
the first step is the same
EDIT: catalog/model/extension/payment/pp_standard.php
FIND AND REMOVE:
if (!in_array(strtoupper($this->currency->getCode()), $currencies)) { $status = false; }
the second step is
at catalog/controller/extension/payment/pp_standard.php
find
$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
after it ,and
$order_info['currency_code'] = 'USD';
this solution will exchange all currencies to USD and then go to paypal.com to pay

Categories