Пример #1
0
        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();
        }
Пример #2
0
        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;
            }
        }
Пример #3
0
        public async Task <UCart_Cart> GetCartAsync(string userId)
        {
            using var log = BeginFunction(nameof(CartUserService), nameof(GetCartAsync), userId);
            try
            {
                // HACK: Review logic
                //AssertIsEndUser(userId);
                //await Assert(SecurityPolicy.IsAuthorized, userId).ConfigureAwait(false);

                var ordererReference = CreateOrdererReference.FromUserId(userId);
                var ordererId        = await OrderMicroService.AllocateOrdererAsync(ordererReference);

                var mOrder = await OrderMicroService.GetCartOrderAsync(ordererId).ConfigureAwait(false);

                if (mOrder == null)
                {
                    return(null);
                }

                var mProjectOrder = OrderUserService.Create.UOrder_Order(mOrder, null);

                var squareCustomerReference = CreateSquareCustomerReference.FromUserId(userId);
                var squareCustomerId        = await SquareMicroService.AllocateSquareCustomerAsync(squareCustomerReference);

                var squarePaymentReference = CreateSquarePaymentReference.FromOrderId(mOrder.OrderId);
                var squarePaymentId        = await SquareMicroService.AllocateSquarePaymentAsync(squarePaymentReference, squareCustomerId);

                var mSquarePayment = await SquareMicroService.GetSquarePaymentAsync(squarePaymentId);

                var mWebPaymentRequest = mSquarePayment.WebPaymentRequests.Where(r =>
                                                                                 r.WebRequestTypeCode == SquarePaymentWebRequestTypeCodes.Payment &&
                                                                                 r.ProcessingStatusCode != SquareProcessingStatusCodes.Cancelled).SingleOrDefault();

                Cart_PaymentStatus paymentStatus;
                if (mWebPaymentRequest == null)
                {
                    paymentStatus = Cart_PaymentStatus.Required;
                }
                else
                {
                    switch (mWebPaymentRequest.ProcessingStatusCode)
                    {
                    case SquareProcessingStatusCodes.Rejected:
                        paymentStatus = Cart_PaymentStatus.Required;
                        break;

                    case SquareProcessingStatusCodes.Processed:
                        paymentStatus = Cart_PaymentStatus.Complete;
                        break;

                    case SquareProcessingStatusCodes.Pending:
                    case SquareProcessingStatusCodes.Processing:
                    case SquareProcessingStatusCodes.Transmitting:
                    case SquareProcessingStatusCodes.Transmitted:
                    case SquareProcessingStatusCodes.Exception:
                        paymentStatus = Cart_PaymentStatus.InProgress;
                        break;

                    default:
                        // Includes SquarePaymentWebRequestStatusTypes.Cancelled - should have been filtered out above.
                        //
                        throw new InvalidOperationException($"Unknown payment transaction status type {mWebPaymentRequest.ProcessingStatusCode}.");
                    }
                }

                var result = new UCart_Cart()
                {
                    Order         = mProjectOrder,
                    PaymentStatus = paymentStatus
                };

                log.Result(result);

                return(result);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }