Skip to main content
Inspiring
Jun 21, 2022 7:41 AM UTC
Answered

Problems using cfhttp to attach a PaymentMethod to a Customer with the Stripe API

Following the Stripe documentation, which uses curl, I have tried the code:

<cfhttp   method = "POST"  url="https://api.stripe.com/v1/payment_methods/pm_1LCuutGKxC2502V8WNrkE3Ja"   result = "result">
    <cfhttpparam type="header" name="Authorization" value="Bearer sk_test_51...."> 
    <cfhttpparam type="FormField" name="customer" value = "cust_admin_1">
.....
</cfhttp>

 

where pm_1LCuutGKxC2502V8WNrkE3Ja is the Payment Method id from the JSON response when the card payment method was set up.

The above code gives the error: You cannot attach a PaymentMethod to a Customer during PaymentMethod creation. Please instead create the PaymentMethod and then attach it using the attachment method of the PaymentMethods API.

Of course the PaymentMethod had already been created, and indeed if I change the ID slightly I get the message No such PaymentMethod: pm_1LCuutGKxC2502V8WNrkE3Ja22 as would be expected.

 

Thanks in advance for any comments.

    This topic is closed to new replies. Start a new post to keep the conversation going.
    BKBK
    BKBKCorrect answer
    Community Expert
    Jun 21, 2022 11:02 AM UTC

    You're almost there. You forgot the 'attach' command.

    <cfhttp   method = "POST"  url="https://api.stripe.com/v1/payment_methods/pm_1LCuutGKxC2502V8WNrkE3Ja/attach"   result = "result">
        <cfhttpparam type="header" name="Authorization" value="Bearer sk_test_51...."> 
        <cfhttpparam type="FormField" name="customer" value = "cust_admin_1">
    .....
    </cfhttp>

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    Jun 21, 2022 11:02 AM UTC

    You're almost there. You forgot the 'attach' command.

    <cfhttp   method = "POST"  url="https://api.stripe.com/v1/payment_methods/pm_1LCuutGKxC2502V8WNrkE3Ja/attach"   result = "result">
        <cfhttpparam type="header" name="Authorization" value="Bearer sk_test_51...."> 
        <cfhttpparam type="FormField" name="customer" value = "cust_admin_1">
    .....
    </cfhttp>
    Translate
    Inspiring
    Jun 21, 2022 9:02 PM UTC

    Many thanks. This code works.

    Translate