stripe-payments
Stripe.Net 소개
수색…
통사론
var stripeSubscriptionOptions = 새로운 StripeSubscriptionCreateOptions ();
// options 객체를 저장할 변수를 만듭니다.
stripeSubscriptionOptions.Quantity = model.update;// 구독의 좌석 수에 대한 예제 옵션
var subscriptionService = new StripeSubscriptionService();// API 호출을 만들 서비스를 만듭니다.
var stripeSubscription = subscriptionService.Create(user.CustomerIdentifier, planId,stripeSubscriptionOptions);
// service.create (string CustID, string PlanID, Object SubscriptionOptions)
// 고객 ID를 데이터베이스에서 저장해야하는 경우 PlanService를 사용하여 스트라이프에서 planID를 검색하고 위와 같은 옵션 객체를 만들 수 있습니다. NuGet Stripe.Net intellisense가 이러한 경우에도 작동합니다.
비고
컨트롤러 시작 부분에 전화해야합니다.
StripeConfiguration.SetApiKey(YOUR SECRET KEY VAR);
데이터 안전성을 위해 appsettings에서 비밀로 숨겨진 값이어야합니다.
API 키를 설정하지 않으면 구독을 수정하거나 고객을 만들 수 없습니다.
ASP.Net Core 1.0에서 Stripe.Net을 시작으로
https://github.com/jaymedavis/stripe.net 은 훌륭한 출발점입니다.
면도기로 MVC를 사용한다고 가정하면보기 페이지에 몇 가지 사항이 있어야합니다.
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
이 스크립트는 stripe.js를 호출하여 토큰 생성을 처리합니다.
<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>
컨트롤러는 사용자 중 하나를 데리고 스트라이프가 제공 한 고객의 ID 인 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");
}
이 특정 사례에는 컨트롤러에서 결제 정보를받는 것과 같은 몇 가지 추가 기능이 포함되어 있습니다. 이것을 원한다면보기의 양식에 추가하고이를 보유 할 모델을 작성하십시오.