I am trying to use Paypal REST api with retrofit, but i am getting the following error
{"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id":"2689a7ff7bf28","details":[{"field":"/purchase_units","value":"","location":"body","issue":"MISSING_REQUIRED_PARAMETER","description":"A required field / parameter is missing."},{"field":"/intent","value":"","location":"body","issue":"MISSING_REQUIRED_PARAMETER","description":"A required field / parameter is missing."}],"links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-MISSING_REQUIRED_PARAMETER","rel":"information_link","encType":"application/json"}]
here is the request body
Request{method=POST, url=https://api.sandbox.paypal.com/v2/checkout/orders/?%2Fintent=CAPTURE&%2Fpurchase_units=%5B%7B%22currency_code%22%3A%22USD%22%2C%22value%22%3A%22100%22%7D%5D, tags={class retrofit2.Invocation=c.shobaky.tofahaway.orderRequest.Order() [application/json, Bearer A21AAJ-S4HTuRR7ip1zAflRxUBsIVeEkxAAVWM7ZEB7uvn8g-0GBk6Swa3gwWMIiQYBtsXUssvzKWHv6IHDHYIQjREv6ToYow, CAPTURE, [{"currency_code":"USD","value":"100"}]]}}
Here is the code
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.sandbox.paypal.com/v2/checkout/")
.addConverterFactory(GsonConverterFactory.create(gson)).build();
orderRequest Order = retrofit.create(orderRequest.class);
JSONArray Purchase = new JSONArray();
JSONObject Amount = new JSONObject();
try {
Amount.put("currency_code","USD");
Amount.put("value","100");
} catch (JSONException e) {
e.printStackTrace();
}
Purchase.put(Amount);
Call<String> call = Order.Order("application/json","Bearer A21AAJ-S4HTuRR7ip1zAflRxUBsIVeEkxAAVWM7ZEB7uvn8g-0GBk6Swa3gwWMIiQYBtsXUssvzKWHv6IHDHYIQjREv6ToYow","CAPTURE",Purchase);
call.enqueue(this);
}
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
Log.d("RESPONEPYPAL",call.request().toString()+response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
I got the access token using curl by passing the client id and secret id, so i am almost sure it isn’t about the authorization, it looks like it has something to do with "intent" and "purchase unit"
UPDATE
i used this request before the previous one and also didn’t work
{method=POST, url=https://api.sandbox.paypal.com/v2/checkout/orders/, tags={class retrofit2.Invocation=c.shobaky.tofahaway.orderRequest.Order() [application/json, Bearer A21AAJQvfCFvMKkiWA9anZaEU_hBJ0Pb_TPOQtjqtCjFc39nc_rX6Y5RsCKoYbseMjgiCeGPMhyD5Omqw2tUlgh2EZT-9kiXQ, CAPTURE, [{"amount":{"currency_code":"USD","value":"100"}}]]}}
2
Answers
cURL doesn't work properly in windows prompt, so i used power shell and worked well!
Not only does your "request body" show a URL with query string parameters, which is wrong, but you are also sending an empty POST body to PayPal, which is where the well-formed JSON with a
purchase_units
array needs to be.