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,72 @@
using System;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using Braintree;
namespace Bit.Core.Services
{
public class BraintreePaymentService : IPaymentService
{
private readonly BraintreeGateway _gateway;
public BraintreePaymentService(
GlobalSettings globalSettings)
{
_gateway = new BraintreeGateway
{
Environment = globalSettings.Braintree.Production ?
Braintree.Environment.PRODUCTION : Braintree.Environment.SANDBOX,
MerchantId = globalSettings.Braintree.MerchantId,
PublicKey = globalSettings.Braintree.PublicKey,
PrivateKey = globalSettings.Braintree.PrivateKey
};
}
public async Task PurchasePremiumAsync(User user, string paymentToken, short additionalStorageGb)
{
var customerResult = await _gateway.Customer.CreateAsync(new CustomerRequest
{
PaymentMethodNonce = paymentToken,
Email = user.Email
});
if(!customerResult.IsSuccess())
{
// error, throw something
}
var subId = "u" + user.Id.ToString("N").ToLower() +
Utilities.CoreHelpers.RandomString(3, upper: false, numeric: false);
var subRequest = new SubscriptionRequest
{
Id = subId,
PaymentMethodToken = paymentToken,
PlanId = "premium-annually"
};
if(additionalStorageGb > 0)
{
subRequest.AddOns.Add = new AddAddOnRequest[]
{
new AddAddOnRequest
{
InheritedFromId = "storage-gb-annually",
Quantity = additionalStorageGb
}
};
}
var subResult = await _gateway.Subscription.CreateAsync(subRequest);
if(!subResult.IsSuccess())
{
await _gateway.Customer.DeleteAsync(customerResult.Target.Id);
// error, throw something
}
user.StripeCustomerId = customerResult.Target.Id;
user.StripeSubscriptionId = subResult.Target.Id;
}
}
}