dimanche 22 mai 2016

Use Stripe to post to external API

I'm adding payment to my app using Stripe. My app is divided into two parts, a front-end Angular app (http://ift.tt/1WJWIzJ), and a separate Laravel API. They live on two servers.

I'm using Stripe Checkout embed form and Laravel Cashier. Stripe is supposed to hijack my POST request and send the Credit Card data to their servers, then return a credit card token to me, to which I then post to my server.

Their form action assumes my app's API and ClientSide is all within the same app: <form action="" method="POST">

So, to post the token to my API endpoint, doIt, I added:

<form action="http://ift.tt/1qE1bpR" method="POST">
    <script
            src="http://ift.tt/1doUtf9" class="stripe-button"
            data-key="pk_test_UAgGwte0zwi0vCHVSKjAooqk"
            data-amount="999"
            data-name="Demo Site"
            data-description="Widget"
            data-image="/img/documentation/checkout/marketplace.png"
            data-locale="auto">
    </script>
</form>

Which should go here: $router->post('/doIt', 'Resources\Subscriptions@createSubscription');

And I'm trying to die out this info in the Controller:

public function createSubscription()
{

    $token = Input::get('stripeToken');

    $user = User::find(1);

    dd($token);

    $user->subscription('monthly')->create($token);

    $user->trial_ends_at = Carbon::now()->addDays(14);

    $user->save();
}

When I click the Checkout client button, the request goes as expected to Stripe, but it simply routes me to my API. How can I post the credit card token to my API and route as necessary on my client side? (Like, "payment successful!")


Edit: I'm using Angular, so I was thinking of capturing the hidden CC token field in my form controller, then posting from there. But the hidden field is generated on the fly and therefore doesn't exist.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire