如何在bigcommerce中使用webhooks?

时间:2017-05-22 12:28:24

标签: php oauth webhooks bigcommerce

我用https://github.com/bigcommerce/bigcommerce-api-php做了很多尝试, 我能够使用Basic Auth

成功创建连接
Bigcommerce::configure(array(
    'store_url' => 'https://store.mybigcommerce.com',
    'username'  => 'admin',
    'api_key'   => 'd81aada4xc34xx3e18f0xxxx7f36ca'
));

但是当我尝试使用“OAuth”

In order to obtain the auth_token you would consume Bigcommerce::getAuthToken method


$object = new \stdClass();
$object->client_id = 'xxxxxx';
$object->client_secret = 'xxxxx;
$object->redirect_uri = 'https://app.com/redirect';
$object->code = $request->get('code');
$object->context = $request->get('context');
$object->scope = $request->get('scope');

$authTokenResponse = Bigcommerce::getAuthToken($object);

Bigcommerce::configure(array(
    'client_id' => 'xxxxxxxx',
    'auth_token' => $authTokenResponse->access_token,
    'store_hash' => 'xxxxxxx'
));

显示此错误:

Notice: Undefined variable: request in /var/www/html/tests/bigcommerce/test/index.php on line 25 Fatal error: Call to a member function get() on a non-object in /var/www/html/tests/bigcommerce/test/index.php on line 25

有人可以帮帮我吗?我怎样才能在php中使用bigcommerce的webhook?

1 个答案:

答案 0 :(得分:1)

以下是Laravel上使用的示例函数。一旦应用程序获得授权,就会触发此操作$request->get('code')可以替换为$_REQUEST['code'],因为它们纯粹是请求参数。

一旦Bigcommerce::configure步骤完成,Webhook注册就会在try catch块中开始。

 public function onAppAuth(Request $request)
 {
    $object = new \stdClass();
    $object->client_id = env('BC_CLIENT_ID');
    $object->client_secret = env('BC_CLIENT_SECRET');
    $object->redirect_uri = env('BC_CALLBACK_URL');
    $object->code = $request->get('code');
    $object->context = $request->get('context');
    $object->scope = $request->get('scope');
    Bigcommerce::useJson();

    $authTokenResponse = Bigcommerce::getAuthToken($object);

    // configure BC App
    Bigcommerce::configure([
        'client_id' => env('BC_CLIENT_ID'),
        'auth_token' => $authTokenResponse->access_token,
        'store_hash' => explode('/', $request->get('context'))[1]
    ]);

    Bigcommerce::verifyPeer(false);

    try {
        $store_hash =  explode('/', $request->get('context'))[1];

        // register webhook
        Bigcommerce::createWebhook([
                "scope" => "store/order/created",
                "destination" => env('APP_URL')."api/order/process",
                "is_active" => true
            ]);

        return view('thankyou');

    } catch(Error $error) {
        echo $error->getCode();
        echo $error->getMessage();
    }

}