/// <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>
        /// 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));
        }
Esempio n. 3
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));
        }