2017-03-07 59 views
2

我正在使用stripe處理我的網站上的付款。爲了實現這一點,我想使用Stripe的嵌入式支付形式,稱爲結帳。目前缺乏文檔或最新的例子來幫助我。使用stripe的表單來處理ASP .NET中的付款MVC

在我有嵌入的形式,像這樣的觀點:

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

而我的控制器操作處理後:

[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"); 
    } 

解決。

+1

你能收集到所有的是傳遞參數編輯到表單,以確保它們實際上與請求一起傳遞?而不是隻是試圖使用它們? – korben

+1

我使用您在視圖和操作方法簽名中具有的確切代碼進行了測試,並且都運行良好。你是否遺漏了該方法上的[HttpPost]裝飾器? –

+2

我忘了將它實際包含在代碼片段中,它在它之上。有沒有方法可以檢查來自該控制器的響應是否成功? –

回答

2

我「米使用此代碼來測試和它的工作原理沒有問題:。

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

查看代碼的關鍵是從我使用的是磁條文檔測試信用卡號碼演示關鍵是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> 
+0

我也試圖相同,但在這裏我得到一個大會相關的問題在線 StripeCharge stripeCharge = chargeService.Create(myCharge); 問題是 無法加載文件或程序集「Newtonsoft.Json,Version = 9.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed」或其某個依賴項。定位的程序集清單定義與程序集引用不匹配。 (來自HRESULT的異常:0x80131040)。 你能幫我解決這個問題嗎? – Madhav

0

刪除行,你作出這樣應該可以正常工作的動態響應之前,你是做兩個請求到服務器條紋。