1
0
mirror of https://github.com/bitwarden/server synced 2025-12-23 11:43:23 +00:00

paymentservice with stripe & braintree implem.

This commit is contained in:
Kyle Spearrin
2017-07-28 00:17:31 -04:00
parent c991d48cbc
commit 2dc9c196c4
13 changed files with 236 additions and 72 deletions

View File

@@ -0,0 +1,70 @@
using System;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using Stripe;
using System.Collections.Generic;
namespace Bit.Core.Services
{
public class StripePaymentService : IPaymentService
{
private const string PremiumPlanId = "premium-annually";
private const string StoragePlanId = "storage-gb-annually";
public StripePaymentService(
GlobalSettings globalSettings)
{
}
public async Task PurchasePremiumAsync(User user, string paymentToken, short additionalStorageGb)
{
var customerService = new StripeCustomerService();
var customer = await customerService.CreateAsync(new StripeCustomerCreateOptions
{
Description = user.Name,
Email = user.Email,
SourceToken = paymentToken
});
var subCreateOptions = new StripeSubscriptionCreateOptions
{
Items = new List<StripeSubscriptionItemOption>(),
Metadata = new Dictionary<string, string>
{
["userId"] = user.Id.ToString()
}
};
subCreateOptions.Items.Add(new StripeSubscriptionItemOption
{
PlanId = PremiumPlanId,
Quantity = 1
});
if(additionalStorageGb > 0)
{
subCreateOptions.Items.Add(new StripeSubscriptionItemOption
{
PlanId = StoragePlanId,
Quantity = additionalStorageGb
});
}
StripeSubscription subscription = null;
try
{
var subscriptionService = new StripeSubscriptionService();
subscription = await subscriptionService.CreateAsync(customer.Id, subCreateOptions);
}
catch(StripeException)
{
await customerService.DeleteAsync(customer.Id);
throw;
}
user.StripeCustomerId = customer.Id;
user.StripeSubscriptionId = subscription.Id;
}
}
}