0

我尝试为 opencart 集成 klarna 付款。

我从 klarna 获得了一个 client_token,因此显示了 iframe。

下一步是授权测试数据。

不幸的是,我得到 { "show_form": false, "approved": false }

结果回来了。

我发送以下测试脚本:

                        <script>
                            try {
                                    Klarna.Payments.init({
                                      "client_token":"<?php echo $klarna_client_token ?>"
                                    })

                          } catch (e) {
                                // Fehler anzeigen
                                alert(e);
                            }

                                Klarna.Payments.load({
                                  container: '#klarna-payments-container',
                                  payment_method_category: 'pay_later'
                                }, function (res) {

                                    console.log(JSON.stringify(res, null, 4));

                                })



                                Klarna.Payments.authorize({
                                  payment_method_category: "pay_later"
                                }, {
                                  billing_address: {
                                    given_name: "Omer",
                                    family_name: "Heberstreit",
                                    email: "omer@Heberstreit.com",
                                    title: "Herr",
                                    street_address: "Hermannstraße 64",
                                    street_address2: "",
                                    postal_code: "53225",
                                    city: "Bonn",
                                    phone: "+491522113356",
                                    country: "DE"
                                  },
                                  order_amount: 10,
                                  order_tax_amount: 0,
                                  order_lines: [{
                                    type: "physical",
                                    reference: "19-402",
                                    name: "Battery Power Pack",
                                    quantity: 1,
                                    unit_price: 10,
                                    tax_rate: 0,
                                    total_amount: 10,
                                    total_discount_amount: 0,
                                    total_tax_amount: 0,
                                    product_url: "https://www.estore.com/products/f2a8d7e34",
                                    image_url: "https://www.exampleobjects.com/logo.png"
                                  }],
                                  customer: {
                                    date_of_birth: "1970-01-01",
                                  }
                                }, function(res2) {


                                    console.log(JSON.stringify(res2, null, 4));

                                })


                        </script>


                        <div id="klarna-payments-container"></div>

它应该可以工作,因为它来自 klarna 的官方测试数据。有人知道,为什么它不起作用?

问候

4

1 回答 1

0
try {
                                    Klarna.Payments.init({
                                      "client_token":"<?php echo $klarna_client_token ?>"
                                    })

                          } catch (e) {
                                // Fehler anzeigen
                                alert(e);
                            }

                                Klarna.Payments.load({
                                  container: '#klarna-payments-container',
                                  payment_method_category: 'pay_later'
                                }, function (res) {

                                    console.log(JSON.stringify(res, null, 4));

                                })

上面的可以改成:

//The following method initializes the Klarna Payments JS library
    window.klarnaAsyncCallback = function () {
        Klarna.Payments.init({ 
            client_token: '<?php echo $klarna_client_token ?>'
        });
        console.log("Payments initialized");
    //The following method loads the payment_method_category in the container with the id of 'klarna_container'
        Klarna.Payments.load({
        container: '#klarna_container',
            payment_method_category: 'pay_later'
            
        }, function (res) {
               console.log("Load function called")
                console.debug(res);
        });
    };

init 和 load 函数将在 klarnaAsyncCallback 中调用。我注意到授权函数总是返回 { "show_form": false, "approved": false } 并且如果在 init 函数内部调用或不在事件内部调用弹出窗口,则不会出现。

/*The following is the authorize function, which triggers Klarna to perform a risk assessment of the purchase 
  The successful response of this risk assessment is an authorization token, which in this example is logged in the console
*/
  

    $(function(){
        $("button.authorize").on('click', function(){
    Klarna.Payments.authorize({
                                      payment_method_category: "pay_later"
                                    }, {
                                      billing_address: {
                                        given_name: "Omer",
                                        family_name: "Heberstreit",
                                        email: "omer@Heberstreit.com",
                                        title: "Herr",
                                        street_address: "Hermannstraße 64",
                                        street_address2: "",
                                        postal_code: "53225",
                                        city: "Bonn",
                                        phone: "+491522113356",
                                        country: "DE"
                                      },
                                      order_amount: 10,
                                      order_tax_amount: 0,
                                      order_lines: [{
                                        type: "physical",
                                        reference: "19-402",
                                        name: "Battery Power Pack",
                                        quantity: 1,
                                        unit_price: 10,
                                        tax_rate: 0,
                                        total_amount: 10,
                                        total_discount_amount: 0,
                                        total_tax_amount: 0,
                                        product_url: "https://www.estore.com/products/f2a8d7e34",
                                        image_url: "https://www.exampleobjects.com/logo.png"
                                      }],
                                      customer: {
                                        date_of_birth: "1970-01-01",
                                      }
                                    }, function(res2) {
    
    
                                        console.log(JSON.stringify(res2, null, 4));
    
                                    })
        })
      })

例如,授权功能可以通过单击按钮来触发,它似乎会创建弹出窗口并在流程之后返回授权令牌。

这样的事情应该会有所帮助:

<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://x.klarnacdn.net/kp/lib/v1/api.js" async></script>
</head>

<body>



    <script type="text/javascript">
        //The following method initializes the Klarna Payments JS library
        window.klarnaAsyncCallback = function() {
            Klarna.Payments.init({
                client_token: '< client-token >'
            });
            console.log("Payments initialized");
            //The following method loads the payment_method_category in the container with the id of 'klarna_container'
            Klarna.Payments.load({
                container: '#klarna_container',
                payment_method_category: '< pay_later | pay_over_time >'

            }, function(res) {
                console.log("Load function called")
                console.debug(res);
            });
            //Multiple widgets can be loaded by calling the load function again
            /*  Klarna.Payments.load({
                 container: '#klarna_container',
                 payment_method_category: '< pay_later | pay_over_time >'

             }, function(res) {
                 console.log("Load function called")
                 console.debug(res);
             }); */
        };



        /*The following is the authorize function, which triggers Klarna to perform a risk assessment of the purchase 
          The successful response of this risk assessment is an authorization token, which in this example is logged in the console
        */
        $(function() {
            $("button.authorize").on('click', function() {
                Klarna.Payments.authorize({
                    payment_method_category: "< pay_later | pay_over_time >"
                }, {
                    purchase_country: "GB",
                    purchase_currency: "GBP",
                    locale: "en-GB",
                    billing_address: {
                        given_name: "Test",
                        family_name: "Person-uk",
                        email: "customer@email.uk",
                        street_address: "13 New Burlington St",
                        street_address2: "Apt 214",
                        postal_code: "W13 3BG",
                        city: "London",
                        region: "",
                        phone: "01895808221",
                        country: "GB"
                    },
                    order_amount: 1000,
                    order_tax_amount: 0,
                    order_lines: [{
                        type: "physical",
                        reference: "19-402",
                        name: "Battery Power Pack",
                        quantity: 1,
                        unit_price: 1000,
                        tax_rate: 0,
                        total_amount: 1000,
                        total_discount_amount: 0,
                        total_tax_amount: 0,
                        product_url: "https://www.estore.com/products/f2a8d7e34",
                        image_url: "https://www.exampleobjects.com/logo.png"
                    }],
                    //     customer: {
                    //     date_of_birth: "1970-01-01",
                    // },
                }, function(res) {
                    console.log("Response from the authorize call:")
                    console.log(res)
                })
            })
        })
    </script>


    <div style="width: 500px; margin: auto; padding-top: 150px; padding-bottom: 30px;">
        <img src="https://x.klarnacdn.net/payment-method/assets/badges/generic/klarna.svg" style="width: 500px; margin: auto;">
    </div>

    <!--Klarna container-->
    <div id="klarna_container" style="width: 500px; margin: auto;"></div>
    <div style="width: 500px; margin: auto;">
        <!--Button to trigger authorize call-->
        <button class="authorize" style="width: 500px; height: 50px; margin: auto;">Buy Now</button>
    </div>
    </script>
</body>

</html>
于 2022-02-15T13:51:54.370 回答