Subscription

A collection of methods to create, modify, and manage the entire lifecycle of user subscriptions.

create

Create a new subscription for the current user. emits the PelcroSubscriptionCreate DOM events when the response is returned successfully.

Example

const selectedPaymentMethodId = 1234

window.Pelcro.subscription.create({
    auth_token: window.Pelcro.user.read().auth_token,
    source_id: selectedPaymentMethodId,
    payment_gateway: "stripe",
    gateway_token: "example_token",
    campaign_key: "my_promotion",
    plan_id: 123,
    address_id: 123,
  	quantity: 1,
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

Parameters

Parameter

Type

auth_token

string

source_id

number

payment_gateway

string "stripe" \| "braintree"

gateway_token

string

address_id

number

plan_id

number

coupon_code

string

gift_recipient_first_name

string

gift_recipient_last_name

string

gift_recipient_email

string

gift_start_date

string YYYY-MM-DD\ gift_start_date: "2021-09-02"

gift_message

string

pay_via_send_invoice

boolean

quantity

number

domains

string[]\ ["example.com", "example2.com"]

ip_addresses

string[]\ ["192.168.1.1", "136.267.4.6"]

campaign_key

string

cancel

Cancel an existing subscription for the current user. Emits the PelcroSubscriptionCancel DOM event when the response is returned successfully.

Example

window.Pelcro.subscription.cancel({
    auth_token: window.Pelcro.user.read().auth_token,
    subscription_id: 123
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

reactivate

Reactivate a canceled subscription for the current user. Emits the PelcroSubscriptionReactivated DOM event when the response is returned successfully.

Example

window.Pelcro.subscription.reactivate({
    auth_token: window.Pelcro.user.read().auth_token,
    subscription_id: 123
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

renew

Renew an existing subscription for the current user. Emit the PelcroSubscriptionRenewed DOM event when the response is returned successfully.

Example

const selectedPaymentMethodId = 1234
const existingSubIdToRenew = 123

window.Pelcro.subscription.renew({
    auth_token: window.Pelcro.user.read().auth_token,
    source_id: selectedPaymentMethodId,
    payment_gateway: "stripe",
    gateway_token: "example_token",
    campaign_key: "my_promotion",
    plan_id: 123,
    address_id: 123,
    subscription_id: existingSubIdToRenew
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

renewGift

Renew an existing gift subscription for another user as the donor.

Example

const selectedPaymentMethodId = 1234
const donatedSubscriptionIdToRenew = 123

window.Pelcro.subscription.renewGift({
    auth_token: window.Pelcro.user.read().auth_token,
    source_id: selectedPaymentMethodId,
    payment_gateway: "stripe",
    gateway_token: "example_token",
    plan_id: 123,
    address_id: 123,
    subscription_id: donatedSubscriptionIdToRenew,
  	quantity: 1
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

change

Change an existing subscription for the current user.

Example

const existingSubIdToChange = "123"
const planIdToChangeTo = "123"

window.Pelcro.subscription.change({
    plan_id: planIdToChangeTo,
    address_id: "123",
    subscription_id: existingSubIdToChange
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

update

  • Suspend or unsuspend shipments for a subscription.
  • Add email domains / IP addresses to the subscription. Whenever an end-user registers/logs in with the same email domain or IP address, they will become a member of this subscription.

Example

const futureDate = "2030-07-25"

window.Pelcro.subscription.update({
    subscription_id: 2516554,
    suspend: true,
    shipments_suspended_until: futureDate
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

Parameters

auth_token

string

subscription_id

number

shipments_suspended_until

string YYYY-MM-DD

domains

string[]\ ["example.com", "example2.com"]

ip_addresses

string[]\ ["192.168.1.1", "136.267.4.6"]

metadata

Object

\

{
  registration_date: "12-11-2021"
}

redeemGift

Redeem a gift code. Emit the PelcroGiftRedeemed DOM event when the response is returned successfully.

Example

window.Pelcro.subscription.redeemGift({
    gift_code: "123456",
    address_id: "123"
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const user = response.data;
    console.log(user);
})

Parameters

Parameter

Type

gift_code

string

address_id

string

subscription_id used to redeem the gift as a future phase of an existing subscription

number

list

List the subscriptions of the current user.

Example

window.Pelcro.subscription.list();

isSubscribedToPlan

Check if the current user is subscribed to a specific plan.

Example

const isSubscribed = window.Pelcro.subscription.isSubscribedToPlan({ id:123 });
console.log(isSubscribed);

isSubscribedToSite

Check if the current user is subscribed to any plan that isn't a donation plan.

Example

const isSubscribed = window.Pelcro.subscription.isSubscribedToSite();
console.log(isSubscribed);

listMembers

List members of the subscription.

Example

const subscriptionID = 2552828

window.Pelcro.subscription.listMembers({
    auth_token:Pelcro.user.read().auth_token,
    subscription_id: subscriptionID,
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const data = response.data;
    console.log(data);
})

inviteMembers

Invite members to the subscription.

Example

const subscriptionID = 2552828

window.Pelcro.subscription.inviteMembers({
    auth_token:Pelcro.user.read().auth_token,
    subscription_id: subscriptionID,
  	emails: ["@example.com", "@example2.com"]
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const data = response.data;
    console.log(data);
})

removeMember

Remove a member from the subscription.

Example

const subscriptionID = 2552828
const memberID = 2354658

window.Pelcro.subscription.removeMember({
    auth_token:Pelcro.user.read().auth_token,
    subscription_id: subscriptionID,
  	id:memberID
}, (error, response) => {
    if (error) {
    	return console.log("error", error.message);
    }

    const data = response.data;
    console.log(data);
})