खोज…


वाक्य - विन्यास

  • var स्ट्राइपस्क्रिप्शन्सओशन्स = नई स्ट्राइपस्क्रिप्शनस्क्रिट ऑउटऑफ़ेशन ();

    // विकल्प ऑब्जेक्ट को रखने के लिए एक वैरिएबल बनाएं

            stripeSubscriptionOptions.Quantity = model.update;
    

    // सदस्यता के लिए सीटों की मात्रा का उदाहरण विकल्प

            var subscriptionService = new StripeSubscriptionService();
    

    // एपीआई कॉल करने के लिए एक सेवा बनाएं

            var stripeSubscription = subscriptionService.Create(user.CustomerIdentifier, planId,
    

    stripeSubscriptionOptions);

    // service.create (string CustID, string PlanID, Object SubscriptionOptions)

    // कस्टमर आईडी को आपके डेटाबेस से बचाया जाना चाहिए, आप प्लानसाइड का उपयोग करके स्ट्राइप से प्लानिड को प्राप्त कर सकते हैं और ऊपर की तरह ऑप्शन ऑब्जेक्ट बना सकते हैं। यदि आप NuGet स्ट्राइप.नेट इंटैलिजेंस इन के लिए भी काम करते हैं।

टिप्पणियों

अपने नियंत्रक की शुरुआत में कहीं आपको कॉल करना चाहिए

StripeConfiguration.SetApiKey(YOUR SECRET KEY VAR);

और डेटा सुरक्षा के लिए इसे एप्लेटिंग में गुप्त के रूप में छिपा हुआ मान होना चाहिए

यदि आप API कुंजी सेट नहीं करते हैं, तो आप सदस्यता को संशोधित करने या ग्राहकों को बनाने में सक्षम नहीं होंगे

ASP.Net कोर 1.0 में स्ट्राइप.नेट के साथ शुरू

https://github.com/jaymedavis/stripe.net एक शानदार शुरुआती बिंदु है।
मान लें कि आप MVC w / रेज़र का उपयोग कर रहे हैं, तो आपको अपने व्यू पेज में कुछ चीजें होनी चाहिए

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

यह स्क्रिप्ट स्ट्राइक.जेएस पर एक टोकन बनाने के लिए कॉल करती है।

<script type="text/javascript">
        Stripe.setPublishableKey('YOUR STRIPE PUBLIC KEY');

var stripeResponseHandler = function (status, response) {
    var $form = $('#payment-form');

    if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
    } else {
        // token contains id, last4, and card type
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" asp-for="stripeToken" />').val(token));
        // and re-submit
        $form.get(0).submit();
    }
};

jQuery(function ($) {
    $('#payment-form').submit(function (e) {
        var $form = $(this);

        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);

        Stripe.card.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
    });
});
</script>

मॉडल में टोकन जोड़ने के लिए सुनिश्चित करें कि आप बदलते हैं

$form.append($('<input type="hidden" asp-for="stripeToken" />').val(token));

अपने मॉडल को प्रतिबिंबित करने के लिए। रूप इस तरह दिखना चाहिए।

 <form asp-action="confirm" method="POST" id="payment-form">
            <span class="payment-errors"></span>

            <div class="row">
                <label>
                    <span>Card Number</span>
                    <input type="text" data-stripe="number" value="4242424242424242">
                </label>
            </div>

            <div class="row">
                <label>
                    <span>CVC</span>
                    <input type="text" data-stripe="cvc" value="123">
                </label>
            </div>

            <div class="row">
                <label>
                    <span>Expiration (MM/YYYY)</span>
                    <input type="text" data-stripe="exp-month" value="12">
                </label>
                <input type="text" data-stripe="exp-year" value="2020">
            </div>

            <button type="submit">Buy Now</button>
        </form> 

कंट्रोलर ग्राहकों में से एक लेता है और एक CustomerIdentifier के लिए जांच करता है जो कि स्ट्राइप द्वारा दिए गए ग्राहक की आईडी है। यदि इसे डेटाबेस में सहेजा नहीं गया है तो यह उनके लिए एक ग्राहक बनाने पर काम करता है

 public async Task<IActionResult> Index(OrderViewModel model)   //HOME PAGE beginning of managing subs
    {
        //get the user and their ID
        var user = await GetCurrentUserAsync();
        var userId = user?.Id;
        // If they have a customer Identifier use it
        if (!string.IsNullOrEmpty(user.CustomerIdentifier))  //eventually if user has a saved card as well
        {
            //Create the API call to subscription and put its response in a list
            var subscriptionService = new StripeSubscriptionService();
            IEnumerable<StripeSubscription> response = subscriptionService.List(user.CustomerIdentifier);

            ViewBag.Subscription = response;

            ViewBag.Customer = user.CustomerIdentifier;


        }
        ModelState.Clear();
        return View(model);
    }
 public async Task<IActionResult> Confirm(OrderViewModel model, string stripeToken)  // CREATE CHARGE NO CARD/ NO CUSTOMER
    {
        model.stripeToken = stripeToken;
        //get the user and their ID
        var user = await GetCurrentUserAsync();
        var userId = user?.Id;
        var planId = "YOUR PLAN ID HERE";  //plan ID only 1 atm but will need to be a dynamic plan ID list later
        // If they have a customer Identifier use it
        if (!string.IsNullOrEmpty(user.CustomerIdentifier))
        {
            //Create the API call to subscription and put its response in a list
            //Use the subscription options to apply a quantity to the initial subscription for seats
            var stripeSubscriptionOptions = new StripeSubscriptionCreateOptions();
            stripeSubscriptionOptions.Quantity = model.update;
            var subscriptionService = new StripeSubscriptionService();
            var stripeSubscription = subscriptionService.Create(user.CustomerIdentifier, planId, stripeSubscriptionOptions);
            //save Subscriptions data here

            ModelState.Clear();
            //await SaveSubscription(stripeSubscription, user);
            await _userManager.UpdateAsync(user);
        }
        else // Customer is new and doesn't have an ID
        {
            //Create API options
            var customer = new StripeCustomerCreateOptions();
            
            // Add option values
            customer.Email = $"{user.Email}";
            customer.Description = $"{user.Email} [{userId}]";
            customer.PlanId = planId;
            
            customer.SourceToken = model.stripeToken;
            //Make the call to create the customer with the creation options
            var customerService = new StripeCustomerService();
            StripeCustomer stripeCustomer = customerService.Create(customer);

            //save the customer ID
            user.CustomerIdentifier = stripeCustomer.Id;

            //create card update options and add billing info
            var cardOptions = new StripeCardUpdateOptions();
            cardOptions.AddressLine1 = model.BillingInfo.AddressL1;
            cardOptions.AddressLine2 = model.BillingInfo.AddressL2;
            cardOptions.AddressCountry = model.BillingInfo.Country;
            cardOptions.AddressCity = model.BillingInfo.City;
            cardOptions.AddressState = model.BillingInfo.State;
            cardOptions.AddressZip = model.BillingInfo.Zip;
            cardOptions.Name = model.BillingInfo.Name;
            var cardUpdate = new StripeCardService();
            // get the customer card ID and then update the card info
            StripeCustomer customerCardGet = customerService.Get(user.CustomerIdentifier);
            var cardId = customerCardGet.DefaultSourceId;
            StripeCard Card = cardUpdate.Update(user.CustomerIdentifier, cardId, cardOptions);


            //save Subscriptions data here
            //user.ConcurrentUsers = Stripe Quantity
            
            ModelState.Clear();

            await _userManager.UpdateAsync(user);
           
        }
        ViewBag.Success = "confirm";
        return View("Success");
    }

इस विशेष उदाहरण में कुछ एक्स्ट्रा कलाकार शामिल हैं जैसे नियंत्रक में बिलिंग जानकारी लेना। यदि आप यह चाहते हैं तो इसे दृश्य पर फ़ॉर्म में जोड़ें और इसे होल्ड करने के लिए एक मॉडल बनाएं।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow