Home > Blockchain >  Unexpected behaviour of payment_intent api with GET and POST method
Unexpected behaviour of payment_intent api with GET and POST method

Time:09-20

I am using the payment_intent API to generate payment intent for payment sheet initialization.

As per the document, payment_intent is the POST method. Showing different errors in android and iOS. enter image description here

CodePudding user response:

To clarify a few things:

1/ You shared your (test mode) secret key in your code snippet, please delete that and roll your API keys (https://stripe.com/docs/keys#keeping-your-keys-safe).

2/ Your iOS/Android apps should not be making requests to Stripe's APIs directly with your secret API key, as that means you are bundling your secret key with your apps which means anyone running your app has access to your secret key.

Instead, you need to make requests from your iOS app to your server and your server should use Stripe's server-side libraries to make requests to Stripe's APIs. Your iOS/Android apps can only make requests with your publishable key.

3/ The PaymentIntent endpoint supports both POST and GET. You can create a PaymentIntent by POSTing to the /v1/payment_intents endpoint, you retrieve a single PaymentIntent with a GET to the /v1/payment_intents/:id endpoint and you list PaymentIntents with a GET to the /v1/payment_intents endpoint.

4/ The error in your POST request shows "Missing required param: amount." so you need to debug your code to make sure the amount parameter is getting through. You can use Stripe's Dashboard Logs page https://dashboard.stripe.com/test/logs to debug what parameters your code is sending to Stripe's API.

CodePudding user response:

Finally, I found a solution. The issue occurred because I am send parameters without encoding.

I found a solution from this link

https://stackoverflow.com/a/58254052/9158543.

let config = {
            method: 'post',
            url: 'https://api.stripe.com/v1/payment_intents',
            headers: {
                Authorization:
                    'Bearer sk_test_51J3PfGLTjj',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        };

        let paymentDetail = {
            customer: 'cus_MSiYLjtdaJPiCW',
            currency: 'USD',
            amount: 100,
            'automatic_payment_methods[enabled]': true
        };

        let formBody: any = [];
        for (let property in paymentDetail) {
            let encodedKey = encodeURIComponent(property);
            let encodedValue = encodeURIComponent(paymentDetail[property]);
            formBody.push(encodedKey   '='   encodedValue);
        }
        formBody = formBody.join('&');

        const result = await axios
            .post('https://api.stripe.com/v1/payment_intents', formBody, {
                headers: config.headers
            })
            .then(function (response) {
                console.info(JSON.stringify(response));
            })
            .catch(function (error) {
                console.error('-----', error.response);
            });
  • Related