mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
charge braintree customer tool
This commit is contained in:
79
src/Admin/Controllers/ToolsController.cs
Normal file
79
src/Admin/Controllers/ToolsController.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Admin.Models;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bit.Admin.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public class ToolsController : Controller
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
|
||||
public ToolsController(GlobalSettings globalSettings)
|
||||
{
|
||||
_globalSettings = globalSettings;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> ChargeBraintree()
|
||||
{
|
||||
return View(new ChargeBraintreeModel());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> ChargeBraintree(ChargeBraintreeModel model)
|
||||
{
|
||||
if(!ModelState.IsValid)
|
||||
{
|
||||
return View(model);
|
||||
}
|
||||
|
||||
var btGateway = new Braintree.BraintreeGateway
|
||||
{
|
||||
Environment = _globalSettings.Braintree.Production ?
|
||||
Braintree.Environment.PRODUCTION : Braintree.Environment.SANDBOX,
|
||||
MerchantId = _globalSettings.Braintree.MerchantId,
|
||||
PublicKey = _globalSettings.Braintree.PublicKey,
|
||||
PrivateKey = _globalSettings.Braintree.PrivateKey
|
||||
};
|
||||
|
||||
var btObjIdField = model.Id[0] == 'o' ? "organization_id" : "user_id";
|
||||
var btObjId = new Guid(model.Id.Substring(1, 32));
|
||||
|
||||
var transactionResult = await btGateway.Transaction.SaleAsync(
|
||||
new Braintree.TransactionRequest
|
||||
{
|
||||
Amount = model.Amount.Value,
|
||||
CustomerId = model.Id,
|
||||
Options = new Braintree.TransactionOptionsRequest
|
||||
{
|
||||
SubmitForSettlement = true,
|
||||
PayPal = new Braintree.TransactionOptionsPayPalRequest
|
||||
{
|
||||
CustomField = $"{btObjIdField}:{btObjId}"
|
||||
}
|
||||
},
|
||||
CustomFields = new Dictionary<string, string>
|
||||
{
|
||||
[btObjIdField] = btObjId.ToString()
|
||||
}
|
||||
});
|
||||
|
||||
if(!transactionResult.IsSuccess())
|
||||
{
|
||||
ModelState.AddModelError(string.Empty, "Charge failed.");
|
||||
}
|
||||
else
|
||||
{
|
||||
model.TransactionId = transactionResult.Target.Id;
|
||||
model.PayPalTransactionId = transactionResult.Target?.PayPalDetails?.CaptureId;
|
||||
}
|
||||
return View(model);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user