Skip to main content

Get Customer's Credit Balance and Apply to Next Charge

In this example you'll get a customer's credit balance and apply it to their next charge.

Pre-requisites

  • You have a Shopify store.
  • You have access to Rewards in your Recharge account. (See Rewards)
  • You have a customer with an existing credit balance and an upcoming charge. (Issue credits through rewards)
  • You have basic knowledge of HTML and JavaScript.

Explanation

In this example we will retrieve the credit summary for a customer and update the customer's settings so that their credits are automatically applied to their next charge.

Example Code

Methods and Types:

<button id="apply-credits-btn">Apply Credits</button>

<script>
document.getElementById('apply-credits-btn').addEventListener('click', async () => {
const session = await getSession();

const customer_credit_summary = await recharge.credit.getCreditSummary(session, { include: ['credit_details'] });
// This will contain information on the customer's current credit balance (see the `CustomerCreditSummary` type):
console.log('customer_credit_summary: ', customer_credit_summary);
// See the customer's current available balance here:
console.log('total_available_balance: ', customer_credit_summary.total_available_balance);

const updated_customer = await recharge.credit.setApplyCreditsToNextCharge(
session,
// Control whether credits should only be applied to recurring charges
{ recurring: true }
);
// updated_customer.apply_credit_to_next_recurring_charge === true
console.log('updated_customer:', updated_customer);

return;
});
</script>