Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : set default payment method in stripe

I am new in stripe, how can we set default payment method in stripe.

And can we pass cardId/sourceId to charge customer along with customerId.

Code:-

private static async Task<string> ChargeCustomer(string customerId)
{
    return await System.Threading.Tasks.Task.Run(() =>
    {
        var myCharge = new StripeChargeCreateOptions
        {
            Amount = 50,
            Currency = "gbp",
            Description = "Charge for property sign and postage",
            CustomerId = customerId
        };

        var chargeService = new StripeChargeService();
        var stripeCharge = chargeService.Create(myCharge);

        return stripeCharge.Id;
    });
}

And 1 more question, how to get charge-list, I am using below code but getting exception(conversion error):-

  private IEnumerable<StripeCharge> GetChargeList()
    {
        var chargeService = new StripeChargeService();
        return chargeService.List();
    }
like image 734
Sunil Chaudhary Avatar asked Oct 29 '22 05:10

Sunil Chaudhary


2 Answers

This is what I ended up doing. Not sure why Stripe Checkout didn't set the card for the subscription setup as the default. Anyway, this fires triggered from the payment_intent.succeeded web hook. Sure there is a better way, but...

    var customerService = new CustomerService(Configs.STRIPE_SECRET_KEY);
    var c = customerService.Get(pi.CustomerId);

    if (!string.IsNullOrEmpty(c.InvoiceSettings.DefaultPaymentMethodId)) {
      status = "already has default payment method, no action";
      hsc = HttpStatusCode.OK;
      return;
    }


    var paymentMethodService = new PaymentMethodService(Configs.STRIPE_SECRET_KEY);
    var lopm = paymentMethodService.ListAutoPaging(options: new PaymentMethodListOptions {
      CustomerId = pi.CustomerId,
      Type = "card"
    });

    if (!lopm.Any()) {
      status = "customer has no payment methods";
      hsc = HttpStatusCode.BadRequest;
      return;
    }
    var pm = lopm.FirstOrDefault();
    customerService.Update(pi.CustomerId, options: new CustomerUpdateOptions {
      InvoiceSettings = new CustomerInvoiceSettingsOptions {
        DefaultPaymentMethodId = pm.Id
      }
    });

    hsc = HttpStatusCode.OK;
    return;
like image 51
sobelito Avatar answered Nov 12 '22 20:11

sobelito


We can pass cardId/BankAccountId/TokenId/SourceId in SourceTokenOrExistingSourceId property of StripeChargeCreateOptions,

private static async Task<string> ChargeCustomer(string customerId, string cardId)
        {
            try
            {
                return await System.Threading.Tasks.Task.Run(() =>
                {
                    var myCharge = new StripeChargeCreateOptions
                    {
                        Amount = 50,
                        Currency = "gbp",
                        Description = "Charge for property sign and postage",
                        CustomerId = customerId,
                        SourceTokenOrExistingSourceId = cardId
                    };

                    var chargeService = new StripeChargeService();
                    var stripeCharge = chargeService.Create(myCharge);

                    return stripeCharge.Id;
                });
            }
            catch(Exception ex)
            {
                return "";
            }
        }

To set/change default payment method:-

 public void ChangeDefaultPayment(string customerId, string sourceId)
    {
        var myCustomer = new StripeCustomerUpdateOptions();
        myCustomer.DefaultSource = sourceId;
        var customerService = new StripeCustomerService();
        StripeCustomer stripeCustomer = customerService.Update(customerId, myCustomer);
    }

Still looking for how to get charge-list.

like image 36
Sunil Chaudhary Avatar answered Nov 12 '22 20:11

Sunil Chaudhary