Note that there are some explanatory texts on larger screens.

plurals
  1. POStripe Webhooks Not Working
    primarykey
    data
    text
    <p>I have implemented a stripe test gateway in an application... The integration page is using the jQuery mode for sending data.</p> <p>Its built in the MVC structure using Codeigniter.</p> <p>Now, here my controller:</p> <pre><code>&lt;?php class stripe_connect extends CI_Controller{ public function __construct() { parent::__construct(); @session_start(); } function index() { $this-&gt;load-&gt;view('stripe_same_site_connect'); } } ?&gt; </code></pre> <p>And here's my view file,</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="&lt;?php echo base_url();?&gt;assets/front_assets/stripe/js/jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="https://js.stripe.com/v1/"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; Stripe.setPublishableKey('PUBLIC KEY'); // Test key! &lt;/script&gt; &lt;script type="text/javascript" src="&lt;?php echo base_url();?&gt;assets/front_assets/stripe/js/buy-controller.js"&gt;&lt;/script&gt; &lt;script&gt; function showErrorDialogWithMessage(message) { // For the tutorial, we'll just do an alert. You should customize this function to // present "pretty" error messages on your page. alert(message); // Re-enable the submit button so the user can try again $('#buy-submit-button').removeAttr("disabled"); } // Called when Stripe's server returns a token (or an error) function stripeResponseHandler(status, response) { if (response.error) { // Stripe.js failed to generate a token. The error message will explain why. // Usually, it's because the customer mistyped their card info. // You should customize this to present the message in a pretty manner: alert(response.error.message); } else { // Stripe.js generated a token successfully. We're ready to charge the card! var token = response.id; var firstName = $("#first-name").val(); var lastName = $("#last-name").val(); var email = $("#email").val(); // We need to know what amount to charge. Assume $20.00 for the tutorial. // You would obviously calculate this on your own: var price = 20; // Make the call to the server-script to process the order. // Pass the token and non-sensitive form information. var request = $.ajax ({ type: "POST", url: "&lt;?php echo base_url();?&gt;ajax/stripe_payment", dataType: "json", data: { "stripeToken" : token, "firstName" : firstName, "lastName" : lastName, "email" : email, "price" : price } }); request.done(function(msg) { if (msg.result === 0) { // Customize this section to present a success message and display whatever // should be displayed to the user. alert(msg.message); } else { // The card was NOT charged successfully, but we interfaced with Stripe // just fine. There's likely an issue with the user's credit card. // Customize this section to present an error explanation alert("The user's credit card failed."); } }); request.fail(function(jqXHR, textStatus) { // We failed to make the AJAX call to pay.php. Something's wrong on our end. // This should not normally happen, but we need to handle it if it does. alert("Error: failed to call pay.php to process the transaction."); }); } } $(document).ready(function() { $('#buy-form').submit(function(event) { // immediately disable the submit button to prevent double submits $('#buy-submit-button').attr("disabled", "disabled"); var fName = $('#first-name').val(); var lName = $('#last-name').val(); var email = $('#email').val(); var cardNumber = $('#card-number').val(); var cardCVC = $('#card-security-code').val(); // First and last name fields: make sure they're not blank if (fName === "") { showErrorDialogWithMessage("Please enter your first name."); return; } if (lName === "") { showErrorDialogWithMessage("Please enter your last name."); return; } // Validate the email address: var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (email === "") { showErrorDialogWithMessage("Please enter your email address."); return; } else if (!emailFilter.test(email)) { showErrorDialogWithMessage("Your email address is not valid."); return; } // Stripe will validate the card number and CVC for us, so just make sure they're not blank if (cardNumber === "") { showErrorDialogWithMessage("Please enter your card number."); return; } if (cardCVC === "") { showErrorDialogWithMessage("Please enter your card security code."); return; } // Boom! We passed the basic validation, so request a token from Stripe: Stripe.createToken({ number: cardNumber, cvc: cardCVC, exp_month: $('#expiration-month').val(), exp_year: $('#expiration-year').val() }, stripeResponseHandler); // Prevent the default submit action on the form return false; }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;Payment Form&lt;/h2&gt; &lt;form id="buy-form" method="post" action="javascript:"&gt; &lt;p class="form-label"&gt;First Name:&lt;/p&gt; &lt;input class="text" id="first-name" spellcheck="false"&gt;&lt;/input&gt; &lt;p class="form-label"&gt;Last Name:&lt;/p&gt; &lt;input class="text" id="last-name" spellcheck="false"&gt;&lt;/input&gt; &lt;p class="form-label"&gt;Email Address:&lt;/p&gt; &lt;input class="text" id="email" spellcheck="false"&gt;&lt;/input&gt; &lt;p class="form-label"&gt;Credit Card Number:&lt;/p&gt; &lt;input class="text" id="card-number" autocomplete="off"&gt;&lt;/input&gt; &lt;p class="form-label"&gt;Expiration Date:&lt;/p&gt; &lt;select id="expiration-month"&gt; &lt;option value="1"&gt;January&lt;/option&gt; &lt;option value="2"&gt;February&lt;/option&gt; &lt;option value="3"&gt;March&lt;/option&gt; &lt;option value="4"&gt;April&lt;/option&gt; &lt;option value="5"&gt;May&lt;/option&gt; &lt;option value="6"&gt;June&lt;/option&gt; &lt;option value="7"&gt;July&lt;/option&gt; &lt;option value="8"&gt;August&lt;/option&gt; &lt;option value="9"&gt;September&lt;/option&gt; &lt;option value="10"&gt;October&lt;/option&gt; &lt;option value="11"&gt;November&lt;/option&gt; &lt;option value="12"&gt;December&lt;/option&gt; &lt;/select&gt; &lt;select id="expiration-year"&gt; &lt;?php $yearRange = 20; $thisYear = date('Y'); $startYear = ($thisYear + $yearRange); foreach (range($thisYear, $startYear) as $year) { if ( $year == $thisYear) { print '&lt;option value="'.$year.'" selected="selected"&gt;' . $year . '&lt;/option&gt;'; } else { print '&lt;option value="'.$year.'"&gt;' . $year . '&lt;/option&gt;'; } } ?&gt; &lt;/select&gt; &lt;p class="form-label"&gt;CVC:&lt;/p&gt; &lt;input class="text" id="card-security-code" autocomplete="off"&gt;&lt;/input&gt; &lt;input id="buy-submit-button" type="submit" value="Place This Order »"&gt;&lt;/input&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And Here's the ajax payment controller file:</p> <pre><code>&lt;?php class ajax extends CI_Controller{ public function __construct() { parent::__construct(); require APPPATH.'libraries/stripeLibrary/Stripe.php'; } // Helper Function: used to post an error message back to our caller function returnErrorWithMessage($message) { $a = array('result' =&gt; 1, 'errorMessage' =&gt; $message); echo json_encode($a); } function stripe_payment() { // Credit Card Billing //require_once('stripeLibrary/Stripe.php'); // change this path to wherever you put the Stripe PHP library! $trialAPIKey = "TEST KEY"; // These are the SECRET keys! $liveAPIKey = "LIVE KEY"; Stripe::setApiKey($trialAPIKey); // Switch to change between live and test environments // Get all the values from the form $token = $_POST['stripeToken']; $email = $_POST['email']; $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $price = $_POST['price']; $priceInCents = $price * 100; // Stripe requires the amount to be expressed in cents try { // We must have all of this information to proceed. If it's missing, balk. if (!isset($token)) throw new Exception("Website Error: The Stripe token was not generated correctly or passed to the payment handler script. Your credit card was NOT charged. Please report this problem to the webmaster."); if (!isset($email)) throw new Exception("Website Error: The email address was NULL in the payment handler script. Your credit card was NOT charged. Please report this problem to the webmaster."); if (!isset($firstName)) throw new Exception("Website Error: FirstName was NULL in the payment handler script. Your credit card was NOT charged. Please report this problem to the webmaster."); if (!isset($lastName)) throw new Exception("Website Error: LastName was NULL in the payment handler script. Your credit card was NOT charged. Please report this problem to the webmaster."); if (!isset($priceInCents)) throw new Exception("Website Error: Price was NULL in the payment handler script. Your credit card was NOT charged. Please report this problem to the webmaster."); try { // create the charge on Stripe's servers. THIS WILL CHARGE THE CARD! $charge = Stripe_Charge::create(array( "amount" =&gt; $priceInCents, "currency" =&gt; "usd", "card" =&gt; $token, "description" =&gt; $email) ); if ($charge-&gt;paid == true) { $array = array('result' =&gt; 0, 'email' =&gt; $email, 'price' =&gt; $price, 'message' =&gt; 'Thank you; your transaction was successful!'); echo json_encode($array); // Store the order in the database. // Send the email. // Celebrate! } else { // Charge was not paid! $array = array('result' =&gt; 0, 'email' =&gt; $email, 'price' =&gt; $price, 'message' =&gt; 'Not paid'); echo json_encode($array); } // If no exception was thrown, the charge was successful! // Here, you might record the user's info in a database, email a receipt, etc. // Return a result code of '0' and whatever other information you'd like. // This is accessible to the jQuery Ajax call return-handler in "buy-controller.js" } catch (Stripe_Error $e) { // The charge failed for some reason. Stripe's message will explain why. $message = $e-&gt;getMessage(); returnErrorWithMessage($message); } } catch (Exception $e) { // One or more variables was NULL $message = $e-&gt;getMessage(); returnErrorWithMessage($message); } } } ?&gt; </code></pre> <p>Now the issue is that, I am getting positive alert message which states that payment is done.</p> <p>But I can't get any transaction record when i see the test dashboard.</p> <p>Also I can't see any webhook event occurring in the dashboard as well, but I have enabled the webhook.</p> <p>Here's my webhook URL:</p> <p><a href="http://the-iguts.com/wepay_testing/get_stripe_pay" rel="nofollow noreferrer">http://the-iguts.com/wepay_testing/get_stripe_pay</a></p> <p>And my script in the controllwe file is:</p> <pre><code>&lt;?php class get_stripe_pay extends CI_Controller{ public function __construct() { parent::__construct(); require APPPATH.'libraries/stripeLibrary/Stripe.php'; } function index() { $trialAPIKey = "TEST KEY"; // These are the SECRET keys! //$liveAPIKey = "LIVE KEY"; Stripe::setApiKey($trialAPIKey); // Switch to change between live and test environments $body = file_get_contents('php://input'); $event_json = json_decode($body); $header = "From: MyApp Support &lt;support@myapp.com&gt;"; $admin = "support@myapp.com"; try { // for extra security, retrieve from the Stripe API, this will fail in Test Webhooks $event_id = $event_json-&gt;{'id'}; $event = Stripe_Event::retrieve($event_id); if ($event-&gt;type == 'charge.succeeded') { $this-&gt;load-&gt;library('email'); $config['charset'] = 'utf-8'; $config['wordwrap'] = TRUE; $config['mailtype'] = 'html'; $config['protocol'] = 'sendmail'; $this-&gt;email-&gt;initialize($config); $this-&gt;email-&gt;from("saswat.saz.routroy@gmail.com"); $this-&gt;email-&gt;to($event-&gt;data-&gt;object-&gt;customer); $this-&gt;email-&gt;subject("Payment"); $this-&gt;email-&gt;message("Payment"); if($this-&gt;email-&gt;send()) { $this-&gt;email-&gt;from($event-&gt;data-&gt;object-&gt;customer); $this-&gt;email-&gt;to("saswat.saz.routroy@gmail.com"); $this-&gt;email-&gt;subject("Payment"); $this-&gt;email-&gt;message("Payment"); if($this-&gt;email-&gt;send()) { return "success"; } else die("error". mysql_error()); } else die("error". mysql_error()); } // This will send receipts on succesful invoices if ($event-&gt;type == 'invoice.payment_succeeded') { email_invoice_receipt($event-&gt;data-&gt;object, $header); } } catch (Stripe_InvalidRequestError $e) { mail($admin, $e-&gt;getMessage(), $body, $header); } function email_payment_receipt($payment, $email, $header) { $subject = 'Payment has been received'; mail($email, $subject, payment_received_body($payment, $email), $header); } } } ?&gt; </code></pre> <p><img src="https://i.stack.imgur.com/heWDb.png" alt="enter image description here"></p> <p>Also see the webhook section, no event occurred.</p> <p><img src="https://i.stack.imgur.com/8gzM3.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload