-1

I am recieving an authorization call back as follows:

<script>
$(function(){
    $("button.authorize").on('click', function(){
        Klarna.Payments.authorize({
            payment_method_category: ''
             
             }, function(res) {
                console.log("Response from the authorize call:")
                console.log(res)
                            })
    })
  })
   
</script>

Which returns:

Object { show_form: true, approved: true, finalize_required: false, authorization_token: "mytoken" }

How can I use the value of authorization_token in the following url:

https://api.playground.klarna.com/payments/v1/authorizations/**res.authorization_token**/order
knittl
  • 246,190
  • 53
  • 318
  • 364
Ria
  • 516
  • 6
  • 24
  • Does this answer your question? [Send JSON data from Javascript to PHP?](https://stackoverflow.com/questions/8599595/send-json-data-from-javascript-to-php) – CherryDT Oct 04 '22 at 14:16
  • 1
    `res.authorization_token`? – ControlAltDel Oct 04 '22 at 14:16
  • @ControlAltDel I did the following: https://api.playground.klarna.com/payments/v1/authorizations/res.authorization_token?/order but that did come up with forbidden any idea how to use the res.authorization_token? – Ria Oct 04 '22 at 14:22
  • TBH I'm not sure what this question has to do with [tag:php]. – knittl Oct 04 '22 at 19:57

1 Answers1

0

Use string concatenation or template strings:

  • Concatenation: var s = 'https://api.playground.klarna.com/payments/v1/authorizations/' + res.authorization_token + '/order';
  • Template strings: var s = `https://api.playground.klarna.com/payments/v1/authorizations/${res.authorization_token}/order`;

(Another method which I wouldn't recommend is joining an array to build the final string: var s = ['https://api.playground.klarna.com/payments/v1/authorizations', res.authorization_token, 'order'].join('/');. Nothing to gain by this, but I've added it for completeness' sake)

But it's probably a good idea to do a tutorial on the basics of JavaScript before integrating a third-party payment API. So much that can go wrong and money is involved.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thank you for your reply, however when I use res.authorization_token in the url ot return a bad authorization token is there a way I can check that the console.log(res) is getting the token? Print it out somehow? I am very sorry but I just am very stuck – Ria Oct 04 '22 at 15:03
  • @Ria I'm not sure I understood your question. `console.log` will print any string (or object) that you pass to it. – knittl Oct 04 '22 at 15:07
  • I am so sorry, I cannot get the results to display/use on my page I would like to print the authorization token on my page so it would say something like "mytoken" as a result – Ria Oct 04 '22 at 15:14
  • 1
    And cross fingers that `res.authorization_token` isn’t `../../../delete-all-my-stuff` – user3840170 Oct 04 '22 at 15:24
  • @Ria "print on my page" does that mean adding it to the DOM or logging it with `console.log`? Note that "how to add something to the DOM" is a very different question from "how to use a JSON property in my request's URL". If you have a second question, please post a new one. – knittl Oct 04 '22 at 16:59