Search code examples
phpwoocommerceklarna

Add PayPal and BACS to Klarna Checkout


I'm trying to add PayPal and BACS (Brank Transfer) to my Klarna Checkout for WooCommerce.

I found the example Plugin from Klarna (https://github.com/krokedil/klarna-checkout-external-payment-method-for-woocommerce/blob/master/klarna-checkout-external-payment-method-for-woocommerce.php)

This works perfectly but when I try to change the code to display and use the BACS payment, I only get the BACS option displayed and not PayPal and BACS.

This is the example Code for PayPal provided by krokedil (I have cut out the code for the Plugin settings in the backend):

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' );
    $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 = array(
        'name'         => $name,
        'image_url'    => $image_url,
        'description'  => $description,
        'redirect_url' => add_query_arg(
            array(
                'kco-external-payment' => '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()
        ),
    );

    $klarna_external_payment                  = array( $klarna_external_payment );
    $request_args['external_payment_methods'] = $klarna_external_payment;

    return $request_args;
}

I came up with the following code which will indeed display the BACS Payment Method and allow the payment. However the PayPal Payment Option disappers as soon, as I enable my code.

add_filter( 'kco_wc_api_request_args', 'kcoepm_create_order_bacs' );
function kcoepm_create_order_bacs( $request_args ) {

    $klarna_external_payment = array(
        'name'         => 'BACS',
        'image_url'    => 'https://via.placeholder.com/350x150',
        'description'  => 'Pay via Banktransfer',
        '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_payment                  = array( $klarna_external_payment );
    $request_args['external_payment_methods'] = $klarna_external_payment;

    return $request_args;
}

I don't know how I can register both payment methods simultaneously. I think it has something to do with the array not being registered.

I found this notice on the krokedik documentation but don't know how I can implement it:

"Each defined external payment method sent in the request to Klarna should be added as an array as described in the documentation here: https://developers.klarna.com/api/#checkout-api__create-a-new-order__external_payment_methods."

Any help would be much appreciated!


Solution

  • This code adds both payment methods to the checkout: https://gist.github.com/flymke/c5f49b52c8ecf5069c68b6d9a4e84c76

    You need to add both $klarna_external_payments arrays to $request_args['external_payment_methods'].

    Make sure you've changed the payment methods to the correct id's, in my example PayPal is called "ppec_paypal" since we are using PayPal express checkout and not regular PayPal: 'kco-external-payment' => 'ppec_paypal'. You can find it in the code where it says:

    // Set this to the ID of the relevant payment method in WooCommerce.