【问题标题】:Parsing Error on if $event->type stripe webhookif $event->type stripe webhook 解析错误
【发布时间】:2015-05-30 21:26:32
【问题描述】:

我正在使用以下代码作为条带 webhook 从 phpmailer 向用户发送 cutsom 电子邮件。来自this gist

我似乎无法弄清楚为什么会出现这个错误,我很少使用 php,所以我确定这是我不熟悉的东西,这是我收到的错误。

解析错误:语法错误,意外的 '$event' (T_VARIABLE),在第 24 行的 /usr/share/nginx/html/emailReceipt.php 中需要 '('

代码

<?php

// SETUP:
// 1. Customize all the settings (stripe api key, email settings, email text)
// 2. Put this code somewhere where it's accessible by a URL on your server.
// 3. Add the URL of that location to the settings at https://manage.stripe.com/#account/webhooks
// 4. Have fun!

// set your secret key: remember to change this to your live secret key in production
// see your keys here https://manage.stripe.com/account
//Stripe::setApiKey("YOUR STRIPE SECREY KEY");
Stripe::setApiKey("sk_test_WggOmz2smwT9nGdx7aos2R5Q");


// retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);

// for extra security, retrieve from the Stripe API
$event_id = $event_json->id;
$event = Stripe_Event::retrieve($event_id);

// This will send receipts on succesful invoices
if $event->type == 'invoice.payment_succeeded' {
  email_invoice_receipt(event->data->object);
}

function email_invoice_receipt($invoice) {
  $customer = Stripe_Customer::retrieve($invoice->customer);

  //Make sure to customize your from address
  $subject = 'Your payment has been received';
  $headers = 'From: "MyApp Support" <support@myapp.com>';

  mail($customer->email, $subject, message_body(), $headers);
}

function format_stripe_amount($amount) {
  return sprintf('$%0.2f', $amount / 100.0);
}

function format_stripe_timestamp($timestamp) {
  return strftime("%m/%d/%Y", $timestamp);
}

function payment_received_body($invoice, $customer) {
  $subscription = $invoice->lines->subscriptions[0];
  return <<'EOF'
Dear {$customer->email}:

This is a receipt for your subscription. This is only a receipt,
no payment is due. Thanks for your continued support!

-------------------------------------------------
SUBSCRIPTION RECEIPT

Email: {$customer->email}
Plan: {$subscription->plan->name}
Amount: {format_stripe_amount($invoice->total)} (USD)

For service between {format_stripe_timestamp($subscription->period->start)} and {format_stripe_timestamp($subscription->period->end)}

-------------------------------------------------

EOF;
}

?>

【问题讨论】:

标签: php stripe-payments webhooks


【解决方案1】:

您的if 语句中缺少大括号:

if $event->type == 'invoice.payment_succeeded'

应该是:

if ($event->type == 'invoice.payment_succeeded')

【讨论】:

  • 这也改变了错误Parse error: syntax error, unexpected '-&gt;' (T_OBJECT_OPERATOR)
  • 第 25 行,下一行。
  • email_invoice_receipt(event-&gt;data-&gt;object); 至少缺少$ - email_invoice_receipt($event-&gt;data-&gt;object);,我认为应该是这样。看起来像$event = Stripe_Event::retrieve($event_id);@TomSawyer
  • @TomSawyer 你应该看看基本的 php 语法,你的变量名缺少$
  • @TomSawyer 是的,我现在只是在讨论 return &lt;&lt;'EOF' 删除引号,应该有 3 倍的 &lt;,根据heredoc php.net/manual/en/… 上的手册@ - return &lt;&lt;&lt;EOF
猜你喜欢
  • 2021-11-28
  • 2021-07-14
  • 2020-10-30
  • 2019-03-03
  • 2020-10-14
  • 2017-06-08
  • 2016-03-09
  • 2021-04-03
  • 1970-01-01
相关资源
最近更新 更多