ASP.NET stripe.net无法向没有活动卡的客户收费

时间:2016-08-09 12:49:51

标签: c# asp.net stripe.net

 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;
        }

输入信用卡信息后,我确实在条带系统中创建了客户,但他没有收费,我得到以下例外:"无法向没有活动卡的客户收取费用#34;。任何帮助或提示?

1 个答案:

答案 0 :(得分:4)

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

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