条纹:创建条纹会话对象-invalid_request_error

时间:2019-03-30 13:10:00

标签: java retrofit2 stripe-payments

在我的Java项目中,我想创建Stripe会话对象。 这里的api方法: https://stripe.com/docs/api/checkout/sessions/create

如果使用curl比成功获取json响应:

curl https://api.stripe.com/v1/checkout/sessions \
  -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
  -H "Stripe-Version: 2019-03-14; checkout_sessions_beta=v1" \
  -d success_url="https://example.com/success" \
  -d cancel_url="https://example.com/cancel" \
  -d payment_method_types[]=card \
  -d line_items[][name]=T-shirt \
  -d line_items[][description]="Comfortable cotton t-shirt" \
  -d line_items[][amount]=1500 \
  -d line_items[][currency]=usd \
  -d line_items[][quantity]=2

好。 现在,我想通过 Retrofit 2 在Java上执行相同的操作。

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import retrofit2.Response;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        System.out.println("Start request");

        Map<String, Object> bodyMap = getParams();
        printMap(bodyMap);
        TransportService.checkoutSessions(bodyMap, new DefaultRestClientCallback<Void>() {
            @Override
            public void onSuccess(Response<Void> response) {
                //super.onSuccess(response);
                System.out.println("checkoutSessions: onSuccess: response = " + response);
            }

            @Override
            public void onError(ErrorResponse errorResponse) {
                System.out.println("checkoutSessions: onError: errorResponse = " + errorResponse);
            }
        });
    }

    private static Map<String, Object> getParams() {
        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("success_url", "https://example.com/success");
        bodyMap.put("cancel_url", "https://example.com/cancel");
        List<String> payemtnMethodType = new ArrayList<String>();
        payemtnMethodType.add("card");
        bodyMap.put("payment_method_types", payemtnMethodType);
        JsonArray lineItemsJsonArray = new JsonArray();
        JsonObject lineItemsJsonObject = new JsonObject();
        lineItemsJsonObject.addProperty("name", "T-shirt");
        lineItemsJsonObject.addProperty("description", "Comfortable cotton t-shirt");
        lineItemsJsonObject.addProperty("amount", 1500);
        lineItemsJsonObject.addProperty("currency", "usd");
        lineItemsJsonObject.addProperty("quantity", 1);
        lineItemsJsonArray.add(lineItemsJsonObject);
        bodyMap.put("line_items", lineItemsJsonArray);

        return bodyMap;
    }

    private static void printMap(Map<String, Object> map) {
        map.forEach((k, v) -> {
            System.out.println("key : " + k + " value : " + v);
        });
    }



import retrofit2.Call;
import retrofit2.Callback;

import java.util.*;

public class TransportService {

    public static void checkoutSessions(Map<String, Object> bodyMap, Callback<Void> callback) {
        StripeRestClient stripeMonitorRestClient = RestClientFactory.createRestClient(StripeRestClient.class);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Accept", "application/json");
        headerMap.put("Content-Type", "application/x-www-form-urlencoded");
        headerMap.put("Stripe-Version", "2019-03-14");
        headerMap.put("checkout_sessions_beta", "v1");
        headerMap.put("Authorization", "Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc");
        Call<Void> adMessagesList = stripeMonitorRestClient.checkoutSessions(headerMap, bodyMap);
        // asynchronously
        adMessagesList.enqueue(callback);
    }
}



   import retrofit2.Call;
import retrofit2.http.*;

import java.util.Map;

public interface StripeRestClient {

    @POST("checkout/sessions")
    Call<Void> checkoutSessions(@HeaderMap Map<String, String> headers, @Body Map<String, Object> body);

}



But when I start Java app I get error - **invalid_request_error**.

{
  "error": {
    "code": "parameter_unknown",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-unknown",
    "message": "Received unknown parameter: {\"payment_method_types\":",
    "param": "{\"payment_method_types\":",
    "type": "invalid_request_error"
  }
}
Retrofit log:

gradlew run
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> Task :run
Start request
key : payment_method_types value : [card]
key : line_items value : [{"name":"T-shirt","description":"Comfortable cotton t-shirt","amount":1500,"currency":"usd","quantity":1}]
key : cancel_url value : https://example.com/cancel
key : success_url value : https://example.com/success
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://api.stripe.com/v1/checkout/sessions http/1.1
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/x-www-form-urlencoded
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: Content-Length: 240
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: Accept: application/json
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: checkout_sessions_beta: v1
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: Stripe-Version: 2019-03-14
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: 
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: {"payment_method_types":["card"],"line_items":[{"name":"T-shirt","description":"Comfortable cotton t-shirt","amount":1500,"currency":"usd","quantity":1}],"cancel_url":"https://example.com/cancel","success_url":"https://example.com/success"}
Mar 31, 2019 5:37:17 PM okhttp3.internal.platform.Platform log
INFO: --> END POST (240-byte body)
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: <-- 400 Bad Request https://api.stripe.com/v1/checkout/sessions (1573ms)
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: Server: nginx
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: Date: Sun, 31 Mar 2019 14:37:20 GMT
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/json
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: Content-Length: 278
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: Connection: keep-alive
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: access-control-allow-credentials: true
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: access-control-allow-methods: GET, POST, HEAD, OPTIONS, DELETE
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: access-control-allow-origin: *
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: access-control-expose-headers: Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: access-control-max-age: 300
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: cache-control: no-cache, no-store
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: request-id: req_BcsEOssLr3JsIM
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: stripe-version: 2019-03-14
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: Strict-Transport-Security: max-age=31556926; includeSubDomains; preload
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: 
Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: {
  "error": {
    "code": "parameter_unknown",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-unknown",
    "message": "Received unknown parameter: {\"payment_method_types\":",
    "param": "{\"payment_method_types\":",
    "type": "invalid_request_error"
  }
}

Mar 31, 2019 5:37:19 PM okhttp3.internal.platform.Platform log
INFO: <-- END HTTP (278-byte body)
checkoutSessions: onError: errorResponse = ErrorResponse{ code = 0, message = 'null'}

0 个答案:

没有答案