Beispiel #1
0
        public void CreateCustomerTest()
        {
            string authorization = "Basic asdadsa";

            mockRestClient.Expects.One.Method(v => v.Execute(new RestRequest())).With(NMock.Is.TypeOf(typeof(RestRequest))).WillReturn(customerResponse);
            ApiClient apiClient = new ApiClient(mockRestClient.MockObject);

            apiClient.Configuration = null;
            Identifier     identifier     = new Identifier("*****@*****.**", "Customer Reference");
            CreateCustomer createCustomer = new CreateCustomer("create customer test", identifier, null);

            Configuration configuration = new Configuration
            {
                ApiClient      = apiClient,
                Username       = "******",
                Password       = "******",
                AccessToken    = null,
                ApiKey         = null,
                ApiKeyPrefix   = null,
                TempFolderPath = null,
                DateTimeFormat = null,
                Timeout        = 60000,
                UserAgent      = "asdasd"
            };

            instance = new CustomersApi(configuration);

            var response = instance.CreateCustomer(createCustomer, authorization);

            Assert.IsInstanceOf <Customer>(response, "response is Customer");
        }
Beispiel #2
0
        /// <summary>
        /// Create the new customer
        /// </summary>
        /// <param name="customerRequest">Request parameters to create customer</param>
        /// <returns>Customer</returns>
        public Customer CreateCustomer(CreateCustomerRequest customerRequest)
        {
            try
            {
                //create customer API
                var configuration = CreateApiConfiguration();
                var customersApi  = new CustomersApi(configuration);

                //create the new customer
                var createCustomerResponse = customersApi.CreateCustomer(customerRequest);
                if (createCustomerResponse == null)
                {
                    throw new NopException("No service response");
                }

                //check whether there are errors in the service response
                if (createCustomerResponse.Errors?.Any() ?? false)
                {
                    var errorsMessage = string.Join(";", createCustomerResponse.Errors.Select(error => error.ToString()));
                    throw new NopException($"There are errors in the service response. {errorsMessage}");
                }

                return(createCustomerResponse.Customer);
            }
            catch (Exception exception)
            {
                //log full error
                _logger.Error($"Square payment error: {exception.Message}.", exception, _workContext.CurrentCustomer);

                return(null);
            }
        }
Beispiel #3
0
        public void CustomersApi_CreateCustomer()
        {
            CustomersApi api = new CustomersApi(BaseUrl, apiToken, apiKey);

            CreateCustomersRequest req = new CreateCustomersRequest();

            req.BirthDate = DateTime.Today.AddYears(-30);
            req.Email     = "*****@*****.**";
            req.Fullname  = "Teste moip client 4";
            req.OwnId     = "3";
            req.Phone     = new PhoneDto()
            {
                AreaCode    = 11,
                CountryCode = 55,
                Number      = 98985959
            };
            req.ShippingAddress = new AddressDto()
            {
                City         = "Sao Paulo",
                Complement   = "A",
                Country      = "BR",
                District     = "SP",
                State        = "SP",
                Street       = "Sao paullo",
                StreetNumber = "12",
                ZipCode      = "09090170"
            };
            req.TaxDocument = new DocumentDto()
            {
                Number = "95672830013",
                Type   = DocumentType.CPF
            };



            var retorno = api.CreateCustomer(req);

            Assert.IsNotNull(retorno);
            Assert.IsNotNull(retorno.Id);
        }
Beispiel #4
0
        public static TransactionResult ProcessPayment(TransactionRequest request, SquareSettings squareSettings, ILogger logger)
        {
            var order           = request.Order;
            var config          = GetConfiguration(squareSettings);
            var nonce           = request.GetParameterAs <string>("nonce");
            var location        = GetApplicationLocations(squareSettings, logger).FirstOrDefault(x => x.Id == squareSettings.LocationId);
            var billingAddress  = order.BillingAddressSerialized.To <EvenCart.Data.Entity.Addresses.Address>();
            var shippingAddress = order.ShippingAddressSerialized?.To <EvenCart.Data.Entity.Addresses.Address>();
            var customerId      = order.User.GetPropertyValueAs <string>(SquareCustomerIdKey);

            if (customerId.IsNullEmptyOrWhiteSpace())
            {
                var createCustomerRequest  = new CreateCustomerRequest(EmailAddress: order.User.Email, GivenName: order.User.Name);
                var customerApi            = new CustomersApi(config);
                var customerCreateResponse = customerApi.CreateCustomer(createCustomerRequest);
                customerId = customerCreateResponse.Customer.Id;
            }
            var paymentRequest = new CreatePaymentRequest(SourceId: nonce,
                                                          IdempotencyKey: Guid.NewGuid().ToString(),
                                                          LocationId: location?.Id,
                                                          CustomerId: customerId,
                                                          AmountMoney: new Money()
            {
                Amount   = (long)((request.Amount ?? order.OrderTotal) * 100),
                Currency = order.CurrencyCode
            },
                                                          AppFeeMoney: new Money()
            {
                Amount   = (long)(100 * order.PaymentMethodFee),
                Currency = order.CurrencyCode
            },
                                                          BillingAddress: new Address(billingAddress.Address1, billingAddress.Address2,
                                                                                      FirstName: billingAddress.Name, Locality: billingAddress.Landmark,
                                                                                      PostalCode: billingAddress.ZipPostalCode),
                                                          BuyerEmailAddress: order.User.Email
                                                          );

            if (shippingAddress != null)
            {
                paymentRequest.ShippingAddress = new Address(shippingAddress.Address1, shippingAddress.Address2,
                                                             FirstName: shippingAddress.Name, Locality: shippingAddress.Landmark,
                                                             PostalCode: shippingAddress.ZipPostalCode);
            }
            if (squareSettings.AuthorizeOnly)
            {
                paymentRequest.Autocomplete = false; //authorize only
            }
            var paymentsApi       = new PaymentsApi(config);
            var paymentResponse   = paymentsApi.CreatePayment(paymentRequest);
            var transactionResult = new TransactionResult()
            {
                OrderGuid       = order.Guid,
                TransactionGuid = Guid.NewGuid().ToString(),
            };

            if (paymentResponse != null)
            {
                var payment = paymentResponse.Payment;
                transactionResult.Success            = true;
                transactionResult.TransactionAmount  = (decimal)(payment.AmountMoney.Amount ?? 0) / 100;
                transactionResult.ResponseParameters = new Dictionary <string, object>()
                {
                    { "paymentId", payment.Id },
                    { "referenceId", payment.ReferenceId },
                    { "orderId", payment.OrderId },
                    { "cardDetails", payment.CardDetails }
                };
                if (payment.Status == "APPROVED")
                {
                    transactionResult.NewStatus = PaymentStatus.Authorized;
                }
                else if (payment.Status == "COMPLETED")
                {
                    transactionResult.NewStatus = PaymentStatus.Complete;
                }
                else
                {
                    transactionResult.NewStatus = PaymentStatus.Failed;
                    var errors = string.Join(",", paymentResponse.Errors);
                    logger.Log <TransactionResult>(LogLevel.Warning, "The payment for Order#" + order.Id + " by square failed." + errors);
                    transactionResult.Exception = new Exception("An error occurred while processing payment. Error Details: " + errors);
                    transactionResult.Success   = false;
                }
            }
            else
            {
                logger.Log <TransactionResult>(LogLevel.Warning, "The payment for Order#" + order.Id + " by square failed. No response received.");
                transactionResult.Success   = false;
                transactionResult.Exception = new Exception("An error occurred while processing payment");
            }

            return(transactionResult);
        }