Search code examples
phpwoocommerceyii2webhooks

Woocommerce webhook to Yii2 API returns 401


i try to receive modification of stock product when a product is bought on a woocommerce website via a webhook on woocommerce to my yii2 app.

So far i receive an 401 error.

for info, i try to connect with an access_token. With postman and with woocommerce i get the 401 error but no more data, i don't understand how i should connect.

Also, i have auttomatic/woocommerce on my Yii2 App, as the other way, i succeed to update woocommerce products from yii2 :)

this is my api controller :

<?php
namespace app\modules\api\controllers;
use Yii;
use yii\rest\ActiveController;
//use yii\web\Response;
use yii\filters\auth\HttpBearerAuth;
use app\modules\api\resources\Cors;
use app\modules\api\resources\ProductsResource;

class ProductsController extends ActiveController
{
  public $modelClass = ProductsResource::class;
  Public $enableCsrfValidation = false;
  public static function allowedDomains() {
      return [$_SERVER["REMOTE_ADDR"], '*'];
  }

  public function behaviors()
  {
        $behaviors = parent::behaviors();
        $behaviors['authenticator']['except'] = ['options'];
        $auth = $behaviors['authenticator'];
        $auth['authMethods'] = [
             HttpBearerAuth::class
        ];

        unset($behaviors['authenticator']);
        $behaviors['authenticator'] = $auth;
          return array_merge($behaviors, [
            'corsFilter'  => [
              'class' => Cors::className(),
              'cors'  => [
                // restrict access to domains:
                'Origin'                           => static::allowedDomains(),
                'Access-Control-Request-Method' => ['GET','HEAD','POST','PUT','PATCH', 'OPTIONS'],
                'Access-Control-Allow-Credentials' => false,
                'Access-Control-Max-Age'           => 3600,                 // Cache (seconds)
                'Access-Control-Allow-Headers' => ['origin','authorization', 'X-CSRF-TOKEN','X-Requested-With','X-Auth-Token','content-type'],
                'Access-Control-Request-Headers' => ['*'],
               'Access-Control-Expose-Headers' => []
              ],
            ],
            'verbs' => [
                'class' => \yii\filters\VerbFilter::className(),
                'actions' => [
                    'Stock_Interne_API' => ['POST','PUT'], // Only allow POST requests for the 'webhook' action
                ],
            ],
          ]);
          return $behaviors;
  }

  protected function verbs()
  {
      return [
          'index' => ['GET', 'HEAD','POST','PUT'],
          'view' => ['GET', 'HEAD'],
          'create' => ['POST'],
          'update' => ['PUT', 'PATCH'],
          'delete' => ['DELETE'],
      ];
  }

  public function actions()
  {
      //  Yii::$app->response->statusCode = 200;
        $actions = parent::actions();
        //$actions['create']['prepareDataProvider'] = [$this, 'prepareDataProvider'];
        // disable the "delete" and "create" actions
      //  unset($actions['delete'], $actions['update'], $actions['index']);
         unset($actions['create']);
         unset($actions['update']);
         unset($actions['index']);
         unset($actions['view']);
        return $actions;
  }
  public function actionIndex(){
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

     $data = Yii::$app->request->post();

    return 'ok';
  }  public function actionView(){
      \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

       $data = Yii::$app->request->post();

      return 'ok';
    }
    public function actionCreate(){
      \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

       $data = Yii::$app->request->post();

      return 'ok';
    }
  public function actionUpdate(){
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

     $data = Yii::$app->request->post();

    return 'ok';
  }


}

i would first to be able to get the correct return from the controller, then i will check what data i get and at last work on the function to update the stock on the yii2 app, but i need to be able to receive the data from the webhook before ^^


Solution

  • Well, i've tried several things and did not find the solution, but, as it works with other apps i work for, i think i did something wrong. I will delete my code and do it again.