Skip to content

Instantly share code, notes, and snippets.

@flymke
Created September 7, 2020 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flymke/c5f49b52c8ecf5069c68b6d9a4e84c76 to your computer and use it in GitHub Desktop.
Save flymke/c5f49b52c8ecf5069c68b6d9a4e84c76 to your computer and use it in GitHub Desktop.
klarna-checkout-external-payment-methods-for-woocommerce.php
<?php
/*
Plugin Name: Klarna Checkout (V3) External Payment Methods for WooCommerce
Plugin URI: http://krokedil.com
Description: Adds PayPal and BACS as an extra payment method in Klarna Checkout iframe. Works with V3 of Klarna Checkout.
Version: 2.0.0
Author: Michael Schönrock
Author URI: https://www.halloecho.de
*/
/*
Code based on https://github.com/krokedil/klarna-checkout-external-payment-method-for-woocommerce
*/
/*
|--------------------------------------------------------------------------
| PAYPAL
|--------------------------------------------------------------------------
*/
/**
* Extends KCO settings with External Payment Method - PayPal settings.
*/
add_filter( 'kco_wc_gateway_settings', 'kcoepm_form_fields' );
function kcoepm_form_fields( $settings ) {
$settings['epm_paypal_settings_title'] = array(
'title' => __( 'External Payment Method - PayPal', 'kco-epm-wc' ),
'type' => 'title',
);
$settings['epm_paypal_name'] = array(
'title' => __( 'Name', 'kco-epm-wc' ),
'type' => 'text',
'description' => __( 'Title for PayPal payment method. This controls the title which the user sees in the checkout form.', 'kco-epm-wc' ),
'default' => __( 'PayPal', 'kco-epm-wc' ),
);
$settings['epm_paypal_description'] = array(
'title' => __( 'Description', 'kco-epm-wc' ),
'type' => 'textarea',
'description' => __( 'Description for PayPal payment method. This controls the description which the user sees in the checkout form.', 'kco-epm-wc' ),
'default' => '',
);
$settings['epm_paypal_img_url'] = array(
'title' => __( 'Image url', 'kco-epm-wc' ),
'type' => 'text',
'description' => __( 'The url to the PayPal payment Icon.', 'kco-epm-wc' ),
'default' => 'https://www.paypalobjects.com/webstatic/mktg/Logo/pp-logo-100px.png',
);
$settings['epm_paypal_disable_button'] = array(
'title' => __( 'Disable other gateway button', 'kco-epm-wc' ),
'type' => 'checkbox',
'description' => __( 'Disables the "Select another Payment method" button on the Klarna Checkout.', 'kco-epm-wc' ),
'default' => 'no',
);
return $settings;
}
/*
|--------------------------------------------------------------------------
| Vorkasse
|--------------------------------------------------------------------------
*/
/**
* Extends KCO settings with External Payment Method - Vorkasse settings.
*/
add_filter( 'kco_wc_gateway_settings', 'kcoepm_form_fields_vorkasse' );
function kcoepm_form_fields_vorkasse( $settings ) {
$settings['epm_vorkasse_settings_title'] = array(
'title' => __( 'External Payment Method - Vorkasse', 'kco-epm-wc' ),
'type' => 'title',
);
$settings['epm_vorkasse_name'] = array(
'title' => __( 'Name', 'kco-epm-wc' ),
'type' => 'text',
'description' => __( 'Title for Vorkasse payment method. This controls the title which the user sees in the checkout form.', 'kco-epm-wc' ),
'default' => __( 'vorkasse', 'kco-epm-wc' ),
);
$settings['epm_vorkasse_description'] = array(
'title' => __( 'Description', 'kco-epm-wc' ),
'type' => 'textarea',
'description' => __( 'Description for vorkasse payment method. This controls the description which the user sees in the checkout form.', 'kco-epm-wc' ),
'default' => '',
);
$settings['epm_vorkasse_img_url'] = array(
'title' => __( 'Image url', 'kco-epm-wc' ),
'type' => 'text',
'description' => __( 'The url to the vorkasse payment Icon.', 'kco-epm-wc' ),
'default' => '',
);
return $settings;
}
/**
* Add Payment Methods to the KCO iframe.
*/
add_filter( 'kco_wc_api_request_args', 'kcoepm_create_order_paypal' );
function kcoepm_create_order_paypal( $request_args ) {
$kco_settings = get_option( 'woocommerce_kco_settings' );
/* PAYPAL */
$name = isset( $kco_settings['epm_paypal_name'] ) ? $kco_settings['epm_paypal_name'] : '';
$image_url = isset( $kco_settings['epm_paypal_img_url'] ) ? $kco_settings['epm_paypal_img_url'] : '';
$description = isset( $kco_settings['epm_paypal_description'] ) ? $kco_settings['epm_paypal_description'] : '';
$klarna_external_payment_paypal = array(
'name' => $name,
'image_url' => $image_url,
'description' => $description,
'redirect_url' => add_query_arg(
array(
'kco-external-payment' => 'ppec_paypal', // Set this to the ID of the relevant payment method in WooCommerce.
'order_id' => isset( $request_args['merchant_reference2'] ) ? $request_args['merchant_reference2'] : '{checkout.order.id}',
),
wc_get_checkout_url()
),
);
/* VORKASSE */
$name = isset( $kco_settings['epm_vorkasse_name'] ) ? $kco_settings['epm_vorkasse_name'] : '';
$image_url = isset( $kco_settings['epm_vorkasse_img_url'] ) ? $kco_settings['epm_vorkasse_img_url'] : '';
$description = isset( $kco_settings['epm_vorkasse_description'] ) ? $kco_settings['epm_vorkasse_description'] : '';
$klarna_external_payment_vorkasse = array(
'name' => $name,
'image_url' => $image_url,
'description' => $description,
'redirect_url' => add_query_arg(
array(
'kco-external-payment' => 'bacs', // Set this to the ID of the relevant payment method in WooCommerce.
'order_id' => isset( $request_args['merchant_reference2'] ) ? $request_args['merchant_reference2'] : '{checkout.order.id}',
),
wc_get_checkout_url()
),
);
$klarna_external_payments = array( $klarna_external_payment_paypal, $klarna_external_payment_vorkasse );
$request_args['external_payment_methods'] = $klarna_external_payments;
return $request_args;
}
add_action( 'init', 'kcoepm_remove_other_gateway_button' );
function kcoepm_remove_other_gateway_button() {
$kco_settings = get_option( 'woocommerce_kco_settings' );
$disable_button = isset( $kco_settings['epm_paypal_disable_button'] ) ? $kco_settings['epm_paypal_disable_button'] : 'no';
if ( 'yes' === $disable_button ) {
remove_action( 'kco_wc_after_order_review', 'kco_wc_show_another_gateway_button', 20 );
}
}
@flymke
Copy link
Author

flymke commented Sep 7, 2020

This code adds BACS and PayPal to the Klarna Checkout Plugin by krokedil.

@Anniwin
Copy link

Anniwin commented Dec 7, 2021

Hi! This is exactly what I've been looking for, but as the noob I am I cant figure out how to implement this. Where do I add this code? Please help :)

@pgautam15
Copy link

Hi,
I have added code, but it's giving an error while payment with Paypal (Failed to find the payment method for the external payment.)
can you please help me out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment