public static string Charge(string nonce)
        {
            TransactionsApi transactionsApi = new TransactionsApi();
            // Every payment you process with the SDK must have a unique idempotency key.
            // If you're unsure whether a particular payment succeeded, you can reattempt
            // it with the same idempotency key without worrying about double charging
            // the buyer.
            string uuid = NewIdempotencyKey();

            // Monetary amounts are specified in the smallest unit of the applicable currency.
            // This amount is in cents. It's also hard-coded for $1.00,
            // which isn't very useful.
            Money amount = new Money(100, Money.CurrencyEnum.USD);

            ChargeRequest body = new ChargeRequest(AmountMoney: amount, IdempotencyKey: uuid, CardNonce: nonce);

            try
            {
                var response = transactionsApi.Charge(LocationId, body);
                return("Transaction complete\n" + response.ToJson());
            }
            catch (ApiException e)
            {
                return(e.Message);
            }
        }
        public void OnPost()
        {
            string          nonce           = Request.Form["nonce"];
            TransactionsApi transactionsApi = new TransactionsApi();
            // Every payment you process with the SDK must have a unique idempotency key.
            // If you're unsure whether a particular payment succeeded, you can reattempt
            // it with the same idempotency key without worrying about double charging
            // the buyer.
            string uuid = NewIdempotencyKey();

            // Monetary amounts are specified in the smallest unit of the applicable currency.
            // This amount is in cents. It's also hard-coded for $1.00,
            // which isn't very useful.
            Money amount = new Money(100, Money.CurrencyEnum.USD);

            // To learn more about splitting transactions with additional recipients,
            // see the Transactions API documentation on our [developer site]
            // (https://docs.connect.squareup.com/payments/transactions/overview#mpt-overview).
            ChargeRequest body = new ChargeRequest(AmountMoney: amount, IdempotencyKey: uuid, CardNonce: nonce);

            try
            {
                var response = transactionsApi.Charge(LocationId, body);
                this.ResultMessage = "Transaction complete!   " + response.ToJson();
            }
            catch (ApiException e)
            {
                this.ResultMessage = e.Message;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Charge transaction
        /// </summary>
        /// <param name="chargeRequest">Request parameters to charge transaction</param>
        /// <returns>Transaction and/or errors if exist</returns>
        public PaymentResponse <Transaction> Charge(ExtendedChargeRequest chargeRequest)
        {
            try
            {
                //try to get the selected location
                var selectedLocation = GetActiveLocations().FirstOrDefault(location => location.Id.Equals(_squarePaymentSettings.LocationId));
                if (selectedLocation == null)
                {
                    throw new NopException("Location is a required parameter for payment requests");
                }

                //create transaction API
                var configuration   = CreateApiConfiguration();
                var transactionsApi = new TransactionsApi(configuration);

                //create charge transaction
                var chargeResponse = transactionsApi.Charge(selectedLocation.Id, chargeRequest);
                if (chargeResponse == null)
                {
                    throw new NopException("No service response");
                }

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

                return(new PaymentResponse <Transaction> {
                    ResponseValue = chargeResponse.Transaction
                });
            }
            catch (Exception exception)
            {
                //log full error
                var errorMessage = exception.Message;
                _logger.Error($"Square payment error: {errorMessage}.", exception, _workContext.CurrentCustomer);

                if (exception is ApiException apiException)
                {
                    //try to get error details
                    var response = JsonConvert.DeserializeObject <ChargeResponse>(apiException.ErrorContent) as ChargeResponse;
                    if (response?.Errors?.Any() ?? false)
                    {
                        errorMessage = string.Join(";", response.Errors.Select(error => error.Detail));
                    }
                }

                return(new PaymentResponse <Transaction> {
                    Error = errorMessage
                });
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Charge transaction
        /// </summary>
        /// <param name="chargeRequest">Request parameters to charge transaction</param>
        /// <returns>Transaction</returns>
        public Transaction Charge(ChargeRequest chargeRequest)
        {
            try
            {
                //try to get the selected location
                var selectedLocation = GetActiveLocations().FirstOrDefault(location => location.Id.Equals(_squarePaymentSettings.LocationId));
                if (selectedLocation == null)
                    throw new NopException("Location is a required parameter for payment requests");

                //create transaction API
                var configuration = CreateApiConfiguration();
                var transactionsApi = new TransactionsApi(configuration);

                //create charge transaction
                var chargeResponse = transactionsApi.Charge(selectedLocation.Id, chargeRequest);
                if (chargeResponse == null)
                    throw new NopException("No service response");

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

                return chargeResponse.Transaction;
            }
            catch (Exception exception)
            {
                var errorMessage = $"Square payment error: {exception.Message}.";
                if (exception is ApiException apiException)
                    errorMessage = $"{errorMessage} Details: {apiException.ErrorCode} - {apiException.ErrorContent}";

                //log errors
                _logger.Error(errorMessage, exception, _workContext.CurrentCustomer);

                return null;
            }
        }
Exemplo n.º 5
0
        public string Charge(ChargeRequest charge)
        {
            var response = transactionApi.Charge(locationId, charge);

            return("Transaction complete\n" + response.ToJson());
        }