I have a Linux (Ubuntu) server and I would like to run a script from my Ubuntu terminal after each new Stripe subscription. To do this, my server would have to be permanently connected to my stripe account.
I could do this with a crontab that runs, every 15 minutes, a script that connects to the stripe API and detects when a new event occurs. The script would compare the active subscribers with those listed in a file saved on my server in the previous 15 minutes.
I am wondering if using Stripe CLI you could also run a script from the linux command line every time there is a change in subscriptions (new subscription or unsubscribe). In this case, Stripe CLI should permanently connect my linux server to my Stripe account and should allow running a script based on the following condition:
"If there is a new subscription or cancellation in my Stripe account, then run script"
I would like to know if this alternative is possible and if this would be a better option than the first one. I would also like to know how to perform this alternative.
My question is how to enter the condition from the result of the following command line:
$ stripe listen --events=customer.subscription.created
CodePudding user response:
This option would force the server to be working continuously. I would like to know if there is a better option.
A better option for what? To hibernate the server? To prevent a selfmade script from running to detect new events? What about webhooks?
Go with Bash
This one-liner starts Stripe CLI to listen customer.subscription.created
events and continously parses its output with jq
(the set of extracted data is for demo purposes):
stripe listen \
--events=customer.subscription.created \
--format JSON |
while IFS='' read event; do \
echo $event | grep 'data' | \
jq '{event: .type, \
subscr_created: .data.object.created | todate, \
customer: .data.object.customer, \
amount: .data.object.plan.amount}'; \
done
Example output:
{
"event": "customer.subscription.created",
"subscr_created": "2022-06-16T10:12:24Z",
"customer": "cus_Fr31GH4ac3SFhk",
"amount": 2000
}