/// <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);

            var attempt = TryGetApiResult(() => 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 = MakeSubscriptionCacheKey(request.Id);
                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));
        }
Example #2
0
        /// <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 = TryGetApiResult(() => BraintreeGateway.PaymentMethod.Update(token, request));

            if (!attempt.Success)
            {
                return(Attempt <PaymentMethod> .Fail(attempt.Exception));
            }

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                var cacheKey = MakePaymentMethodCacheKey(token);

                RuntimeCache.ClearCacheItem(cacheKey);

                Updated.RaiseEvent(new SaveEventArgs <PaymentMethod>(result.Target), this);

                return(Attempt <PaymentMethod> .Succeed((PaymentMethod)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));
        }
Example #3
0
        /// <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 (Exists(customer))
            {
                return(Attempt.Succeed(GetBraintreeCustomer(customer)));
            }

            var request = RequestFactory.CreateCustomerRequest(customer, paymentMethodNonce, billingAddress);

            Creating.RaiseEvent(new Core.Events.NewEventArgs <CustomerRequest>(request), this);

            // attempt the API call
            var attempt = TryGetApiResult(() => 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)RuntimeCache.GetCacheItem(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));
        }
Example #4
0
        /// <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, TimeSpan.FromHours(6))));
            }

            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 <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);

            var attempt = TryGetApiResult(() => 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));
        }
Example #6
0
        /// <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));
        }
Example #7
0
        /// <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 = BraintreeApiService.Transaction.Refund(transaction.Id, amount);

            if (!attempt.IsSuccess())
            {
                var error = new BraintreeApiException(attempt.Errors, attempt.Message);
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Refund, error.Message, 0);
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, error), invoice, false));
            }

            payment.Amount = payment.Amount - amount;

            if (payment.Amount != 0)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "To show partial payment remaining after refund", payment.Amount);
            }

            GatewayProviderService.Save(payment);

            return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, false));
        }
Example #8
0
        /// <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 = GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, amount, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = "Braintree Vault Transaction";
            payment.ExtendedData.SetValue(Braintree.Constants.ProcessorArguments.PaymentMethodNonce, token);

            var result = 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));
        }
Example #9
0
        /// <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);
        }
Example #10
0
        /// <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));
        }
        /// <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 = _braintreeCustomerApiService.GetBraintreeCustomer(customer);

            var request = RequestFactory.CreatePaymentMethodRequest(customer, paymentMethodNonce);

            if (!string.IsNullOrEmpty(token))
            {
                request.Token = token;
            }

            if (billingAddress != null)
            {
                request.BillingAddress = RequestFactory.CreatePaymentMethodAddressRequest(billingAddress);
            }


            Creating.RaiseEvent(new Core.Events.NewEventArgs <PaymentMethodRequest>(request), this);

            var attempt = TryGetApiResult(() => BraintreeGateway.PaymentMethod.Create(request));

            if (!attempt.Success)
            {
                return(Attempt <PaymentMethod> .Fail(attempt.Exception));
            }

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                var cacheKey = MakePaymentMethodCacheKey(token);
                RuntimeCache.ClearCacheItem(cacheKey);

                var customerCacheKey = MakeCustomerCacheKey(customer);
                RuntimeCache.ClearCacheItem(customerCacheKey);

                Created.RaiseEvent(new Core.Events.NewEventArgs <PaymentMethod>(result.Target), this);

                return(Attempt <PaymentMethod> .Succeed((PaymentMethod)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));
        }
Example #12
0
        protected override IPaymentResult ProcessPayment(IInvoice invoice, TransactionOption option, decimal amount, string paymentMethodNonce)
        {
            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, amount, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = "Braintree Transaction";
            payment.ExtendedData.SetValue(Braintree.Constants.ProcessorArguments.PaymentMethodNonce, paymentMethodNonce);

            var merchCustomer = invoice.Customer();

            if (merchCustomer == null)
            {
                var customerError = new NullReferenceException("A customer is not associated with the invoice.  Braintree vault transactions require a customer reference.");
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, customerError), invoice, false));
            }

            var result = BraintreeApiService.Transaction.Sale(invoice, paymentMethodNonce, merchCustomer, invoice.GetBillingAddress(), 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));
        }
Example #13
0
        /// <summary>
        /// Processes a payment against the Braintree API using the BraintreeApiService.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="option">
        /// The option.
        /// </param>
        /// <param name="amount">
        /// The amount.
        /// </param>
        /// <param name="token">
        /// The payment method nonce.
        /// </param>
        /// <param name="email">
        /// The email.
        /// </param>
        /// <param name="merchantAccountId"></param>
        /// <returns>
        /// The <see cref="IPaymentResult"/>.
        /// </returns>
        /// <remarks>
        /// This converts the <see cref="Result{T}"/> into Merchello <see cref="IPaymentResult"/>
        /// </remarks>
        protected override IPaymentResult ProcessPayment(IInvoice invoice, TransactionOption option, decimal amount, string token, string email = "", string merchantAccountId = "")
        {
            var payment = this.GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, amount, this.PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = "Braintree PayPal One Time Transaction";
            payment.ExtendedData.SetValue(Constants.Braintree.ProcessorArguments.PaymentMethodNonce, token);

            var result = this.BraintreeApiService.Transaction.Sale(invoice, amount, token, option: option, email: email, merchantAccountId: merchantAccountId);

            if (result.IsSuccess())
            {
                payment.ExtendedData.SetBraintreeTransaction(result.Target);

                // AVS and CVV data
                payment.ExtendedData.SetAvsCvvData(result.Target);

                // Set the transaction ID as an extended data item
                payment.ExtendedData.SetValue(Core.Constants.ExtendedDataKeys.TransactionId, result.Target.Id);

                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>
        /// 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)
        {
            var attempt = TryGetApiResult(() => BraintreeGateway.Subscription.Cancel(subscriptionId));

            if (!attempt.Success)
            {
                return(false);
            }

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                return(true);
            }

            var error = new BraintreeApiException(result.Errors, result.Message);

            LogHelper.Error <BraintreeSubscriptionApiService>("Failed to cancel a subscription", error);

            return(false);
        }
Example #15
0
        /// <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, TimeSpan.FromHours(6))));
            }

            var error = new BraintreeApiException(result.Errors);

            LogHelper.Error <BraintreeCustomerApiService>("Braintree API Customer Create return a failure", error);

            return(Attempt <Customer> .Fail(error));
        }
        /// <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."));

            var request = RequestFactory.CreateCustomerRequest(customer, paymentMethodNonce, billingAddress, true);

            Updating.RaiseEvent(new SaveEventArgs<CustomerRequest>(request), this);

            // attempt the API call
            var attempt = TryGetApiResult(() => 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 = MakeCustomerCacheKey(customer);
                RuntimeCache.ClearCacheItem(cacheKey);

                Updated.RaiseEvent(new SaveEventArgs<Customer>(result.Target), this);

                return Attempt<Customer>.Succeed((Customer)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>
        /// 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 = TryGetApiResult(() => BraintreeGateway.PaymentMethod.Update(token, request));

            if (!attempt.Success) return Attempt<PaymentMethod>.Fail(attempt.Exception);

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                var cacheKey = MakePaymentMethodCacheKey(token);

                RuntimeCache.ClearCacheItem(cacheKey);

                Updated.RaiseEvent(new SaveEventArgs<PaymentMethod>(result.Target), this);

                return Attempt<PaymentMethod>.Succeed((PaymentMethod)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 = _braintreeCustomerApiService.GetBraintreeCustomer(customer);

            var request = RequestFactory.CreatePaymentMethodRequest(customer, paymentMethodNonce);

            if (!string.IsNullOrEmpty(token)) request.Token = token;

            if (billingAddress != null) request.BillingAddress = RequestFactory.CreatePaymentMethodAddressRequest(billingAddress);

            Creating.RaiseEvent(new Core.Events.NewEventArgs<PaymentMethodRequest>(request), this);

            var attempt = TryGetApiResult(() => BraintreeGateway.PaymentMethod.Create(request));

            if (!attempt.Success) return Attempt<PaymentMethod>.Fail(attempt.Exception);

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                var cacheKey = MakePaymentMethodCacheKey(token);

                RuntimeCache.ClearCacheItem(cacheKey);

                Created.RaiseEvent(new Core.Events.NewEventArgs<PaymentMethod>(result.Target), this);

                return Attempt<PaymentMethod>.Succeed((PaymentMethod)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>
        /// 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 (Exists(customer)) return Attempt.Succeed(GetBraintreeCustomer(customer));

            var request = RequestFactory.CreateCustomerRequest(customer, paymentMethodNonce, billingAddress);

            Creating.RaiseEvent(new Core.Events.NewEventArgs<CustomerRequest>(request), this);

            // attempt the API call
            var attempt = TryGetApiResult(() => 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)RuntimeCache.GetCacheItem(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 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);

            var attempt = TryGetApiResult(() => 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);

                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>
        /// 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)
        {
            var attempt = TryGetApiResult(() => BraintreeGateway.Subscription.Cancel(subscriptionId));

            if (!attempt.Success) return false;

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                return true;
            }

            var error = new BraintreeApiException(result.Errors, result.Message);

            LogHelper.Error<BraintreeSubscriptionApiService>("Failed to cancel a subscription", error);

            return false;
        }