我正在尝试在我的本地环境中测试paypalwebhook;具体来说,当用户取消订阅时,我会给自己(管理员)发送一封电子邮件。
我使用ngrok生成一个url(类似于
https://d044ce9878a1.ngrok.io
https://d044ce9878a1.ngrok.io?paypal_listener
我的应用程序。
<?php
namespace memberships;
use Exception;
use mailer\NotifyAdmin;
class Webhooks {
/**
* Listens to Paypal's webhooks
*/
public function __construct() {
// Bail out if the incoming request isn't marked with the "paypal_listener" param
if( ! isset( $_GET[ "paypal_listener" ] ) ) {
return;
}
// Prepare to send an email to the administrator
$this->NotifyAdmin = new NotifyAdmin();
// Get the incoming webhook
$input = file_get_contents( "php://input" );
$data = json_decode( $input );
switch( $data->event_type ) {
case "BILLING.SUBSCRIPTION.CANCELLED":
$this->on_subscription_canceled( $data );
break;
}
}
/**
* Sends an email to the user confirming that their subscription was canceled
*/
private function on_subscription_canceled( $data ) {
// For testing purposes, I'm sending an email to the administrator as if an error occurred
$this->NotifyAdmin->setErrMessage( "The webhook worked!" );
$this->NotifyAdmin->send_email();
}
}
如果我模拟通过开发人员仪表板发送一个webhook事件,那么代码可以工作,并通过电子邮件将消息发送给管理员
"The webhook worked!"
已成功发送。
在本地,我的(React)应用程序位于
localhost:3000
. 我使用沙盒用户帐户注册和创建贝宝订阅;这一切都很好,如果我登录到沙盒贝宝仪表板,我可以看到订阅已成功创建并处于活动状态。如果我从Paypal仪表板取消订阅,订阅将成功取消,但webhook不起作用我无法获取
“网络钩子起作用了!”
所以问题是,一旦我验证了Paypal可以向
https://d044ce9878a1.ngrok.io?贝宝监听器
(因为webhook模拟器可以工作),难道其他所有的东西都能正常工作吗?或者我错过了其他需要采取的步骤吗?