コード例 #1
0
        public void Can_Get_Customer_From_Service()
        {
            //// Arrange

            //// Act
            var customer = _braintreeCustomerApiService.GetBraintreeCustomer(TestCustomer);

            Assert.NotNull(customer);
        }
        /// <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));
        }