Android Question Stripe.js Integration

Keith Flanagan

Member
Licensed User
Hi All

I have Stripe Payments for a Customer working with the code listed below.

This is all good, I created the Payment method for this customer in the Stripe dashboard.

However I need to associate and create a payment method with the customer via my own UI in B4A. From reading the documentation https://stripe.com/docs/api/cards/create you can simply create a card with a request like so [using cUrl format]

B4X:
job1.PostString("https://api.stripe.com/v1/customers/cus_HsGfXvel9JmnxK/sources","source=tok_visa")
job1.GetRequest.SetHeader("Authorization","Bearer sk_test_...")


The problem is how to obtain the token, the documentation states "A token, like the ones returned by Stripe.js. Stripe will automatically validate the card. "

So in B4A what is the best way to use Stripe.js and have it return a card token? I am thinking a webview but unsure if its just a matter of creating some local HTML file adding reference to stripe.js and hooking into events from webview in B4A to capture when the token is created?

Any advice on this please?
Thanks


B4X:
Dim job1 As HttpJob
    job1.Initialize("JobStripe",Me)
    
    job1.PostString("https://api.stripe.com/v1/payment_intents","amount=600&currency=eur&customer=cus_Hs2dmIB3Ytxqjz&confirm=true")
    job1.GetRequest.SetHeader("Authorization","Bearer sk_test_...")
    
    Wait For (job1) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString.Length)
        
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim RootObject As Map = parser.NextObject
        Dim charges As Map = RootObject.Get("charges")
        Dim data As List = charges.Get("data")
        For Each coldata As Map In data
            Dim receipt_url As String = coldata.Get("receipt_url")
            WebView1.LoadUrl(receipt_url)
            WebView1.Visible = True
            WebView1.BringToFront
            Button1.Visible = False
            Exit
        Next
    End If
    j.Release
 

drgottjr

Expert
Licensed User
Longtime User
it would be simple enough to try incorporating
stripe.js in a webview.
but you should know that webview is not a full browser.
you may find certain functions that work in chrome are
not supported in a webview.

stripe.js appears not to be the only way to work with stripe.
there is a REST backend (which, i'm guessing, supports
the same functions as stripe.js). that would be fine for
httputils.

they also have a java library, which might plug into b4a
via the use of a javaobject. it would/should function like
httputils (ie, you make a call to the library, it contacts
somebody out there and returns something to you, perhaps
as a string or probably some kind of javaobject). depending
on the complexity of the library, it could work with b4a
nicely. (if you know how to wrap things, you could enable
b4a to use the library more naturally by wrapping it. but
javaobject can get the job done.)

stripe has a lot of documentation. forgive me if i don't
spend the next week going through it all. it was evident,
however, from a quick read that stripe.js isn't the only path.
also, it's pretty clear you (developer) create a "source" as
a json object and pass it to stripe, which returns the token
you refer to. that operation can work with httputils,
provided you know the endpoint for the request. you have
to prompt the customer for all the necessary data, build the
json and send it up the httputils ladder and wait for your
token just like you wait for any other httputils request.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i'm guessing, supports
the same functions as stripe.js). that would be fine for
httputils.
They do!


 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…