1

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!

4

1 回答 1

0

此代码将两种付款方式添加到结帐: https ://gist.github.com/flymke/c5f49b52c8ecf5069c68b6d9a4e84c76

您需要将两个$klarna_external_payments数组都添加到$request_args['external_payment_methods'].

确保您已将付款方式更改为正确的 ID,在我的示例中,PayPal 称为“ppec_paypal”,因为我们使用的是 PayPal 快速结账而不是常规 PayPal:'kco-external-payment'=> 'ppec_paypal'。您可以在代码中找到它:

// 将此设置为 WooCommerce 中相关付款方式的 ID。

于 2020-09-07T11:34:21.883 回答