/// <summary> /// Processes the payment. /// </summary> /// <param name="invoice"> /// The invoice. /// </param> /// <param name="option"> /// The option. /// </param> /// <param name="amount"> /// The amount. /// </param> /// <param name="token"> /// The token. /// </param> /// <param name="email"> /// The email. /// </param> /// <returns> /// The <see cref="IPaymentResult"/>. /// </returns> protected override IPaymentResult ProcessPayment(IInvoice invoice, TransactionOption option, decimal amount, string token, string email = "") { var payment = this.GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, amount, this.PaymentMethod.Key); payment.CustomerKey = invoice.CustomerKey; payment.Authorized = false; payment.Collected = false; payment.PaymentMethodName = this.BackOfficePaymentMethodName; payment.ExtendedData.SetValue(Constants.Braintree.ProcessorArguments.PaymentMethodNonce, token); var result = this.BraintreeApiService.Transaction.VaultSale(invoice, token, option); if (result.IsSuccess()) { payment.ExtendedData.SetBraintreeTransaction(result.Target); if (option == TransactionOption.Authorize) payment.Authorized = true; if (option == TransactionOption.SubmitForSettlement) { payment.Authorized = true; payment.Collected = true; } return new PaymentResult(Attempt<IPayment>.Succeed(payment), invoice, true); } var error = new BraintreeApiException(result.Errors, result.Message); return new PaymentResult(Attempt<IPayment>.Fail(payment, error), invoice, false); }
/// <summary> /// Performs the actual work of capturing the payment. /// </summary> /// <param name="invoice"> /// The invoice. /// </param> /// <param name="payment"> /// The payment. /// </param> /// <param name="amount"> /// The amount. /// </param> /// <param name="args"> /// The args. /// </param> /// <returns> /// The <see cref="IPaymentResult"/>. /// </returns> protected override IPaymentResult PerformCapturePayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args) { var transaction = payment.ExtendedData.GetBraintreeTransaction(); if (transaction == null) { var error = new NullReferenceException("Braintree transaction could not be found and/or deserialized from payment extended data collection"); return new PaymentResult(Attempt<IPayment>.Fail(payment, error), invoice, false); } var attempt = this.BraintreeApiService.Transaction.SubmitForSettlement(transaction.Id); if (!attempt.IsSuccess()) { var error = new BraintreeApiException(attempt.Errors, attempt.Message); this.GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, error.Message, 0); return new PaymentResult(Attempt<IPayment>.Fail(payment, error), invoice, false); } payment.Collected = true; this.GatewayProviderService.Save(payment); var transactionReference = payment.ExtendedData.GetBraintreeTransaction(); this.GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "Payment submitted for settlement to card " + transactionReference.MaskedNumber, amount); return new PaymentResult(Attempt<IPayment>.Succeed(payment), invoice, true); }
/// <summary> /// Performs the actual work of performing the refund. /// </summary> /// <param name="invoice"> /// The invoice. /// </param> /// <param name="payment"> /// The payment. /// </param> /// <param name="amount"> /// The amount. /// </param> /// <param name="args"> /// The args. /// </param> /// <returns> /// The <see cref="IPaymentResult"/>. /// </returns> protected override IPaymentResult PerformRefundPayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args) { var transaction = payment.ExtendedData.GetBraintreeTransaction(); if (transaction == null) { var error = new NullReferenceException("Braintree transaction could not be found and/or deserialized from payment extended data collection"); return new PaymentResult(Attempt<IPayment>.Fail(payment, error), invoice, false); } var attempt = this.BraintreeApiService.Transaction.Refund(transaction.Id, amount); if (!attempt.IsSuccess()) { var error = new BraintreeApiException(attempt.Errors, attempt.Message); this.GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Refund, error.Message, 0); return new PaymentResult(Attempt<IPayment>.Fail(payment, error), invoice, false); } foreach (var applied in payment.AppliedPayments()) { applied.TransactionType = AppliedPaymentType.Refund; applied.Amount = 0; applied.Description += " - Refunded"; this.GatewayProviderService.Save(applied); } payment.Amount = payment.Amount - amount; if (payment.Amount != 0) { this.GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "To show partial payment remaining after refund", payment.Amount); } this.GatewayProviderService.Save(payment); return new PaymentResult(Attempt<IPayment>.Succeed(payment), invoice, false); }
/// <summary> /// The update. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="paymentMethodNonce">The "nonce-from-the-client"</param> /// <param name="billingAddress">The customer billing address</param> /// <param name="shippinggAddress">The shipping address</param> /// <returns> /// The <see cref="Customer"/>. /// </returns> public Attempt<Customer> Update(ICustomer customer, string paymentMethodNonce = "", IAddress billingAddress = null, IAddress shippinggAddress = null) { if (!this.Exists(customer)) return Attempt<Customer>.Fail(new NullReferenceException("Could not finde matching Braintree customer.")); LogHelper.Info<BraintreeTransactionApiService>(string.Format("Braintree Update customer attempt for CustomerKey: {0}, name: {1}", customer.Key, customer.FullName)); var request = this.RequestFactory.CreateCustomerRequest(customer, paymentMethodNonce, billingAddress, true); Updating.RaiseEvent(new SaveEventArgs<CustomerRequest>(request), this); // attempt the API call var attempt = this.TryGetApiResult(() => this.BraintreeGateway.Customer.Update(customer.Key.ToString(), request)); if (!attempt.Success) return Attempt<Customer>.Fail(attempt.Exception); var result = attempt.Result; if (result.IsSuccess()) { var cacheKey = this.MakeCustomerCacheKey(customer); this.RuntimeCache.ClearCacheItem(cacheKey); Updated.RaiseEvent(new SaveEventArgs<Customer>(result.Target), this); return Attempt<Customer>.Succeed((Customer)this.RuntimeCache.GetCacheItem(cacheKey, () => result.Target)); } var error = new BraintreeApiException(result.Errors); LogHelper.Error<BraintreeCustomerApiService>("Braintree API Customer Create return a failure", error); return Attempt<Customer>.Fail(error); }
/// <summary> /// Creates a Braintree <see cref="Customer"/> from a Merchello <see cref="ICustomer"/> /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="paymentMethodNonce"> /// The "nonce-from-the-client" /// </param> /// <param name="billingAddress"> /// The billing address /// </param> /// <param name="shippingAddress"> /// The shipping Address. /// </param> /// <returns> /// The <see cref="Attempt{Customer}"/>. /// </returns> public Attempt<Customer> Create(ICustomer customer, string paymentMethodNonce = "", IAddress billingAddress = null, IAddress shippingAddress = null) { if (this.Exists(customer)) return Attempt.Succeed(this.GetBraintreeCustomer(customer)); var request = this.RequestFactory.CreateCustomerRequest(customer, paymentMethodNonce, billingAddress); Creating.RaiseEvent(new Core.Events.NewEventArgs<CustomerRequest>(request), this); // attempt the API call LogHelper.Info<BraintreeTransactionApiService>(string.Format("Braintree Create customer attempt for CustomerKey: {0}, name: {1}", customer.Key, customer.FullName)); var attempt = this.TryGetApiResult(() => this.BraintreeGateway.Customer.Create(request)); if (!attempt.Success) return Attempt<Customer>.Fail(attempt.Exception); var result = attempt.Result; if (result.IsSuccess()) { Created.RaiseEvent(new Core.Events.NewEventArgs<Customer>(result.Target), this); return Attempt.Succeed((Customer)this.RuntimeCache.GetCacheItem(this.MakeCustomerCacheKey(customer), () => result.Target)); } var error = new BraintreeApiException(result.Errors); LogHelper.Error<BraintreeCustomerApiService>("Braintree API Customer Create return a failure", error); return Attempt<Customer>.Fail(error); }
/// <summary> /// Updates a payment method /// </summary> /// <param name="token">The payment method token</param> /// <param name="request"> /// The request. /// </param> /// <returns> /// The <see cref="Attempt"/>. /// </returns> public Attempt<PaymentMethod> Update(string token, PaymentMethodRequest request) { Mandate.ParameterNotNull(request, "request"); Updating.RaiseEvent(new SaveEventArgs<PaymentMethodRequest>(request), this); var attempt = this.TryGetApiResult(() => this.BraintreeGateway.PaymentMethod.Update(token, request)); if (!attempt.Success) return Attempt<PaymentMethod>.Fail(attempt.Exception); var result = attempt.Result; if (result.IsSuccess()) { var cacheKey = this.MakePaymentMethodCacheKey(token); this.RuntimeCache.ClearCacheItem(cacheKey); Updated.RaiseEvent(new SaveEventArgs<PaymentMethod>(result.Target), this); return Attempt<PaymentMethod>.Succeed((PaymentMethod)this.RuntimeCache.GetCacheItem(cacheKey, () => result.Target)); } var error = new BraintreeApiException(result.Errors, result.Message); LogHelper.Error<BraintreePaymentMethodApiService>("Failed to update payment method", error); return Attempt<PaymentMethod>.Fail(error); }
/// <summary> /// Adds a credit card to an existing customer. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="paymentMethodNonce"> /// The payment method nonce. /// </param> /// <param name="token"> /// The token. /// </param> /// <param name="billingAddress"> /// The billing address. /// </param> /// <param name="isDefault"> /// A value indicating whether or not this payment method should become the default payment method. /// </param> /// <returns> /// The <see cref="Attempt{PaymentMethod}"/> indicating whether the payment method creation was successful. /// </returns> public Attempt<PaymentMethod> Create(ICustomer customer, string paymentMethodNonce, string token, IAddress billingAddress = null, bool isDefault = true) { //// Asserts the customer exists or creates one in BrainTree if it does not exist var btc = this._braintreeCustomerApiService.GetBraintreeCustomer(customer); var request = this.RequestFactory.CreatePaymentMethodRequest(customer, paymentMethodNonce); if (!string.IsNullOrEmpty(token)) request.Token = token; if (billingAddress != null) request.BillingAddress = this.RequestFactory.CreatePaymentMethodAddressRequest(billingAddress); Creating.RaiseEvent(new Core.Events.NewEventArgs<PaymentMethodRequest>(request), this); var attempt = this.TryGetApiResult(() => this.BraintreeGateway.PaymentMethod.Create(request)); if (!attempt.Success) return Attempt<PaymentMethod>.Fail(attempt.Exception); var result = attempt.Result; if (result.IsSuccess()) { var cacheKey = this.MakePaymentMethodCacheKey(token); this.RuntimeCache.ClearCacheItem(cacheKey); Created.RaiseEvent(new Core.Events.NewEventArgs<PaymentMethod>(result.Target), this); return Attempt<PaymentMethod>.Succeed((PaymentMethod)this.RuntimeCache.GetCacheItem(cacheKey, () => result.Target)); } var error = new BraintreeApiException(result.Errors, result.Message); LogHelper.Error<BraintreeCustomerApiService>("Failed to add a credit card to a customer", error); return Attempt<PaymentMethod>.Fail(error); }
/// <summary> /// Cancels an existing subscription /// </summary> /// <param name="subscriptionId"> /// The subscription id. /// </param> /// <returns> /// A value indicating whether or not the cancellation was successful. /// </returns> public bool Cancel(string subscriptionId) { LogHelper.Info<BraintreeTransactionApiService>(string.Format("Braintree Cancel subscription attempt SubscriptionId: {0}", subscriptionId)); var attempt = this.TryGetApiResult(() => this.BraintreeGateway.Subscription.Cancel(subscriptionId)); if (!attempt.Success) return false; var result = attempt.Result; if (result.IsSuccess()) { var cacheKey = this.MakeSubscriptionCacheKey(subscriptionId); this.RuntimeCache.ClearCacheItem(cacheKey); return true; } var error = new BraintreeApiException(result.Errors, result.Message); LogHelper.Error<BraintreeSubscriptionApiService>("Failed to cancel a subscription", error); return false; }
/// <summary> /// Updates an existing subscription /// </summary> /// <param name="request"> /// The request. /// </param> /// <returns> /// The <see cref="Attempt"/>. /// </returns> public Attempt<Subscription> Update(SubscriptionRequest request) { Updating.RaiseEvent(new SaveEventArgs<SubscriptionRequest>(request), this); LogHelper.Info<BraintreeTransactionApiService>(string.Format("Braintree Update subscription attempt PlanId: {0}", request.PlanId)); var attempt = this.TryGetApiResult(() => this.BraintreeGateway.Subscription.Update(request.Id, request)); if (!attempt.Success) return Attempt<Subscription>.Fail(attempt.Exception); var result = attempt.Result; if (result.IsSuccess()) { Updated.RaiseEvent(new SaveEventArgs<Subscription>(result.Target), this); var cacheKey = this.MakeSubscriptionCacheKey(request.Id); this.RuntimeCache.ClearCacheItem(cacheKey); return Attempt<Subscription>.Succeed(result.Target); } var error = new BraintreeApiException(result.Errors, result.Message); LogHelper.Error<BraintreeSubscriptionApiService>("Failed to create a subscription", error); return Attempt<Subscription>.Fail(error); }
/// <summary> /// Creates a <see cref="Subscription"/>. /// </summary> /// <param name="request"> /// The request. /// </param> /// <returns> /// The <see cref="Attempt{Subscription}"/>. /// </returns> public Attempt<Subscription> Create(SubscriptionRequest request) { Creating.RaiseEvent(new Core.Events.NewEventArgs<SubscriptionRequest>(request), this); LogHelper.Info<BraintreeTransactionApiService>(string.Format("Braintree create subscription attempt PlanID: {0}, Price: {1}", request.PlanId, request.Price)); var attempt = this.TryGetApiResult(() => this.BraintreeGateway.Subscription.Create(request)); if (!attempt.Success) return Attempt<Subscription>.Fail(attempt.Exception); var result = attempt.Result; if (result.IsSuccess()) { Created.RaiseEvent(new Core.Events.NewEventArgs<Subscription>(result.Target), this); return Attempt<Subscription>.Succeed(result.Target); } var error = new BraintreeApiException(result.Errors, result.Message); LogHelper.Error<BraintreeSubscriptionApiService>("Failed to create a subscription", error); return Attempt<Subscription>.Fail(error); }