public async Task CreatePayment() { var logger = ServiceScope.ServiceProvider.GetService <ILogger <SquareTest> >(); var squareCustomerReference = CreateSquareCustomerReference.FromTimestamp(GetUniqueNow()); var squareCustomerId = await SquareMicroService.AllocateSquareCustomerAsync(squareCustomerReference); logger.LogInformation($"Square customer ID = {squareCustomerId}"); var squarePaymentReference = CreateSquarePaymentReference.FromTimestamp(GetUniqueNow()); var squarePaymentId = await SquareMicroService.AllocateSquarePaymentAsync(squarePaymentReference, squareCustomerId); logger.LogInformation($"Square payment ID = {squarePaymentId}"); var squareWebPaymentTransactionId = await SquareMicroService.CreateSquareWebPaymentRequestAsync(squarePaymentId, 100m, "cnon:card-nonce-ok"); var paymentResponse = await SquareMicroService.ProcessWebPaymentRequestAsync(squareWebPaymentTransactionId); logger.LogInformation($"Square payment response = {paymentResponse}"); //var squareRefundTransactionId = await SquareMicroService.CreateSquareRefundTransactionAsync(squarePaymentId, 25m); //var refundResponse = await SquareMicroService.ProcessSquarePaymentTransactionAsync(squareRefundTransactionId); //logger.LogInformation($"Square refund response = {refundResponse}"); _ = await SquareMicroService.ProcessEventsAsync(); }
public async Task <UCart_CreateSquarePaymentResponse> CreateSquarePaymentAsync(string userId, decimal paymentAmount, string nonce) { using var log = BeginFunction(nameof(CartUserService), nameof(CreateSquarePaymentAsync), userId, paymentAmount, nonce); try { var ordererReference = CreateOrdererReference.FromUserId(userId); var ordererId = await OrderMicroService.AllocateOrdererAsync(ordererReference); var uOrder = await GetCartOrderAsync(userId); if (uOrder == null) { throw new InvalidOperationException("Shopping cart is empty."); } var squareCustomerReference = CreateSquareCustomerReference.FromUserId(userId); var squareCustomerId = await SquareMicroService.AllocateSquareCustomerAsync(squareCustomerReference); var squarePaymentReference = CreateSquarePaymentReference.FromOrderId(uOrder.MOrder.OrderId); var squarePaymentId = await SquareMicroService.AllocateSquarePaymentAsync(squarePaymentReference, squareCustomerId); var squareWebPaymentRequestId = await SquareMicroService.CreateSquareWebPaymentRequestAsync(squarePaymentId, paymentAmount, nonce); var mProcessWebPaymentTransactionResponse = await SquareMicroService.ProcessWebPaymentRequestAsync(squareWebPaymentRequestId); IList <Cart_CreateSquarePaymentResponseErrorData> errors; if (mProcessWebPaymentTransactionResponse.Errors == null) { errors = null; _ = await OrderMicroService.SubmitCartAsync(ordererId); } else { errors = new List <Cart_CreateSquarePaymentResponseErrorData>(); foreach (var mError in mProcessWebPaymentTransactionResponse.Errors) { errors.Add(new Cart_CreateSquarePaymentResponseErrorData() { Category = mError.Category, Code = mError.Code, Detail = mError.Detail, Field = mError.Field }); } } var result = new UCart_CreateSquarePaymentResponse() { Errors = errors }; _ = await EventProcessorMicroService.ProcessPendingEvents().ConfigureAwait(false); log.Result(result); return(result); } catch (Exception ex) { log.Exception(ex); throw; } }