Android Question CURL translation in B4A - Stripe -

cirollo

Active Member
Licensed User
Longtime User
Hi!

I'm trying to translate in B4A my CURL call to Stripe Payment API, this is the basic call in CURL:

B4X:
curl https://api.stripe.com/v1/charges \
   -u sk_test_4eC39HqLyjWDarjtT1zdp7dc

Stripe docs say that

PI requests. You can manage your API keys in the Dashboard. Your API keys carry many privileges, so be sure to keep them secure! Do not share your secret API keys in publicly accessible areas such as GitHub, client-side code, and so forth.

Authentication to the API is performed via HTTP Basic Auth. Provide your API key as the basic auth username value. You do not need to provide a password.

If you need to authenticate via bearer auth (e.g., for a cross-origin request), use -H "Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc" instead of -u sk_test_4eC39HqLyjWDarjtT1zdp7dc.

All API requests must be made over HTTPS. Calls made over plain HTTP will fail. API requests without authentication will also fail.

now I'm trying to do this in B4A:

B4X:
Dim job1 As HttpJob
       job1.Initialize("JobStripe",Me)
       job1.Download("https://api.stripe.com/v1/charges")
       job1.GetRequest.SetHeader("Authorization","Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc")

and also this
B4X:
    job1.Initialize("JobStripe",Me)
       job1.PostString("https://api.stripe.com/v1/charges","amount=5")
       job1.GetRequest.SetHeader("Authorization","sk_test_8schl5Ig2ZuQe5XIpjnNjKQH")



but when compiling I get:

Building folders structure. (0.06s)
Compilazione del codice. (0.20s)
Compilazione del codice di layouts (0.06s)
Organizzazione Librerie. (0.00s)
Generazione file R. (0.17s)
Compilazione del codice Java prodotto. (4.49s)
Conversione byte code - ottimizzazione dex. Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httpjob;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service$2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service$httputils2service_BR;
5 errors; aborting

where's the problem?

regards,
ciro
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
are you using the library AND the code modules?
You should use either the Library OR the modules but not both at the same time.
 
Last edited:
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
mybe I found the problem.....

I was using the following libraries:

okhttp
okhttputils2
httputils2

removing httputils2 solved the problems...

just a simple question: the code that was using httputils2 will still work now?
 
Last edited:
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
Hi! I succedeed with curl translation to call Stripe API but I'm having some problems on the returned JSON, this is the server response:

B4X:
{
  "id": "tok_1DePTxENBiByw1eYkUZaGFpj",
  "object": "token",
  "card": {
    "id": "card_1DePTxENBiByw1eYstyEqGfG",
    "object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": null,
    "address_zip_check": null,
    "brand": "Visa",
    "country": "US",
    "cvc_check": "unchecked",
    "dynamic_last4": null,
    "exp_month": 12,
    "exp_year": 2020,
    "fingerprint": "XauSvjvOfRW1kGeS",
    "funding": "credit",
    "last4": "4242",
    "metadata": {
    },
    "name": null,
    "tokenization_method": null
  },
  "client_ip": "151.84.255.212",
  "created": 1544112825,
  "livemode": false,
  "type": "card",
  "used": false
}

I'm getting an

java.lang.RuntimeException: JSON Array expected

when reading it like I do with others JSON, this way:

B4X:
       Dim root As List = parser.NextArray
           For Each colroot As Map In root
               StripeToken = colroot.Get("id")
           Next

why???
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
Isn't your root a map instead of a list (using nextObject instead of nextArray)?

I get this:

B4X:
Error occurred on line: 283 (Studenti)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List
   at anywheresoftware.b4a.objects.collections.List.getSize(List.java:129)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
   at anywheresoftware.b4a.BA$2.run(BA.java:365)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:101)
   at android.os.Looper.loop(Looper.java:166)
   at android.app.ActivityThread.main(ActivityThread.java:7425)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
UPDATE

solved using ABM hint and the amazing JsonTree online tool !!!

B4X:
       Dim root2 As Map = parser.NextObject
           StripeToken = root2.Get("id")
           root2.Clear
 
Upvote 0
Top