Guides

Create another folder labeled stripe-payout in your functions folder, and include a stripe-payout.js file. The following function takes in amount and accoundId body parameters. This function is triggered by a webhook that we will configure on Swell to fire after every order.

We'll be issuing Stripe Transfers with the function below. Transfers accumulate in a Connected Account and are then paid out to the associated bank account/debit card. You can change the interval by visiting the Stripe dashboard's settings and modifying the payout schedule.

We'll need swell-node in order to query our Vendor's account and retrieve their stripe-connect-id to perform the payout:

Install swell-node
npm install swell-node --save

The following is an example of fetching the stripe_connect_id for one order item and transferring funds. For orders containing multiple order items, loop through the order to handle multiple transfers.

Fetch the stripe_connect_id
const stripe = require('stripe')('STRIPE_TEST_KEY');
const swell = require('swell-node');

swell.init('SWELL_STORE_ID', 'SWELL_SECRET_KEY');

exports.handler = async function (event, context) {
    // Only allow POST
    if (event.httpMethod !== "POST") {
        return { statusCode: 405, body: "Method Not Allowed" };
    }

    const body = JSON.parse(event.body);

    // Fetch stripe_connect_id from the vendor_id. 
    const account = await swell.get('/accounts', {
        where: {
            id: body.data.items[0].vendor_id
        }
    });

    const stripeId = account.results[0].stripe_connect_id;
  
    // Payout amounts must be in cents.
    const truncAmt = (body.data.items[0].price_total - body.data.items[0].item_platform_fee).toFixed(2);
    const payoutAmt =
        truncAmt.toString().split('.')[0] + truncAmt.toString().split('.')[1];

    const transfer = await stripe.transfers.create({
        amount: payoutAmt,
        currency: 'cad', // Change to your Stripe currency
        destination: stripeId
    });

    const data = await transfer;
    return {
        statusCode: 200,
        body: JSON.stringify({ data })
    }
}

Now that we have the necessary functions created, we can deploy them to Netlify. The last step is creating Swell webhook to test our functions.

👉

Be sure to commit and push the latest changes to your remote repo—this triggers a new deployment in Netlify every time you push changes to the master branch.

If your folder structure and function naming is correct, select your site in your Netlify account and navigate to the Functions tab. You should see the following functions:

Netlify functions

When selecting a function from this list, you can monitor for any function invocations and console.log commands. You can also see the function endpoint—which we'll use to trigger them via webhook.

Now that we have the endpoints, we can use them to create webhooks within Swell:

In your Swell store, select Developer > Webhooks

  1. Under Webhooks, select Add new webhook
  2. In the webhook url, add the endpoint of your stripe-payout function. You can see this by clicking on the function from the Netlify Functions screen.
  3. Under events, select order.created, this will trigger our stripe-payout function after every order.
  4. Select Save

In order to test our live integration, you'll need to load funds in your Stripe account to make transfers to another Stripe Connect account. You can do this in two separate ways:

  1. By the Stripe dashboard by selecting: Balances > Add to balance. This functionality is only available to US Stripe accounts.
  2. By creating a manual charge using a specific card number, You can do this by selecting: Payments > Create payment > enter $5000 (or other) under Amount > enter 4000 0000 0000 0077 as the card number, any future date as the expiration date, and any 3 digit number for CVC.

Now that we have funds to transfer with our payout functionality, we can test our payout functions.

Create a test order from the Swell Dashboard in order to verify everything we've built in this guide. If successful, you should not see any errors in the function log for the stripe-payout function.

Netlify function testing

Let's check our Stripe connect account to see that the transfer was successful. Select the test Connect account that you created under Connected accounts. If you have a sufficient balance in your Stripe account, you will see a transfer similar to the one below:

Confirming transfer on Stripe

Well done—with everything showing properly, you have now successfully implemented everything you needed to establish a marketplace, onboard vendors for payouts, and facilitate the payouts to the vendors.