How to implement recurring charge in a Shopify app using Node JS?

Girish_Rajwani
Shopify Partner
80 2 8

Hi guys,

 

i am trying to implement Shopify app recurring charge but i can't find proper Node JS code for the reference. can anyone please share?

 

I am doing this first time hence need proper example.

 

Thanks in advance 🙂 

*Girish Rajwani - Shopify Expert
*Was this helpful? Click Like to let us know!
*Was your question answered? Mark it as an Accepted Solution.
*My Shopify Apps: https://apps.shopify.com/partners/girish-rajwani
Replies 6 (6)

Moeed
Shopify Partner
3697 932 1166

Hey @Girish_Rajwani 

Sure! I can provide you with an example of how to implement Shopify app recurring charges using Node.js. Below is a sample code snippet that demonstrates how to create a recurring charge for a Shopify app using the Shopify API and the 'shopify-api-node' package:

const Shopify = require('shopify-api-node');

// Create a new instance of the Shopify API client
const shopify = new Shopify({
  shopName: 'your-shop.myshopify.com',
  apiKey: 'your-api-key',
  password: 'your-api-password',
});

// Set up the recurring charge parameters
const recurringCharge = {
  name: 'Your Recurring Charge',
  price: '9.99',
  return_url: 'https://your-app.com/charge/accepted',
  test: true, // Set to false when testing is complete
  trial_days: 7, // Optional trial period
};

// Create the recurring charge
shopify.recurringApplicationCharge.create(recurringCharge)
  .then(charge => {
    console.log(charge);
    // Redirect the user to the confirmation URL
    // charge.confirmation_url will contain the URL
  })
  .catch(error => {
    console.error(error);
  });

 

Make sure to replace 'your-shop.myshopify.com', 'your-api-key', and 'your-api-password' with your actual Shopify store's domain, app API key, and app API password respectively. Also, customize the 'recurringCharge' object with the relevant details for your recurring charge.

 

This example uses the 'shopify-api-node package', which provides a convenient way to interact with the Shopify API in Node.js. You can install this package by running 'npm install shopify-api-node' in your project directory.

 

Note that this is just a basic example, and you may need to modify it according to your specific requirements and application logic. Refer to the Shopify API documentation for more information on the available methods and options for recurring charges.

 

If I managed to help you then, don't forget to Like it and Mark it as Solution!

 

Best Regards,
Moeed

Need a Shopify developer? Chat on WhatsApp


- For Shopify Design Changes | Shopify Custom Coding | Custom Modifications
- Your Coffee Tip and my code, A perfect blend. ❤️
Girish_Rajwani
Shopify Partner
80 2 8

hey moeed,

 

getting 401 unauthorized error. i am using default scaffold provided by Shopify. can you please share that whole code OR Link ?

*Girish Rajwani - Shopify Expert
*Was this helpful? Click Like to let us know!
*Was your question answered? Mark it as an Accepted Solution.
*My Shopify Apps: https://apps.shopify.com/partners/girish-rajwani
Ridzz
Shopify Partner
11 2 0

Hi @Girish_Rajwani 


I'm also facing a challenge with implementing recurring charges in Shopify app. Have you found any solution?

Girish_Rajwani
Shopify Partner
80 2 8

@Ridzz  yes it was working but since one day it is giving 422 error.

 

what issue are you facing ? 

*Girish Rajwani - Shopify Expert
*Was this helpful? Click Like to let us know!
*Was your question answered? Mark it as an Accepted Solution.
*My Shopify Apps: https://apps.shopify.com/partners/girish-rajwani
Ridzz
Shopify Partner
11 2 0

Thank you for replying @Girish_Rajwani
I was actually trying to implement Shopify app recurring charge and I was able to get it using the Shopify API RecurringApplicationCharge  🙂

stevenolay
Shopify Partner
2 0 0

I'm pretty confused. Shopify Partner Developer accounts/app don't have a shop domain as far as I can tell. They have a Client ID and a Client Secret. 

I keep getting unauthorized errors and i have no idea what credentials are expected here