Example #1
0
        private async Task <string> ProcessPayment(StripeChargeModel model)
        {
            return(await Task.Run(() =>
            {
                var charge = new Stripe.StripeChargeCreateOptions
                {
                    Amount = (int)(model.Amount),
                    Currency = "usd",
                    Description = "Purchase",
                    Source = new Stripe.StripeSourceOptions
                    {
                        TokenId = model.Token
                    }
                };

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

                var chargeService = new Stripe.StripeChargeService("sk_test_4lknJTkG0q14upuDDfpZO1Nl006QJSGsZF");
                Stripe.StripeCharge chargeId = null;
                try
                {
                    chargeId = chargeService.Create(charge);
                } catch (Exception e)
                {
                    System.Diagnostics.Debug.Print(e.Message);
                }


                return chargeId.Id;
            }));
        }
Example #2
0
        /// <summary>
        /// Tries to create a charge.
        /// </summary>
        /// <param name="userId">User Id.</param>
        /// <param name="options">Payment options.</param>
        /// <param name="token">Card token.</param>
        /// <param name="description">Charge description.</param>
        /// <exception cref="Ifly.Payments.ChargeException">Occurs when charge is failed.</exception>
        public void TryCreateCharge(int userId, PaymentOptions options, string token, string description)
        {
            User u      = null;
            int  amount = 0;

            Stripe.StripeCharge charge = null;
            ChargeException.ChargeFailureReason?reason = null;

            if (userId > 0 && !string.IsNullOrEmpty(token) && options.Subscription != SubscriptionType.Basic)
            {
                using (var repo = Resolver.Resolve <IUserRepository>())
                {
                    u = repo.Select(userId);

                    if (u != null)
                    {
                        try
                        {
                            amount = this.GetChargeAmount(options);

                            if (amount > 0)
                            {
                                charge = new Stripe.StripeChargeService().Create(new Stripe.StripeChargeCreateOptions()
                                {
                                    Amount      = amount,
                                    Currency    = "usd",
                                    Card        = token,
                                    Description = description.IndexOf("{0}") >= 0 ? string.Format(description, (int)(amount / 100)) : description
                                });

                                if (!charge.Paid.HasValue || !charge.Paid.Value)
                                {
                                    OnChargeException(new InvalidOperationException("Charge failed (response from Stripe)."),
                                                      userId, options.Subscription, token, u, charge);
                                }
                                else
                                {
                                    if (u.Subscription == null)
                                    {
                                        u.Subscription = new UserSubscription();
                                    }

                                    u.Subscription.Renewed           = DateTime.UtcNow;
                                    u.Subscription.RenewedTo         = options.Subscription;
                                    u.Subscription.RenewedToDuration = options.Duration;

                                    repo.Update(u);

                                    this.CreatePaymentHistoryEntry(u.Id, options, amount, charge.StripeCard != null ? string.Format("{0} (xxxx xxxx xxxx {1})",
                                                                                                                                    charge.StripeCard.Type, charge.StripeCard.Last4) : null, charge.Id, false);
                                }
                            }
                            else
                            {
                                reason = ChargeException.ChargeFailureReason.BadRequest;
                            }
                        }
                        catch (Exception ex)
                        {
                            OnChargeException(ex, userId, options.Subscription, token, u, charge);
                        }
                    }
                    else
                    {
                        reason = ChargeException.ChargeFailureReason.BadRequest;
                    }
                }
            }
            else
            {
                reason = ChargeException.ChargeFailureReason.BadRequest;
            }

            if (reason.HasValue)
            {
                OnChargeException(new InvalidOperationException("Charge failed (general failure)."),
                                  userId, options.Subscription, token, u, charge, reason.Value);
            }
        }