0

I'm trying to make an IPN through Stripe so the customer can pay via credit card. I have added the javascript widget into my site as below:

<form id="stripe" action="ipn/stripe/stripe.php" method="POST">
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="'.PK_TEST.'"
        data-amount="'.item_price.'"
        data-name="'.item_name.'"
        data-description="IPN Test"
        data-label="Pay with Stripe &raquo;"
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
        data-email="'.$email.'"
        data-locale="auto">
    </script>
</form>

It works correctly as on my stripe account, I can see the logs for the IPN page (ipn/stripe/stripe.php).

The issue is that the code I'm using in my IPN isn't getting passed through or an error is being executed stopping my mail from emailing me.

This is my IPN code (Note that I changed the email and password so thats not the error, I just didn't want everyone seing my login):

<?php
// Load the Stripe library
require_once 'init.php';
include('../../config/config.php');

// Set your LIVE or TEST secret key
\Stripe\Stripe::setApiKey('sk_test_a8mZExfLGSxNXalX0W3RIXAq');

// Retrieve the request's body
$body = @file_get_contents('php://input');
if (!$body)
    return;

// Parse $body as JSON
$event_json = json_decode($body);
if (!$event_json)
    return;

// Quietly exit out if incomplete data
if ($event_json->object != 'event')
    return;

if (!isset($event_json->id))
    return;

$event_id = $event_json->id;

try {
    // Retrieve authentic event directly from Stripe
    $event = \Stripe_Event::retrieve($event_id);
    http_response_code(200); // PHP 5.4 or greater
}

catch (Exception $e) {
    $err_msg = 'Error: Stripe Webhook. Event id: ' . $event_id . '. ' . $e;
    // Additionally, send email if needed
    error_log($err_msg, 0);
}

// Match up each event type with a function and pass it the event
switch ($event->type) {
    case 'charge.failed':
        charge_failed($event);
        break;
        
    case 'charge.succeeded':
        charge_succeeded($event);
        break;

    default:
        break;
}

# # # # # # # # # # # # # # # # # # # # # # # # # #
#                   Funtions for above                     #
# # # # # # ## # # # # # # # # # # # # # # # # # # 

function charge_failed($event) {
    // function code...
    $to = 'benzahdd55@outlook';
    $message = purchase_email('Error', '0.10', $to, '24-03-2018', 'ABC-ABC-ABC-ABC', 'Ben Wilson');
    require_once('../../mailer/class.phpmailer.php');
    include("../../mailer/class.smtp.php"); 
    require '../../mailer/PHPMailerAutoload.php';

    $mail = new PHPMailer();    
    $body = $message;
    $subject = "Error";
    $mail->SMTPDebug = 1; // enables SMTP debug information (for testing) || 1 = errors and messages || 2 = messages only
    $mail->SMTPAuth  = true; // enable SMTP authentication
    $mail->Host      = "smtp.gmail.com"; // sets the SMTP server
    $mail->SMTPSecure = 'ssl';
    $mail->IsHTML(true);
    $mail->Port      = 465; // set the SMTP port for the GMAIL server
    $mail->Username  = "my_email@gmail.com"; // SMTP account username
    $mail->Password  = "password1"; // SMTP account password
    $mail->SetFrom("admin@easyipn.us", project_name." Admin");
    $mail->AddAddress($to);
    $mail->Subject = $subject;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address, "Buyer@".project_name);

    // Append a new person to the file
    $current .= "About to send mail\n";
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
    http_response_code(200); // PHP 5.4 or greater
}
function charge_succeeded($event) {
    // function code...
    $to = 'benzahdd55@outlook';
    $message = purchase_email('Success', '0.10', $to, '24-03-2018', 'ABC-ABC-ABC-ABC', 'Ben Wilson');
    require_once('../../mailer/class.phpmailer.php');
    include("../../mailer/class.smtp.php"); 
    require '../../mailer/PHPMailerAutoload.php';

    $mail = new PHPMailer();    
    $body = $message;
    $subject = "Success!";
    $mail->SMTPDebug = 1; // enables SMTP debug information (for testing) || 1 = errors and messages || 2 = messages only
    $mail->SMTPAuth  = true; // enable SMTP authentication
    $mail->Host      = "smtp.gmail.com"; // sets the SMTP server
    $mail->SMTPSecure = 'ssl';
    $mail->IsHTML(true);
    $mail->Port      = 465; // set the SMTP port for the GMAIL server
    $mail->Username  = "my_email@gmail.com"; // SMTP account username
    $mail->Password  = "password1"; // SMTP account password
    $mail->SetFrom("admin@easyipn.us", project_name." Admin");
    $mail->AddAddress($to);
    $mail->Subject = $subject;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address, "Buyer@".project_name);

    // Append a new person to the file
    $current .= "About to send mail\n";
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
    http_response_code(200); // PHP 5.4 or greater
}


http_response_code(200); // PHP 5.4 or greater
?>

As you can see, I'm trying to send mail using PHPMailer upon a successfull payment.

When I use the Stripe 'Send a test event to a webhook endpoint', it shows me the response of : `
Fatal error: Uncaught Error: Class 'Stripe_Event' not found in /storage/ssd4/819/5144819/public_html/ipn/stripe/stripe.php:30 Stack trace:

0 {main}

thrown in /storage/ssd4/819/5144819/public_html/ipn/stripe/stripe.php on line 30
saying that Stripe_Event isn't found.

I'm calling the init.php file which should be loading everything. Am I calling stripe_event wrong or what ?

Community
  • 1
  • 1
Benza
  • 175
  • 1
  • 15
  • \Stripe\Stripe_Event? – lollmbaowtfidgafgtfoohwtbs Mar 24 '18 at 13:36
  • @lollmbaowtfidgafgtfoohwtbs still getting this error : `
    Fatal error: Uncaught Error: Class 'Stripe\Stripe_Event' not found in /storage/ssd4/819/5144819/public_html/ipn/stripe/stripe.php:30 Stack trace: #0 {main} thrown in /storage/ssd4/819/5144819/public_html/ipn/stripe/stripe.php on line 30
    `
    – Benza Mar 24 '18 at 13:39
  • your code seems to be missing the Strip_Event class. You said it worked somewhere else, but doesnt work in another place? Are you using this?: github.com/stripe/stripe-php/tree/master/lib If so, there is no Stripe_Event to use. What are you trying to do exactly? Didnt you already retrieve the event data up above the Strip_Event code...a couple lines up? – lollmbaowtfidgafgtfoohwtbs Mar 25 '18 at 05:43

0 Answers0