Пример #1
0
        public async Task <List <StripeTransaction> > GetListOfTransactions()
        {
            var transactions = new List <StripeTransaction>();

            var stripeClient = new StripeClient(appSettings.ApiSecret);

            var options = new ChargeListOptions {
                Limit = 100
            };
            var service = new ChargeService(stripeClient);
            StripeList <Charge> charges = await service.ListAsync(
                options
                );

            return(charges.Data.Select(c => new StripeTransaction()
            {
                Id = c.Id,
                Paid = c.Paid,
                ApplicationFeeAmount = MoneyExtender.ConvertToDollars(c.ApplicationFeeAmount != null ? c.ApplicationFeeAmount.Value : 0),
                Status = c.Status,
                Description = c.Description,
                PaymentType = c.Object,
                AmountRefunded = MoneyExtender.ConvertToDollars(c.AmountRefunded),
                Amount = MoneyExtender.ConvertToDollars(c.Amount),
                Created = c.Created,
                FailureCode = c.FailureCode,
                FailureMessage = c.FailureMessage,
                TotalAmount = MoneyExtender.ConvertToDollars(c.Amount) - MoneyExtender.ConvertToDollars(c.AmountRefunded)
            }).ToList());
        }
Пример #2
0
        public async Task CancelAndRecoverChargesAsync(ISubscriber subscriber)
        {
            if (!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
            {
                var subscriptionService = new SubscriptionService();
                await subscriptionService.CancelAsync(subscriber.GatewaySubscriptionId,
                                                      new SubscriptionCancelOptions());
            }

            if (string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
            {
                return;
            }

            var customerService = new CustomerService();
            var customer        = await customerService.GetAsync(subscriber.GatewayCustomerId);

            if (customer == null)
            {
                return;
            }

            if (customer.Metadata.ContainsKey("btCustomerId"))
            {
                var transactionRequest = new Braintree.TransactionSearchRequest()
                                         .CustomerId.Is(customer.Metadata["btCustomerId"]);
                var transactions = _btGateway.Transaction.Search(transactionRequest);

                if ((transactions?.MaximumCount ?? 0) > 0)
                {
                    var txs = transactions.Cast <Braintree.Transaction>().Where(c => c.RefundedTransactionId == null);
                    foreach (var transaction in txs)
                    {
                        await _btGateway.Transaction.RefundAsync(transaction.Id);
                    }
                }

                await _btGateway.Customer.DeleteAsync(customer.Metadata["btCustomerId"]);
            }
            else
            {
                var chargeService = new ChargeService();
                var charges       = await chargeService.ListAsync(new ChargeListOptions
                {
                    CustomerId = subscriber.GatewayCustomerId
                });

                if (charges?.Data != null)
                {
                    var refundService = new RefundService();
                    foreach (var charge in charges.Data.Where(c => !c.Refunded))
                    {
                        await refundService.CreateAsync(new RefundCreateOptions { ChargeId = charge.Id });
                    }
                }
            }

            await customerService.DeleteAsync(subscriber.GatewayCustomerId);
        }
Пример #3
0
        public async Task <IEnumerable <Charge> > ListCharges()
        {
            var service = new ChargeService();
            var options = new ChargeListOptions
            {
                Limit = 3,
            };
            var charges = await service.ListAsync(options);

            return(charges);
        }
Пример #4
0
        public async Task <StripeList <Charge> > GetCharges(string customerId, string startingAfter = null)
        {
            var service = new ChargeService();
            var items   = await service.ListAsync(new ChargeListOptions
            {
                Customer      = customerId,
                Limit         = 100,
                StartingAfter = startingAfter
            });

            return(items);
        }
        public async Task <IActionResult> GetPayments()
        {
            var options = new ChargeListOptions
            {
                Limit = 100
            };
            var service = new ChargeService();
            StripeList <Charge> charges = await service.ListAsync(
                options
                );

            return(new OkObjectResult(new { success = "true", payments = PaymentMapper.Map(charges.Data) }));
        }
Пример #6
0
        public async Task <IActionResult> Import([FromForm] DateTime?after)
        {
            string lastCharge    = null;
            var    importedCount = 0;
            var    userCount     = 0;
            var    paymentCount  = 0;
            var    updatedCount  = 0;

            while (true)
            {
                var charges = await _chargeService.ListAsync(new ChargeListOptions
                {
                    Created = new DateRangeOptions
                    {
                        GreaterThan = after ?? new DateTime(2019, 1, 1)
                    },
                    Limit         = 100,
                    StartingAfter = lastCharge
                });

                importedCount += charges.Count();
                var(users, payments, updates) = await _paymentService.SavePayments(charges);

                userCount    += users;
                paymentCount += payments;
                updatedCount += updates;

                if (!charges.HasMore)
                {
                    break;
                }

                lastCharge = charges.Data.Last().Id;
            }

            TempData["SuccessMessage"] = $"Found {importedCount} payments, created {userCount} new users, saved {paymentCount} new payments and updated {updatedCount} existing payments";
            return(RedirectToAction(nameof(Index)));
        }
Пример #7
0
        public async Task <BillingInfo> GetBillingAsync(ISubscriber subscriber)
        {
            var billingInfo = new BillingInfo();

            ICollection <Transaction> transactions = null;

            if (subscriber is User)
            {
                transactions = await _transactionRepository.GetManyByUserIdAsync(subscriber.Id);
            }
            else if (subscriber is Organization)
            {
                transactions = await _transactionRepository.GetManyByOrganizationIdAsync(subscriber.Id);
            }
            if (transactions != null)
            {
                billingInfo.Transactions = transactions?.OrderByDescending(i => i.CreationDate)
                                           .Select(t => new BillingInfo.BillingTransaction(t));
            }

            var customerService     = new CustomerService();
            var subscriptionService = new SubscriptionService();
            var chargeService       = new ChargeService();
            var invoiceService      = new InvoiceService();

            if (!string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
            {
                var customer = await customerService.GetAsync(subscriber.GatewayCustomerId);

                if (customer != null)
                {
                    billingInfo.CreditAmount = customer.AccountBalance / 100M;

                    if (customer.Metadata?.ContainsKey("btCustomerId") ?? false)
                    {
                        try
                        {
                            var braintreeCustomer = await _btGateway.Customer.FindAsync(
                                customer.Metadata["btCustomerId"]);

                            if (braintreeCustomer?.DefaultPaymentMethod != null)
                            {
                                billingInfo.PaymentSource = new BillingInfo.BillingSource(
                                    braintreeCustomer.DefaultPaymentMethod);
                            }
                        }
                        catch (Braintree.Exceptions.NotFoundException) { }
                    }
                    else if (!string.IsNullOrWhiteSpace(customer.DefaultSourceId) && customer.Sources?.Data != null)
                    {
                        if (customer.DefaultSourceId.StartsWith("card_") || customer.DefaultSourceId.StartsWith("ba_"))
                        {
                            var source = customer.Sources.Data.FirstOrDefault(s =>
                                                                              (s is Card || s is BankAccount) && s.Id == customer.DefaultSourceId);
                            if (source != null)
                            {
                                billingInfo.PaymentSource = new BillingInfo.BillingSource(source);
                            }
                        }
                    }

                    var charges = await chargeService.ListAsync(new ChargeListOptions
                    {
                        CustomerId = customer.Id,
                        Limit      = 20
                    });

                    billingInfo.Charges = charges?.Data?.OrderByDescending(c => c.Created)
                                          .Select(c => new BillingInfo.BillingCharge(c));

                    var invoices = await invoiceService.ListAsync(new InvoiceListOptions
                    {
                        CustomerId = customer.Id,
                        Limit      = 20
                    });

                    billingInfo.Invoices = invoices?.Data?.OrderByDescending(i => i.Date)
                                           .Select(i => new BillingInfo.BillingInvoice(i));
                }
            }

            if (!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
            {
                var sub = await subscriptionService.GetAsync(subscriber.GatewaySubscriptionId);

                if (sub != null)
                {
                    billingInfo.Subscription = new BillingInfo.BillingSubscription(sub);
                }

                if (!sub.CanceledAt.HasValue && !string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
                {
                    try
                    {
                        var upcomingInvoice = await invoiceService.UpcomingAsync(
                            new UpcomingInvoiceOptions { CustomerId = subscriber.GatewayCustomerId });

                        if (upcomingInvoice != null)
                        {
                            billingInfo.UpcomingInvoice = new BillingInfo.BillingInvoiceInfo(upcomingInvoice);
                        }
                    }
                    catch (StripeException) { }
                }
            }

            return(billingInfo);
        }
Пример #8
0
 public async Task <StripeList <Charge> > ListAsync(ChargeListOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default)
 {
     return(await chargeService.ListAsync(options, requestOptions, cancellationToken));
 }