Example #1
0
        public async Task <Charge> Charge(ChargeDTO charge)
        {
            var idClaim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            if (!long.TryParse(idClaim, out var ownerId))
            {
                throw new UnauthorizedAccessException();
            }

            var ownerUser = await _userRepository.GetByIdAsync(ownerId);

            var customers = new CustomerService();
            var charges   = new Stripe.ChargeService();

            var customer = await customers.CreateAsync(new CustomerCreateOptions
            {
                Email       = ownerUser.Email,
                SourceToken = charge.Token
            });

            var newCharge = await charges.CreateAsync(new ChargeCreateOptions
            {
                Amount      = charge.Value,
                Description = "Test charge",
                Currency    = charge.Currency,
                CustomerId  = customer.Id
            });

            return(newCharge);
        }
        public async Task <Charge> CreateChargeAsync(string customerId, string source, string destination, string description, decimal totalAmount, decimal applicationFeeAmount, Dictionary <string, string> metadata)
        {
            var options = new ChargeCreateOptions
            {
                Customer             = customerId,
                Source               = source,
                Description          = description,
                Amount               = (long)(totalAmount * 100),
                ApplicationFeeAmount = (long)(applicationFeeAmount * 100),
                TransferData         = new ChargeTransferDataOptions {
                    Destination = destination
                },
                Currency = "usd",
                Capture  = false,
                Metadata = metadata
            };

            var service = new Stripe.ChargeService();

            return(await service.CreateAsync(options));
        }
        public async Task <(ResponseModel, Stripe.Charge)> ProcessPayment(PaymentCreateBindingModel model)
        {
            var responseModel = new ResponseModel()
            {
                ServiceStatus = Enums.ServiceStatus.Error
            };

            try
            {
                StripeConfiguration.ApiKey = "sk_test_kRWpvCZmKMlhNd5BOMK10CfV";
                var metaData = new Dictionary <string, string>
                {
                    { nameof(model.CardholderName), model.CardholderName }
                };
                var myCharge = new Stripe.ChargeCreateOptions
                {
                    Amount      = (int)(model.Amount * 100),
                    Currency    = "AUD",
                    Metadata    = metaData,
                    Source      = model.StripeToken,
                    Description = $"$ {model.Amount} paid for customer"
                };

                var stripeCharge = await _stripeChargeService.CreateAsync(myCharge);

                responseModel.ServiceStatus = Enums.ServiceStatus.Success;
                return(responseModel, stripeCharge);
            }
            catch (Stripe.StripeException e)
            {
                responseModel.Errors.Add((e.HttpStatusCode.ToString(), e.Message));
                return(responseModel, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #4
0
        public async Task <IActionResult> Create([FromBody] CreateOrderViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _db.Users.SingleAsync(x => x.UserName == HttpContext.User.Identity.Name);

            var order = new Data.Entities.Order
            {
                DeliveryAddress = new Data.Entities.Address
                {
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    Address1  = model.Address1,
                    Address2  = model.Address2,
                    TownCity  = model.TownCity,
                    County    = model.County,
                    Postcode  = model.Postcode
                },
                Items = model.Items.Select(x => new Data.Entities.OrderItem
                {
                    ProductId = x.ProductId,
                    ColourId  = x.ColourId,
                    StorageId = x.StorageId,
                    Quantity  = x.Quantity
                }).ToList()
            };

            user.Orders.Add(order);
            await _db.SaveChangesAsync();

            var total = await _db.Orders
                        .Where(x => x.Id == order.Id)
                        .Select(x => Convert.ToInt32(x.Items.Sum(i =>
                                                                 i.ProductVariant.Price * i.Quantity) * 100))
                        .SingleAsync();

            var charges = new Stripe.ChargeService();
            var charge  = await charges.CreateAsync(new Stripe.ChargeCreateOptions
            {
                Amount      = total,
                Description = $"Order {order.Id} payment",
                Currency    = "GBP",
                Source      = model.StripeToken
            });

            if (string.IsNullOrEmpty(charge.FailureCode))
            {
                order.PaymentStatus = PaymentStatus.Paid;
            }
            else
            {
                order.PaymentStatus = PaymentStatus.Declined;
            }

            await _db.SaveChangesAsync();

            return(Ok(new CreateOrderResponseViewModel(order.Id, order.PaymentStatus)));
        }