0

Creating subscription site in wix code. I keep getting a 400 unknown parameter: source error. (/subscripton)

if you can spot where i am going wrong it would be appreciated. thanks!

import { fetch } from 'wix-fetch';

export async function subscription(token, item) {

    const cart = item;

    const apiKey = "PRIVATEAPI";

    const response = await
    fetch("https://api.stripe.com/v1/subscriptions", {

        method: 'post',

        headers: {

            "Content-Type": "application/x-www-form-urlencoded",

            "Authorization": "Bearer " + apiKey

        },

        body: encodeBody(token, cart)
    });
    if (response.status >= 200 && response.status < 300) {

        const ret = await response.json();

        return { "chargeId": ret.id };

    }

    let res = await response.json(); 

    let err = res.error.message;

    let code = res.error.code;

    let type = res.error.type;

    return { "error": err, "code": code, "type": type };

}

 function encodeBody(token, cart) {

    let encoded = "";

    for (let [k, v] of Object.entries(cart)) {

        encoded = encoded.concat(k, "=", encodeURI(v), "&");
    }
     encoded = encoded.concat("source=", encodeURI(token));
     return encoded;
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
TheDylanVoyages
  • 19
  • 1
  • 1
  • 1
  • It's not necessary to say that help is wanted, especially by SHOUTING IT. If you didn't need help, you wouldn't be posting here. – Ken White May 01 '19 at 23:38
  • Hi @TheDylanVoyages, creating a Subscription requires a Customer: https://stripe.com/docs/api/subscriptions/create, It looks like right now you are only passing in a token. You should first create a Customer with that card as their source, then use that Customer.id to create the subscription – hmunoz May 02 '19 at 00:01
  • There seems to be some good reference documentation here: https://stripe.com/docs/api/subscriptions/create?lang=node Notice that stipe provides their own node.js library, perhaps using it would be easier. – e-neko May 02 '19 at 00:04
  • @TheDylanVoyages also, it looks like you are creating a subscription client-side from your page? Subscriptions should be created from your backend, preferably using Stripe's API libraries – hmunoz May 02 '19 at 00:05

2 Answers2

1

welcome to StackOverflow!

It looks like you're creating a Subscription. According to the API docs: https://stripe.com/docs/api/subscriptions/create?lang=ruby

customer is a required parameter when creating subscriptions on Stripe. You would need to create a Customer first, attaching a tokenized card to the Customer as a source. Then, you can create a subscription, by passing customer: customer.id

Also, is this request being made client-side? Requests made with your secret API key should be made from your server-side code and preferably using Stripe's API libraries: https://stripe.com/docs/libraries

Since you're using Subscriptions, you should also look into the new version of Stripe Checkout (https://stripe.com/docs/payments/checkout), it allows creating subscriptions with client-side code with just a few lines of code!

hmunoz
  • 2,791
  • 5
  • 11
-1

You're likely passing additional keys that you're not expecting to when you call encodeBody(token, cart).

You should verify that the keys you're passing in token and cart are all valid according to the documentation at https://stripe.com/docs/api/subscriptions/create.

taintedzodiac
  • 2,658
  • 1
  • 18
  • 16