B4J Question Sumup API again

udg

Expert
Licensed User
Longtime User
Hi all,

I successsfully used libraries and snippets from the forum to use my Sumup account, but today I'd like to try direct use of their APIs.
A few lines in B4J should suffice to have a start, but it seems there's something eluding me.

SumUp specs

I'll go for the Client Credentials flow, but I can't figure out how to pass in the required parameters.
Another point that puzzles me is what's stated above the CURL/PHP example: "To request an access token for this flow you need to make a request to the retrieve a token endpoint."
Following that link to generate a token it seems that you need an already allowed "code" (The authorization code that you received from requesting an authorization code.)

So, how do I implement the authenticate-authorize-make payment chain?

TIA

Note: I'm aware of the sup_sk xxxx code as a static token but I'd like to learn how to use the above auth chain.
 
Last edited:

udg

Expert
Licensed User
Longtime User
Well, I guess I found how to start.
It wasn't clear that I had to pass a single string with the required parameters separated by the ampersand symbol.
Something like this seems to work
B4X:
pJob.PostString("https://api.sumup.com/token", "grant_type=client_credentials&client_id=5wxxxx&client_secret=2105caxxxx")
At least, it returns an at_classicXXX access token..
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
1689422999474.png

EDIT
B4X:
'https://developer.sumup.com/docs/api/generate-a-token/
Public Sub TestSumupPostURL
    Dim Link As String = "https://api.sumup.com/token"
    
    Dim Data As Map = CreateMap("client_id" : "xxxx", "client_secret" : "xxxxx", "code" : "xxxxx", "grant_type" : "refresh_token", "refresh_token" : "xxxxx")
    Dim Parameters  As String = Data.As(JSON).ToString
    Log(Parameters)

    Wait For (SumupPostURL(Link, Parameters)) Complete (DataResult As String)
    Log(DataResult)
    
End Sub

Public Sub SumupPostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetContentType("application/json")
        j.GetRequest.SetHeader("Accept","application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub

1689424248322.png
 
Last edited:
  • Like
Reactions: udg
Upvote 0

udg

Expert
Licensed User
Longtime User
Thank you @TILogistic
I tried with maps and json (where required) but it seems that those APIs are sucessfully called only with the "ampersand method". I don't know why.
Even (for a checkout) using Json generator makes the API reply with a "missing parameters" error.

Anyway, it was an exercise to understand how those APIs work. I was successful in authenticate and creating a checkout. Didn't try the real payment but I expect it to go on the same lines as the other two.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Thank you @TILogistic
I tried with maps and json (where required) but it seems that those APIs are sucessfully called only with the "ampersand method". I don't know why.
Even (for a checkout) using Json generator makes the API reply with a "missing parameters" error.

Anyway, it was an exercise to understand how those APIs work. I was successful in authenticate and creating a checkout. Didn't try the real payment but I expect it to go on the same lines as the other two.
Is RawData JSON
change: j.GetRequest.SetContentType("text/plain")
B4X:
Public Sub SumupPostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetHeader("Accept","application/json")
'        j.GetRequest.SetContentType("application/json")
        j.GetRequest.SetContentType("text/plain")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
1689426300344.png
 
  • Like
Reactions: udg
Upvote 0

udg

Expert
Licensed User
Longtime User
Is RawData JSON
change j.GetRequest.SetContentType("text/plain")
That explais the strange behavior I experienced following their examples.
I hope to have some more time next week to assemble a complete working demo from authenticate to payment. Just to have it ready when it will be useful for some new project..
 
Upvote 0
Top