πŸ“£ The forum has moved to GitHub

We’ve switched to GitHub Discussions as the hub for our community. This will improve the experience of collaborating for everyone, sharing what you’re working on, and discussing ideas for how Swell can be improved. Existing threads will remain open, but new posts are disabled.

Go to GitHub β†’

retrieve invoice data with Swell-js?

Hi all, I'm just building out the account page of our headless store. We sell a few books but the main revenue driver for the site is the subscriptions we offer.

I can easily grab subscription or order data for the customer but none of the returned data holds the actual payments/invoices. I can grab the total of all invoices from the subscription object and I can obviously grab the orders from the order object... only thing is the orders object only returns the initial order, not the subsequent charges that are tied to that order/subscription.

Any ideas how I can pull the payments associated with a subscription? I'm looking to map through them all so a subscriber ca see when they've been charged.


Thanks so much!

Mark

Comments

  • Hey all, it's been a few days so thought I'd follow with a bump... any help much appreciated 🀘

  • Hi @Marcin Chmiel

    That feature you mentioned is being worked on and will be released soon.

    In the meantime, our engineers suggested a workaround and you can try it:


      // the customer should be logged
        await swell.account.login('email', 'password');
        let total = 0;
    
      // get selected subscription with all its orders
        const subscriptionResults = await swell.subscriptions.get({
          id: '62a0af3146ff521a438ad62d',
          expand: ['orders:0'],
        })
    
        // get first item from the array
        const subscription = subscriptionResults?.results?.length ? subscriptionResults.results[0] : null;
        if (subscription) {
          if (subscription?.orders?.results) {
            // get each order with its payments
            for (const order of subscription.orders.results) {
              const orderWithPaymentsResults = await swell.get('/orders', {
                id: order.id,
                expand: ['payments:0'],
              });
    
              const orderWithPayments = orderWithPaymentsResults?.results?.length ? orderWithPaymentsResults.results[0] : null;
              if (orderWithPayments?.payments?.results) {
                for (const payment of orderWithPayments.payments.results) {
                  // handle this payment
                  console.log(payment);
                  if (payment.status === 'success') {
                    total = total + payment.amount;
                  }
                }
              }
            }
          }
        }
    

    I hope this is useful for you.

  • Amazing, thanks so much @Gonzalo Rosso - I really appreciate your help!


    All the best

Sign In or Register to comment.