4
votes

Screenshot

I want a Subscription in Stripe, which the recurring payment facility will be enabled according to the plan created in stripe dashboard. From Dashboard I get a plan id How could I achieve tagging the customer with the plan Id

<form action="/Stripe/Charge" method="POST">
    <article>
        <label>Amount: $5.00<br/>
               Buy   </label>
    </article>
    <script src="//checkout.stripe.com/v2/checkout.js"
            class="stripe-button"


            data-key="pk_test_...."
            data-locale="auto"
            data-description="Sample Charge"
            data-amount="500">
    </script>

    <article>
        <label>Amount: $29.00 <br/>
            Pro Subscription 
               </label>
    </article>
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_..."

        data-description="Pro Subscription ($29 per month)"
        data-panel-label="Subscribe"
        data-label="Subscribe"
        data-amount="2900">
    </script>


</form>

am I on the correct path? I want to make both subscription and single payment page my doubt is tagging the planId according to users selected plan

1
i am using stripe for payment gateway i cant tag since i don,t have enough reputation - Amal Sebastian
Stripe Payment Integration in Asp.net Web Forms and its 100 percent working code and you can also download application code2night.com/Blog/MyBlog/… - Shubham
If you are looking the latest solution of Stripe subscriptions integration in ASP.NET, check out the demo ==> techtolia.com/StripeSubscriptions - Leo

1 Answers

6
votes

Are you following the ASP.NET MVC tutorial in the Stripe Docs? If not, it’ll help a lot. Have a read before reading the rest of my answer.

In Step 2 you create a controller which charges the customer. It’s in that same block that you’d want to subscribe the customer to your plan. It should end up looking like this:

public IActionResult Charge(string  stripeEmail, string stripeToken)
{
    var customers = new StripeCustomerService();
    var charges = new StripeChargeService();
    var subscriptions = new StripeSubscriptionService();

    var customer = customers.Create(new StripeCustomerCreateOptions {
      Email = stripeEmail,
      SourceToken = stripeToken
    });

    var charge = charges.Create(new StripeChargeCreateOptions {
      Amount = 500,
      Description = "Sample Charge",
      Currency = "usd",
      CustomerId = customer.Id
    });

    var subscription = subscriptions.Create(customer.id, new StripeSubscriptionCreateOptions() {
      PlanId = "your-plan-here"
    };

    return View();
}

Without changing any more code from the tutorial, this will subscribe a customer to the plan your-plan-here as well as charge them $5. This is because all logic is handled in the back-end, the data-amount in the Stripe checkout is only used to display to the user the amount they will be paying.

Personally when having a charge and a subscription I’ll set the data-amount to be the sum of the charge and the first month of the subscription, or I’ll just use a custom form. But it’s up to you to decide what your customers will prefer.


How does Stripe work?

A typical Stripe payment goes as follows:

  1. The user inputs their card details into the Stripe form
  2. Upon submitting the form, Stripe creates a token which is sent to your server, along with some other information outlined here
  3. When the server receives the token the server must use the token and email to create a “Stripe customer.”
    The token “SourceToken” or “source” becomes the customers payment method
  4. The server can then charge the customers account by creating a “charge”, subscribe a customer to a plan by creating a “subscription” as well as a bunch of other things.

In the case of the example above, var customer = customers.Create(...) creates Stripe customer and assigns it to the variable customer. charges.Create(...) issues a charge to a customer and subscriptions.Create(...) subscribes a customer to a plan that you’ve created in the Stripe dashboard.