mirror of
https://github.com/bitwarden/server.git
synced 2025-02-17 02:01:53 +01:00
charge braintree customer tool
This commit is contained in:
parent
1b5652fb7c
commit
fb21b19490
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);
|
||||
}
|
||||
}
|
||||
}
|
30
src/Admin/Models/ChargeBraintreeModel.cs
Normal file
30
src/Admin/Models/ChargeBraintreeModel.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Bit.Admin.Models
|
||||
{
|
||||
public class ChargeBraintreeModel : IValidatableObject
|
||||
{
|
||||
[Required]
|
||||
[Display(Name = "Braintree Customer Id")]
|
||||
public string Id { get; set; }
|
||||
[Required]
|
||||
[Display(Name = "Amount")]
|
||||
public decimal? Amount { get; set; }
|
||||
public string TransactionId { get; set; }
|
||||
public string PayPalTransactionId { get; set; }
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if(Id != null)
|
||||
{
|
||||
if(Id.Length != 36 || (Id[0] != 'o' && Id[0] != 'u') ||
|
||||
!Guid.TryParse(Id.Substring(1, 32), out var guid))
|
||||
{
|
||||
yield return new ValidationResult("Customer Id is not a valid format.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -28,3 +28,7 @@ h2 {
|
||||
h3 {
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.validation-summary-valid {
|
||||
display: none;
|
||||
}
|
||||
|
@ -40,6 +40,17 @@
|
||||
</li>
|
||||
@if(!GlobalSettings.SelfHosted)
|
||||
{
|
||||
<li class="nav-item dropdown" active-controller="tools">
|
||||
<a class="nav-link dropdown-toggle" href="#" id="toolsDropdown" role="button"
|
||||
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||
Tools
|
||||
</a>
|
||||
<div class="dropdown-menu" aria-labelledby="toolsDropdown">
|
||||
<a class="dropdown-item" asp-controller="Tools" asp-action="ChargeBraintree">
|
||||
Charge Braintree Customer
|
||||
</a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="nav-item" active-controller="Logs">
|
||||
<a class="nav-link" asp-controller="Logs" asp-action="Index">Logs</a>
|
||||
</li>
|
||||
|
44
src/Admin/Views/Tools/ChargeBraintree.cshtml
Normal file
44
src/Admin/Views/Tools/ChargeBraintree.cshtml
Normal file
@ -0,0 +1,44 @@
|
||||
@model ChargeBraintreeModel
|
||||
@{
|
||||
ViewData["Title"] = "Charge Braintree Customer";
|
||||
}
|
||||
|
||||
<h1>Charge Braintree Customer</h1>
|
||||
|
||||
@if(!string.IsNullOrWhiteSpace(Model.TransactionId))
|
||||
{
|
||||
<div class="alert alert-success" role="alert">
|
||||
<p>Charged customer "@Model.Id" for @Model.Amount.Value.ToString("C").</p>
|
||||
<strong>btTransactionId:</strong> @Model.TransactionId<br />
|
||||
<strong>btPayPalTransactionId:</strong> @Model.PayPalTransactionId
|
||||
</div>
|
||||
<a asp-action="ChargeBraintree" class="btn btn-secondary">Charge Another Customer</a>
|
||||
}
|
||||
else
|
||||
{
|
||||
<form method="post">
|
||||
<div asp-validation-summary="All" class="alert alert-danger"></div>
|
||||
<div class="row">
|
||||
<div class="col-sm">
|
||||
<div class="form-group">
|
||||
<label asp-for="Id"></label>
|
||||
<input type="text" class="form-control" asp-for="Id" required
|
||||
placeholder="ex. u298ccf9240b64f7f8b5da9e0003ba287cpz">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm">
|
||||
<div class="form-group">
|
||||
<label asp-for="Amount"></label>
|
||||
<div class="input-group mb-3">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text">$</span>
|
||||
</div>
|
||||
<input type="number" min="0.01" max="10000.00" step="0.01" class="form-control"
|
||||
asp-for="Amount" required placeholder="ex. 10.00">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary mb-2" title="Search">Charge Customer</button>
|
||||
</form>
|
||||
}
|
Loading…
Reference in New Issue
Block a user