I am integrating the Stripe payment gateway with a subscription-based product website in ASP.NET Core 3.1.
I have a Subscription table in the SQL Server database. It has expiration_date (datetime) and is_active (bit) columns.
When the user subscribes to a product with a recurring price (per month), I add a current date + 1 month in the expiration_date column. I also insert 1 in is_active column to indicate that the subscription is active.
Once the first month of subscription has passed, I want the value of is_active flag of this subscription to become 0 in the database. Once the user has paid for the next month, the Current Period will update in stripe subscription and the value of is_active flag is to become 1 again.
For this purpose, I should use Stripe WebHooks. I want to get notified through webhook that a subscription period has ended. I am confused because, as per my understanding, webhook events for customer.subscription and price do not define something that gets triggered when the current period of subscription ends.
The image attached below indicates the current period and date of the next invoice. From documentation I have come to know that upcoming invoice events can be used. But I am not understanding how a webhook can be used with the upcoming invoice.
So, my questions are that which event fires at the end of the final day of one month of the subscription period and which one is fired when a new period is defined?
See https://stripe.com/docs/billing/invoices/subscription#subscription-renewal and https://stripe.com/docs/billing/subscriptions/webhooks#tracking.
When a subscription period ends, an invoice is created(invoice.created) to charge for the upcoming one. So when the cycle ends, you get a customer.subscription.updated event (because current_period_start and current_period_end are updated to the new period) and also an invoice.created event for the invoice that gets created to charge for this new period.
The way I would do it is
listen to the invoice.created event
when handling it, check that the billing_reason is subscription_cycle to differentiate from invoices created for other reasons https://stripe.com/docs/api/invoices/object#invoice_object-billing_reason
retrive the referenced subscription from the invoice(https://stripe.com/docs/api/subscriptions/retrieve) and look at the current_period_[start|end] fields, which is the information you're looking for.
You could also listen to customer.subscription.updated and check if the reason for it updating is the billing period changing(by looking for current_period_start in the previous_attributes of the Event ); or do the same as above but via invoice.paid instead of invoice.created if you only care to update your system when a successful payment is made(thats what the https://stripe.com/docs/billing/subscriptions/webhooks#tracking approach does)
Related
I have 2 states : invoice state keep detail of invoice such as invoice no , amount , vat , payment batch (reference to payment state) etc. and payment state keep detail of payment such as payment batch , items of invoice. What's the best practices to implement flow when invoice paid ?
create updatePaymentFlow for update status to "PAID" in payment state and create updateInoviceFlow for update status to "PAID" in invoice state and then loop of all invoice in payment batch to call updateInoviceFlow.
create only flow updatePaymentAndInvoiceFlow for update both payment state and invoice state.
If there is an example that would be great.
see picture
Looking at the picture I understand that a Payment is dependent on Invoice state, and an Invoice state will have its own status - PAID/UNPAID, and Payment state will have its own status field - PAID/UNPAID. Payment state status will be PAID once all the invoice's have PAID status. I think what is the best way to implement this scenario is what your question is.
I would say have Invoice state as a StatePointer in your Payment state. In this way you can have a flow which will take care of updating the Invoice status. And you can have other flow which takes care of updating your Payment status. In this way, invoice status can be updated independently of Payment. Also having referencing the invoice as StatePointer(Linear Pointer) will always give you the latest updated state in Payment.
I'm building a multisided app where people can sell and buy food. Thats the shortest summarize possible.
When an user make a food order to a restaurant and this order has been marked as dispatched, the app generates a comission on the restaurant profile in firestore.
When the commission is created it triggers a background firebase cloud function that check if the restaurant has an active billing cycle, if not, it creates one like this:
billingCycle: {
openDate: 'the moment where the fee was created',
endDate: '4th day after' //
}
This object its created in the user profile (with correct date) and works ok!
Now I want to emit an invoice when the date of the endDate prop value arrives, here comes the question.
How can I trigger a function when the date of the endDate meet?
I was thinking in moving that decition to the app. That way the app detects when its time to trigger while is using it, but what if the user is not?
How can I trigger the firebase cloud function for emit the invoice independent the interaction of the user with the client app?
I've researched a lot to find an answer but I didn't find anything related so any kind of help is so much appreciated.
*This is the first time Im using FCF.
You can use an onUpdate trigger on documents that may get updated that way. Write code in the function that checks, on every update, if the dates match in the document. If the dates match, emit the invoice, then update the document again with a flag value that indicates the invoice is emitted.
You will also have to use that new flag to determine not to emit the invoice again on further updates (otherwise your function will send an invoice every time it's updated, when the dates match - this boolean will indicate that it already happened).
I have a transaction with very big revenue. I'd like to delete this transaction, using this link: https://support.google.com/analytics/answer/1037443?hl=ru
But I can't understand, what data import type I should choose and And what variables should be passed.
We are going to use Google Analytics hit builder for this purpose.
Follow the steps:
Step 1: You need to have edit permission for the GA property you are trying to do this.
Step 2: Gather transaction data of that particular transaction including shipping and tax.
Step 3: Navigate to hit builder - https://ga-dev-tools.appspot.com/hit-builder
Step 4: Authorize it with your email.
Step 5: Create negative transaction in hit builder.
As you are already having a transaction, you need to apply negative values of the transaction with the same transaction ID.
v=1 // Version.
&tid=xxxxxx // Add your tracking ID here
&t=transaction // Transaction as hit type
&ti=xxxxx // Transaction ID
&tr=-xxx // Negative transaction revenue
&ts=-xx //Negative transaction Shipping
&tt=-xx //Negative transaction tax
&cu=XXX //Currency code
You can use any string as client ID.
Fill up the values by adding parameters and your hit will look something like this.
Once this is filled up, click on 'Validate Hit'
If the hit is validated, click on 'Send to Google Analytics'
Note: It will take some time for data to show up.
I have used this on a test account where I had added the transaction using hit builder, never really faced any situation where I need to do it on live. Let me know if this works.
I need to send a cart status update based on an offline activity. My customers will add items to their cart online via the website but the final checkout and purchase happen offline.
Here is my question: In the example below, a checkout step is updated. Does the client_id provided in this step have to match the original client_id used when the web user started their cart originally?
Measuring Checkout Options
v=1 // Version.
&tid=UA-XXXXX-Y // Tracking ID / Property ID.
**&cid=555 // Anonymous Client ID.**
&t=event // Event hit type
&ec=Checkout // Event Category. Required.
&ea=Option // Event Action. Required.
&pa=checkout_option // Product action (checkout_option).
&cos=2 // Checkout step.
&col=FedEx // Checkout step option.
Client ID is how Google Analytics tracks each individual user throughout their multiple sessions. So, yes- if you want that payload to be associated with the same user, then the CID will need to match- otherwise you'll be artificially inflating your user count (as well as severing the contextual data from that user).
You can parse the cookie that GA stores in the browser to obtain the CID and then fill it there. Not sure what language you're writing in, but there are plenty of snippets out there for parsing the GA cookie. You could also store the CID when it is initially generated in your own session data to be used later while offline. For example:
ga(function(tracker) {
var clientId = tracker.get('clientId');
console.log(clientId);
});
You could also provide your own CID from the start that you generate yourself and tell Google Analytics to use. This might be overkill for your situation, but I wanted to mention it as an option.
I have implemented PayPal on my site. When user choose service to pay he is redirected to pay pal where he commit payment and in the end he is redirected back to my site.
Here I mix IPN and PDT. After payment I receive IPN message and save all variables in database.
When user is back to the site here I get his TXN_ID and UserID (from session) and store it in database.
I read somewhere that it is bad to mix pdt and ipn. Is there any better way to save IPN variables with UserID? Can I somehow send UserId to pay pal and get it with IPN message later when user finish payment?
I'm sorry I have to reopen question. I added 'invoice' field to send UserID to pay pal. But when I try to pay next time with same user ID I am redirected to pay pal page and I receive this error message:
This invoice has already been paid. For more information, please
contact the merchant.
It seams that invoice is not a good option to send user id to pay pal.
I tried to put user ID in "custom" field.
<input name="custom" type="hidden" id="custom" value="<%= UserInfo.UserID%>">
But all I get in IPN message is:
custom=%3C%25%3D+UserInfo.UserID%25%3E
The way I do it is to use the invoice field. This is a field you can send PayPal and they will send it back in the response. This is how I identify transactions coming back from PayPal.
And in my case I don't process both PDT and IPN messages. I process only the one that hits me first. When the second message (PDT or IPN) arrives I check that I have already processed that transaction and I simply discard it.
Also, please don't forget that you need to verify that the messages came from PayPal. Once you get the PDT or IPN message you need to post back to PayPal with your unique Auth_id and the tx token they sent in the original message. You should not rely on any field from the original message.
Once PayPal receives your confirmation request, it will send you a second message with the transaction details. This is the one you should rely on.
I don't see any benefit on processing both PDT and IPN messages for the same transaction. Having said that I do encourage you to implement both, because none of them are guaranteed to reach you.
Edit to include how to pass the invoice to PayPal
To pass the invoice to PayPal, just create a field like:
<input type="hidden" name="invoice" value=$uniquevaluecreatedbyyourapplication>
The unique id could be your user id.
The post to PayPal will be something like
paypal/or/sandbox/address?all the other fields&invoice=123456
Paypal will send back the invoice in the PDT and IPN messages.
Edit - non-unique invoice id number
I did further research and you can configure PayPal to accept multiple transactions for the same invoice id or to accept only one transaction per invoice id. See below the information straight from PayPal's website:
Blocking Accidental Payments
You can choose whether to accept payments with the same Invoice ID or
to block payments when the Invoice ID was already used.
When you receive payments from buyers, you can include an optional
Invoice ID field to track payments; buyers will not see this ID. A
buyer's Invoice ID must be unique for each transaction. If PayPal
receives a payment with an Invoice ID that was used for another
payment, PayPal will not accept the transaction.
To set the options for blocking this type of payment:
Log in to your PayPal account at https://www.paypal.com.
The My Account Overview page opens.
Click the Profile subtab.
The Profile Summary page opens.
In the Selling Preferences column, click the Payment Receiving
Preferences link.
The Payment Receiving Preferences page opens.
Scroll down the page to the Block accidental payments section as shown
below.
Select one of the following options:
Yes, block multiple payments per invoice ID – Do not accept more
than one payment per invoice. This option prevents duplicate payments
that you must refund.
No, allow multiple payments per invoice ID – Do not limit the number
of payments received for an invoice. Choosing this option might result
in duplicate payments that require a refund.
Scroll to the bottom of the page and click the Save button.
Another option would be to create your invoice id with user id + a random number. This would make it unique.
See below a sample code to get unique invoice numbers.
var r = Math.floor(Math.random()*100000);
var invoice = userid + "-" + r;
You could also use date & time instead of a random number.
var d = new Date();
var invoice = userid + "-" + d;
And in the response back from PayPal, you just discard what comes after the dash.
I hope this helps.