Hi everyone,
We are building a very simple marketplace and I am currently struggling to get the simple first step to setup Stripe Connect for seller accounts. Our Stripe account is all setup to connect with Standard accounts.
I followed the steps listed on the Stripe site here https://stripe.com/docs/connect/standard-accounts .
Steps 1 to 3 are fine - we have a button on a register page which routes sellers so they can setup / connect their account, then when they’re done we route them back to a URI page we setup where the seller would get some kind of “you’re all set” message. When the user is routed to the URI page in the URL I can get the “ac_xxxxx” code.
This is where it becomes an issue. In the Stripe instructions of step 4 it says to make a POST request to fetch the seller’s credentials
curl https://connect.stripe.com/oauth/token \
-d client_secret=sk_test_XXXXX \
-d code="{AUTHORIZATION_CODE}" \
-d grant_type=authorization_code
which I have translated to the code below, however when I run my code I get the following error.
{
"error": {
"code": "parameter_missing",
"doc_url": "https://stripe.com/docs/error-codes/parameter-missing",
"message": "You must supply either a card, customer, PII data, bank account, or account legal entity to create a token. If you're making this request with a library, be sure to pass all of the required parameters for creating a token. If you're making this request manually, be sure your POST parameters begin with the token type. For example, a PII token would require `pii[personal_id_number]`, while an account token would require a parameter beginning with `account[legal_entity]`. See the API reference for more information: https://stripe.com/docs/api#token_object",
"type": "invalid_request_error"
}
}
I don’t see any other option than having a card or account[legal_entity] parameter in the POST method which makes no sense when looking at the instructions for step 4.
I am going mad… Can anyone help please?
Thanks!!
FRONT END CODE
This is the code of the URI page, where sellers are routed to once having agreed to link their Stripe account to ours. The “completeRegistration” function is where the POST request to Stripe is made to retrieve credentials and I would then parse those in the “response” object once it works.
import wixLocation from 'wix-location';
import { completeRegistration} from 'backend/stripeProxy';
var authCode;
$w.onReady(function () {
let query = wixLocation.query;
authCode = query['code'];
if (authCode === undefined) {
console.log(query['error_description']);
} else {
completeRegistration(authCode)
.then((response) => {
console.log(response);
console.log("Stripe user ID: " + response['stripe_user_id']);
}).catch(e => {
console.log(e);
});
}
});
BACKEND CODE
I build the object following the curl code found on the Stripe page, injecting the seller’s code provided in the URI when connection is made to our Stripe account. I then believe a fetch to the token page needs to be made, but this is where I must be doing something wrong.
export async function completeRegistration(auth_code) {
const apiKey = "sk_test_XXXXX";
var object = {
"client_secret": apiKey,
"code": auth_code,
"grant_type": "authorization_code"
};
const response = await fetch("https://api.stripe.com/v1/tokens", {
method: 'post',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + apiKey
},
body: encodeBody(object)
});
if (response.status >= 200 && response.status < 300) {
const json = await response.json()
return json.id;
}
const responseText = await response.text();
console.log(responseText);
return response;
}
function encodeBody(body) {
var formBody = [];
for (var property in body) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(body[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
console.log(formBody);
return formBody;
}