Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle situations where billing country is unset #378

Merged
merged 4 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion classes/class-kp-callbacks.php
Expand Up @@ -67,7 +67,7 @@ public function kp_wc_authorization( $data ) {
}

$auth_token = $data['authorization_token'];
$country = $order->get_billing_country();
$country = kp_get_klarna_country( $order );

// Dont do anything if the order has been processed.
if ( $order->has_status( array( 'on-hold', 'processing', 'completed' ) ) ) {
Expand Down
4 changes: 2 additions & 2 deletions classes/requests/helpers/class-kp-order-data.php
Expand Up @@ -101,7 +101,7 @@ public function get_klarna_order_object( $iframe_options = null ) {

return array(
'purchase_country' => $this->klarna_country,
'purchase_currency' => get_woocommerce_currency(),
'purchase_currency' => empty( $order ) ? get_woocommerce_currency() : $order->get_currency(),
'locale' => kp_get_locale(),
'order_amount' => $this->order_data->get_total(),
'order_tax_amount' => $this->order_data->get_total_tax(),
Expand Down Expand Up @@ -251,7 +251,7 @@ public function get_klarna_customer_object( $customer_type = null ) {
'postal_code' => $strip_postcode_spaces ? $customer_data->get_billing_postcode() : str_replace( ' ', '', $customer_data->get_billing_postcode() ),
'city' => $customer_data->get_billing_city(),
'region' => $customer_data->get_billing_state(),
'country' => $customer_data->get_billing_country(),
'country' => empty( $customer_data->get_billing_country() ) ? kp_get_klarna_country() : $customer_data->get_billing_country(),
);

$shipping = array(
Expand Down
16 changes: 12 additions & 4 deletions includes/kp-functions.php
Expand Up @@ -50,12 +50,20 @@ function get_klarna_customer( $customer_type ) {
*/
function kp_get_klarna_country( $order = false ) {
if ( ! empty( $order ) ) {
return apply_filters( 'wc_klarna_payments_country', $order->get_billing_country() );
$country = $order->get_billing_country();

// If the billing_country field is unset, $country will be empty.
if ( ! empty( $country ) ) {
return apply_filters( 'wc_klarna_payments_country', $country );
}
}

/* The billing country selected on the checkout page is to prefer over the store's base location. It makse more sense that we check for available payment methods based on the customer's country. */
if ( method_exists( 'WC_Customer', 'get_billing_country' ) && ! empty( WC()->customer ) && ! empty( WC()->customer->get_billing_country() ) ) {
return apply_filters( 'wc_klarna_payments_country', WC()->customer->get_billing_country() );
/* The billing country selected on the checkout page is to prefer over the store's base location. It makes more sense that we check for available payment methods based on the customer's country. */
if ( method_exists( 'WC_Customer', 'get_billing_country' ) && ! empty( WC()->customer ) ) {
$country = WC()->customer->get_billing_country();
if ( ! empty( $country ) ) {
return apply_filters( 'wc_klarna_payments_country', $country );
}
}

/* Ignores whatever country the customer selects on the checkout page, and always uses the store's base location. Only used as fallback. */
Expand Down