1

I have tried a lot with https://github.com/bigcommerce/bigcommerce-api-php, I am able to create an connection successfully with Basic Auth

Bigcommerce::configure(array(
    'store_url' => 'https://store.mybigcommerce.com',
    'username'  => 'admin',
    'api_key'   => 'd81aada4xc34xx3e18f0xxxx7f36ca'
));

but when i try with "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'
));

It shows this error:

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

Can somebody please help me? How can i use webhooks of bigcommerce in php?

creyD
  • 1,972
  • 3
  • 26
  • 55
Deepak Kumar
  • 31
  • 1
  • 5
  • 1
    Looks like you have the explanation on the error it self. `$request` is not in scope. Depending on which framework you use , it would change who you access the $_REQUEST params. This has nothing to do with webhook registrations. – Gayan Hewa May 22 '17 at 13:45

1 Answers1

1

Below is sample function that is used on Laravel. This gets triggered once the app gets authorised. The $request->get('code') can be replaced with $_REQUEST['code'] since they are purely request params.

Webhook registration begins in the try catch block once the Bigcommerce::configure step completes.

 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();
    }

}
Gayan Hewa
  • 2,277
  • 4
  • 22
  • 41