使用 stripe 的表单在 ASP .NET MVC 中处理付款

using stripe's form to process payment in ASP .NET MVC

我正在使用 stripe 处理我网站上的付款。为了实现这一点,我想使用 Stripe 的嵌入式支付表单,称为结账。到目前为止,缺少文档或最新示例来帮助我解决这个问题。

在视图中我嵌入了这样的表单:

<div class="row">
<div class="container col-md-2 col-md-offset-5">
    <h5>Upgrade your account</h5>
    <form action="/Premium/Charge" method="POST">
        <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                data-key="MYKEY"
                data-amount="1000"
                data-name="My Project Name"
                data-description="Premium Account (€10)"
                data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                data-locale="auto"
                data-zip-code="true"
                data-currency="eur">
        </script>
    </form>
</div>

和我的控制器操作来处理 post:

    [HttpPost]
    [Authorize]
    public ActionResult Charge(string stripeToken, string stripeEmail)
    {
        string apiKey = "MYKEY";
        var client = new Stripe.StripeCustomerCreateOptions();

        // our customer
        client.Email = stripeEmail;
        client.SourceToken = stripeToken;

        // creating our charge
        var charge = new Stripe.StripeChargeCreateOptions();
        charge.Amount = 1000;
        charge.Description = "Premium member charge";
        charge.Currency = "EUR";
        charge.SourceTokenOrExistingSourceId = stripeToken;

        // calling stripe to make the charge, then update users profile
        var chargeService = new Stripe.StripeChargeService();
        Stripe.StripeCharge stripeCharge = new Stripe.StripeCharge();

        // Error arises here
        dynamic response = chargeService.Create(charge);

        if (response.Paid)
        {
            // successful payment
            ViewBag.Status = "success";
            return View("Result");
        }

        ViewBag.Status = "unsuccesful";
        return View("Result");
    }

已解决。

我正在使用此代码进行测试,它可以正常工作:

public class CreditCardController : Controller
{
    public ActionResult Charge()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Charge(string stripeToken, string stripeEmail)
    {
        var myCharge = new StripeChargeCreateOptions();

        // always set these properties
        myCharge.Amount = 1000;
        myCharge.Currency = "eur";

        myCharge.ReceiptEmail = stripeEmail;
        myCharge.Description = "Test Charge";
        myCharge.SourceTokenOrExistingSourceId = stripeToken;
        myCharge.Capture = true;

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

        return View();
    }
}

查看代码。密钥是 Stripe 文档中的演示密钥。我使用的测试信用卡号是 4242424242424242.

<div class="row">
    <div class="container col-md-2 col-md-offset-5">
        <h5>Upgrade your account</h5>
        <form action="/CreditCard/Charge" method="POST">
            <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="pk_test_6pRNASCoBOKtIshFeQd4XMUh"
                    data-amount="1000"
                    data-name="My Project Name"
                    data-description="Premium Account (€10)"
                    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                    data-locale="auto"
                    data-zip-code="true"
                    data-currency="eur">
            </script>
        </form>
    </div>
</div>

在做出应该可以正常工作的动态响应之前删除该行。您正在向条带服务器发出两个请求。