public void OnPost() { string nonce = Request.Form["nonce"]; IPaymentsApi PaymentsApi = client.PaymentsApi; // 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.Builder() .Amount(500L) .Currency("USD") .Build(); // To learn more about splitting payments with additional recipients, // see the Payments API documentation on our [developer site] // (https://developer.squareup.com/docs/payments-api/overview). CreatePaymentRequest createPaymentRequest = new CreatePaymentRequest.Builder(nonce, uuid, amount) .Note("From Square Sample Csharp App") .Build(); try { CreatePaymentResponse response = PaymentsApi.CreatePayment(createPaymentRequest); this.ResultMessage = "Payment complete! " + response.Payment.Note; } catch (ApiException e) { this.ResultMessage = e.Message; } }
public IActionResult Paid() { Square.Environment environment = this.appSettings.Environment == "sandbox" ? Square.Environment.Sandbox : Square.Environment.Production; // Build base client SquareClient client = new SquareClient.Builder() .Environment(environment) .AccessToken(this.appSettings.AccessToken) .Build(); string nonce = Request.Form["nonce"]; IPaymentsApi PaymentsApi = client.PaymentsApi; // 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 = this.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.Builder() .Amount(500L) .Currency("USD") .Build(); // To learn more about splitting payments with additional recipients, // see the Payments API documentation on our [developer site] // (https://developer.squareup.com/docs/payments-api/overview). CreatePaymentRequest createPaymentRequest = new CreatePaymentRequest.Builder(nonce, uuid, amount) .Note("From Square Visit Cart App") .Build(); try { CreatePaymentResponse response = PaymentsApi.CreatePayment(createPaymentRequest); PaymentResultModel model = new PaymentResultModel { ResultMessage = "Payment complete! " + response.Payment.Note }; return(View(model)); } catch (ApiException ex) { return(View("PaymentError", new ErrorViewModel { Message = ex.Message })); } }
public static CreatePaymentResponse GetChargeResponse(OrderData orderData, string nonce) { // Every payment you process 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 var idempotencyKey = IdempotencyKey(); var appliedtotal = orderData.PurchaseInfo.GetXmlPropertyDouble("genxml/appliedtotal"); var alreadypaid = orderData.PurchaseInfo.GetXmlPropertyDouble("genxml/alreadypaid"); // Square uses the smallest denomination of the Currency being used // ie. when we take 1.00 USD then we will need to convert to 100 var currencyFactor = 100; var currencyCode = _settings.GetXmlProperty("genxml/dropdownlist/currencycode"); //TODO: verify only yen requires an adjustment to the currency factor if (currencyCode == "JPY") { currencyFactor = 1; } var orderTotal = (long)((appliedtotal - alreadypaid) * currencyFactor); var amount = new Money(orderTotal, currencyCode); var note = StoreSettings.Current.SettingsInfo.GetXmlProperty("genxml/textbox/storename") + " Order Number: " + orderData.OrderNumber; var bodyAmountMoney = new Money.Builder() .Amount(orderTotal) .Currency(currencyCode) .Build(); // Creating Payment Request // NOTE: OS Order Id is passed in both the ReferenceId & Note field // because the reference_id is not visible in the square ui but can be // found via the transactions api. var body = new CreatePaymentRequest.Builder( nonce, idempotencyKey, bodyAmountMoney) .Autocomplete(true) .LocationId(_locationId) .ReferenceId(orderData.OrderNumber) .Note(note) .Build(); return(_client.PaymentsApi.CreatePayment(body)); }
public async Task <IActionResult> OnPostAsync() { var request = JObject.Parse(await new StreamReader(Request.Body).ReadToEndAsync()); var token = (String)request["token"]; var PaymentsApi = client.PaymentsApi; // 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(); // Get the currency for the location var retrieveLocationResponse = await client.LocationsApi.RetrieveLocationAsync(locationId : locationId); var currency = retrieveLocationResponse.Location.Currency; // 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. var amount = new Money.Builder() .Amount(100L) .Currency(currency) .Build(); // To learn more about splitting payments with additional recipients, // see the Payments API documentation on our [developer site] // (https://developer.squareup.com/docs/payments-api/overview). var createPaymentRequest = new CreatePaymentRequest.Builder( sourceId: token, idempotencyKey: uuid, amountMoney: amount) .Build(); try { var response = await PaymentsApi.CreatePaymentAsync(createPaymentRequest); return(new JsonResult(new { payment = response.Payment })); } catch (ApiException e) { return(new JsonResult(new { errors = e.Errors })); } }