Skip to main content
 首页 » 编程设计

c#之ASP.NET stripe.net 无法向没有事件卡的客户收费

2024年02月05日14powertoolsteam

 protected void ChargePayment(object sender, EventArgs e) 
    { 
        StripeCustomer CurrentCustomer = GetCustomer(); 
 
        if (CurrentCustomer == null) 
        { 
            return; 
        } 
 
 
        var myCharge = new StripeChargeCreateOptions(); 
 
 
        myCharge.Currency = "dkk"; 
        myCharge.CustomerId = CurrentCustomer.Id; 
        myCharge.Description = "KPHF Prorated Charge"; 
 
        string key = "sk_test_P6GjMq1OVwmxZv5YNozAX6dY"; 
        var chargeService = new StripeChargeService(key); 
 
        try 
        { 
            chargeService.Create(myCharge); 
 
        } 
        catch (StripeException ex) 
        { 
            exError.Text = ex.Message; 
        } 
 
 
    } 
 
    private StripeCustomer GetCustomer() 
    { 
        MembershipUser CurrentUser = Membership.GetUser(); 
        var myCustomer = new StripeCustomerCreateOptions(); 
        var myCustomer2 = new StripeCreditCardOptions(); 
 
        myCustomer.Email = cMail.Text; 
        myCustomer2.TokenId = CreditCard.Text; 
        myCustomer2.ExpirationMonth = CardMonth.SelectedItem.Text; 
        myCustomer2.ExpirationYear = CardYear.SelectedItem.Text; 
        myCustomer2.Cvc = cvc.Text; 
 
        myCustomer.PlanId = "1"; 
 
 
        var customerService = new StripeCustomerService("sk_test_P6GjMq1OVwmxZv5YNozAX6dY"); 
 
        try 
        { 
            StripeCustomer result = customerService.Create(myCustomer); 
 
            return result; 
        } 
        catch (StripeException ex) 
        { 
            exError.Text = ex.Message; 
            return null; 
        } 

输入信用卡信息后,我确实在条纹系统中创建了客户,但他没有被收费,我得到以下异常:“无法向没有事件卡的客户收费”。有任何帮助或提示吗?

请您参考如下方法:

您没有将卡附在客户身上。您已创建卡片对象但未与客户关联。按照此语法添加卡片

var myCustomer = new StripeCustomerCreateOptions(); 
    myCustomer.Email = "pork@email.com"; 
    myCustomer.Description = "Johnny Tenderloin (pork@email.com)"; 
 
    // setting up the card 
    myCustomer.SourceCard = new SourceCard() 
    { 
        Number = "4242424242424242", 
        ExpirationYear = "2022", 
        ExpirationMonth = "10", 
        Cvc = "1223"                          // optional 
    }; 
 
    myCustomer.PlanId = *planId*;                          // only if you have a plan 
    myCustomer.TaxPercent = 20;                            // only if you are passing a plan, this tax percent will be added to the price. 
    myCustomer.Coupon = *couponId*;                        // only if you have a coupon 
    myCustomer.TrialEnd = DateTime.UtcNow.AddMonths(1);    // when the customers trial ends (overrides the plan if applicable) 
    myCustomer.Quantity = 1;                               // optional, defaults to 1 
 
    var customerService = new StripeCustomerService(); 
    StripeCustomer stripeCustomer = customerService.Create(myCustomer) 

您可以在这里阅读更多相关信息 stripe .net