3

I am using stripe to process payments on my site. To implement this I want to use Stripe's embedded payment form, called checkout. There's a lack of documentation or up to-date examples to help me with this so far.

In the view I have embedded the form like so:

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

And my controller action to handle the 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");
    }

SOLVED.

Gareth Quirke
  • 216
  • 1
  • 8
  • 25
  • 1
    Are you able to collect all of the arguments that are passed to the form, to ensure they're actually being passed along with the request? Rather than just trying to use them? – korben Mar 08 '17 at 00:20
  • 1
    I tested using the exact code you have in your View and action method signature, and all works well. Are you missing the [HttpPost] decorator on the method? – Ashley Lee Mar 08 '17 at 04:42
  • 2
    I forgot to include it actually in the code snippet, it is above it. Is there a way to check if the response was successful from that controller? – Gareth Quirke Mar 08 '17 at 12:38
  • 2
    @korben I'll put a break point in and see – Gareth Quirke Mar 08 '17 at 12:39

2 Answers2

2

I"m using this code to test and it works without issues:

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

View code. The key is the demo key from the Stripe documentation. Test credit card number I'm using is 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>
Ashley Lee
  • 3,810
  • 1
  • 18
  • 26
  • I am also trying the same but here i am getting one Assembly related issue at the line StripeCharge stripeCharge = chargeService.Create(myCharge); the issue is Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040). could you help me with this issue? – Madhav Jan 19 '18 at 10:27
  • Stripe Payment Integration in Asp.net Web Forms and its 100 percent working code and you can also download application https://code2night.com/Blog/MyBlog/Implement-Stripe-Payment-Gateway-In-ASPNET – Shubham Sep 11 '20 at 18:46
1

Remove the line before you make the dynamic response that should work fine. You were making two requests to the stripe server.

  • Stripe Payment Integration in Asp.net Web Forms and its 100 percent working code and you can also download application https://code2night.com/Blog/MyBlog/Implement-Stripe-Payment-Gateway-In-ASPNET – Shubham Sep 11 '20 at 18:46