Stripe Plan and Subscription Functions

I’ve updated StripeX with new Plan & Subscription functions.

Stripes terminology is slightly confusing:

  • A “Plan” is a subscription you sell. So “Tech Support $50/month” or “$3 per month per user license” is a Plan. Your stripe account can have multiple plans.
  • A “Subscription” is when a customer subscribes to a specific plan. A customer can subscribe to more than one plan.
  • So customers have subscriptions while your account has plans.

The .PlanList() function will build a cursor of all the plans on your account while the .PlanRetrieve(id) function will get the information for a specific plan:

SET PROCEDURE TO w:\vfp7com\prog\StripeX ADDITIVE
SET CLASSLIB TO w:\vfp7com\apps\libcurl\libcurl ADDITIVE
local o
o = NEWOBJECT("StripeX")
o.cAPIKey = "YourKeyGoesHere"
IF !o.Authenticate() && Make sure Stripe working
?"Auth", "Failed", o.cErrorMessage
return
endif
LOCAL lCount
lCount = 0
* Get a list of all plans
lCount = o.PlanList()
?"# Plans found : " + TRANSFORM(lCount)
if lCount > 0
LOCAL lCount
lCOunt = 0
SELECT CurStripePlan
lCount = 0
SCAN
lCount = lCount + 1
?" Plan #" + TRANSFORM(lCount) + " ID: " +;
ALLTRIM(CurStripePlan.ID)
ENDSCAN
ENDIF
* Retrieve a plan
SELECT CurStripePlan
GO top
?"Retrieve Plan " + ALLTRIM(CurStripePlan.ID) + " : " + IIF(o.PlanRetrieve(CurStripePlan.ID), "Success", "Fail")
?" ID : " + o.ObjectExtract("id", o.cData)
?" Name : " + o.ObjectExtract("name", o.cData)
?" Amount : " + TRANSFORM(o.ObjectExtract("amount", o.cData))

view raw
StripeXPlanDemo
hosted with ❤ by GitHub

There are four functions that work with subscriptions (a customer must have a Card on file to add subscriptions. Card functions are explained here):

  • SubscriptionCreate – Creates a subscription for a customer
  • SubscriptionDelete – Remove a subscription from a customer
  • SubscriptionList – Builds a cursor of all the subscriptions a customer has
  • SubscriptionRetrieve – Gets information for one subscription

 

SET PROCEDURE TO w:\vfp7com\prog\StripeX ADDITIVE
SET CLASSLIB TO w:\vfp7com\apps\libcurl\libcurl ADDITIVE
local o
o = NEWOBJECT("StripeX")
o.cAPIKey = "YourKey"
IF !o.Authenticate() && Make sure Stripe working
?"Auth", "Failed", o.cErrorMessage
return
endif
LOCAL lCount
lCount = 0
LOCAL lCustomer
lCustomer = "YourCustomerID"
* Get a list of subscriptions for the customer
LOCAL lCount
lCount = o.SubscriptionList(lCustomer)
?"Subscriptions found for customer: " + TRANSFORM(lCount)
if lCount > 0
lCount = 0
SELECT CurStripeSubscription
lCount = 0
SCAN
lCount = lCount + 1
?" Subscription #" + TRANSFORM(lCount) + ": ID=" +;
ALLTRIM(CurStripeSubscription.ID) +;
", Plan ID: " + ALLTRIM(CurStripeSubscription.PlanID)
ENDSCAN
ENDIF
GO top
?"Retrieve : " + IIF(o.SubscriptionRetrieve(lCustomer, CurStripeSubscription.ID), "Success", "Fail")
?"current_period_end: " + DTOC({^1970/01/01 00:00:00} + o.ObjectExtract("current_period_end", o.cData))
* Delete a subscription
SELECT CurStripeSubscription
GO top
LOCAL lDelete
lDelete = o.SubscriptionDelete(lCustomer, CurStripeSubscription.ID)
?"Delete Subscription " + ALLTRIM(CurStripeSubscription.ID) + ": " + IIF(lDelete, "Success", "Failed")
* Add a subscription to a customer
?"Add Subscription " + ALLTRIM(CurStripePlan.ID) + " : " + o.SubscriptionCreate(lCustomer, CurStripePlan.ID, DATE() + 10)

 

 

One thought on “Stripe Plan and Subscription Functions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s