我正在努力尝试将模型加载到组件中。
我正在将Stripe订阅处理集成到Cakephp 3.3应用程序中,目前我正致力于捕获和处理webhooks。我正在做的大部分工作都运行良好,但是当我尝试在SubscriptionComponent中加载“用户”和“计划”模型时,我遇到了麻烦。
以下是发送测试webhook时应用程序返回Stripe的错误消息:
<response>
<message>Call to undefined method App\Controller\Component\SubscriptionComponent::loadModel()</message>
<url>/subscriptions/stripeHook</url>
<code>500</code>
</response>
这是SubscriptionsController的缩写版本:
namespace App\Controller;
use App\Controller\AppController;
use App\Controller\Component;
use App\Controller\Users;
use App\Model\Table\PlansTable;
use App\Model\Table\UsersTable;
use App\Model\Entity\Plan;
use App\Model\Entity\User;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Mailer\MailerAwareTrait;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
class SubscriptionsController extends AppController
{
use MailerAwareTrait;
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
// allow access to stripeHook without needing a user to be logged in
$this->Auth->allow(['stripeHook']);
// $this->viewBuilder()->layoutPath('App')->layout('default');
}
public function stripeHook()
{
if ($this->request->is('post'))
{
\Stripe\Stripe::setApiKey(Configure::read('Stripe.secret'));
$this->autoRender = false;
$input = @file_get_contents('php://input');
$event = json_decode($input);
try
{ // Check against Stripe to confirm that the ID is valid
$event = \Stripe\Event::retrieve($event->id);
$handlerName = Inflector::variable(str_replace(".", "_", $event->type)) . "Hook";
if (method_exists($this, $handlerName))
{
$status = $this->$handlerName($event);
$log_message = 'StripeHOOK: ' . $status . var_export($event);
$this->log($log_message,'info');
echo var_dump($status);
}
else
{
echo 'Not interested: ' . $handlerName;
}
}
catch (Exception $e)
{
$log_message = 'StripeHOOK - No event found for: ' . $event->id;
$this->log($log_message,'info');
}
}
http_response_code(200);
}
public function customerSubscriptionCreatedHook($event)
{
return $this->customerSubscriptionUpdatedHook($event);
}
public function customerSubscriptionUpdatedHook($event)
{
$this->loadComponent('Subscription');
$status = false;
$result = $this->Subscription->updateSubscription($event->data->object);
return $status;
}
这是(也缩写)SubscriptionComponent:
namespace App\Controller\Component;
use App\Model\Table\PlansTable;
use App\Model\Table\UsersTable;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
class SubscriptionComponent extends Component
{
protected $_defaultConfig = [];
public function updateSubscription($stripePlan)
{
//
// @NOTE: This appears to be where the error is raised
//
$this->loadModel('Plans');
$this->loadModel('Users');
$status = false;
$customer = $stripePlan->customer;
$plan = $stripePlan->plan->id;
$user = $this->Users->find('all', ['conditions' => ['User.stripe_id' => $customer]]);
$user = $user->first();
$plan = $this->Plans->find('all', ['conditions' => ['Plan.stripe_id' => $plan]]);
$plan = $plan->first();
return $status;
}
}
我还在学习CakePhp,所以请耐心等待我:-)我确信这很简单 - 对我来说还不是很明显。
任何帮助都将非常感谢! THX
答案 0 :(得分:1)
尝试使用 TableRegistry
namespace App\Controller\Component;
...
use Cake\ORM\TableRegistry; // <----
class SubscriptionComponent extends Component
{
protected $_defaultConfig = [];
public function updateSubscription($stripePlan)
{
$this->Users = TableRegistry::get('Users'); // <----
$this->Plans = TableRegistry::get('Plans'); // <----
...
$user = $this->Users->find('all', ['conditions' => ['User.stripe_id' => $customer]]);
$user = $user->first();
$plan = $this->Plans->find('all', ['conditions' => ['Plan.stripe_id' => $plan]]);
$plan = $plan->first();
...
}
}