PHP code example of bnmetrics / laravel-shopify-api

1. Go to this page and download the library: Download bnmetrics/laravel-shopify-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bnmetrics / laravel-shopify-api example snippets


"providers" => [
   // other providers...
   BNMetrics\Shopify\ShopifyServiceProvider::class,
]

"aliases" => [
   // other facades...
   'Shopify' => BNMetrics\Shopify\Facade\ShopifyFacade::class,
] 



namespace App\Http\Controllers;

use Shopify;

Class myShopify extends Controller
{
  protected $shop = "example.myshopify.com";
  protected $foo;
  protected $scopes = ['read_products','read_themes'];
  
  public function getPermission()
  {
    $this->foo = Shopify::make($this->shop, $this->scopes);
    return $this->foo->redirect();
  }
  
  public function getResponse(Request $request)
  {
    $this->getPermission();
    
    // Get user data, you can store it in the data base
    $user = $this->foo->auth()->getUser();
    
    //GET request to products.json
    return $this->foo->auth()->get('products', ['fields'=>'id,images,title']);
  }
}

$this->foo = Shopify::retrieve($this->shop, $access_token);

//Get the user information
$user = $this->foo->getUser();

Route::get('/oauth/authorize', 'myshopify@getResponse');
Route::get('/shopify', 'myShopify@getPermission');

//assuming $shop is already oAuth verified Shopify.php object

//GET request
$shop->getProductsAll();
$shop->getProductsById(2324564);


//POST Request
//Options to be passed as request body are manditory for some API endpoints
$options = [
'product' => [
    'title' => 'my cool products',
    'body_html' => '<p> my cool product! </p>',
    'vendor' => 'My Shopify Shop',
    'images' => [
         'src' => 'https://example.com/my_product.jpg'
    ],
  ]
];
$shop->createProducts($options);

//PUT (upload/update an image source)
$modifyOptions = [
   'asset' => [
       'key' => 'assets/example.jpg',
       'src' => 'https://www.example.com/example.jpg'
   
   ]
];
$shop->modifyThemesAssets($modifyOptions);



//DELETE
$productID = 121421;
$imageID = 323546;
$shop->deleteProductsImages($productID, $imageID);

"providers" => [
   // other providers...
   BNMetrics\Shopify\BillingServiceProvider::class,
]

"aliases" => [
   // other facades...
   'ShopifyBilling' => BNMetrics\Shopify\Facade\BillingFacade::class,
] 

Route::get('activate', 'myShopify@activate');

  public function getResponse(Request $request)
  {
    $this->getPermission();
    
    // Get user data, you can store it in the data base
    $user = $this->foo->auth()->getUser();
    
    $return_url = url('activate');
    $options = ['name'=>'my awesome App', 'price' => '10',
                'return_url' => $return_url,
                ];
    
    // Redirect the user to the myshopify page to approve the charge
    //Saving user into the session for the activate method
    return \ShopifyBilling::driver('RecurringBilling')
                            ->create($this->user, $options)->redirect()->with('user', $this->user);      
  }

public function activated(Request $request)
{
    $user = $request->session()->get('user');
    $activated = \ShopifyBilling::driver('RecurringBilling')
            ->activate($user->name, $user->token, $request->get('charge_id'));
    
    return redirect('/myapp-homepage');
}

public function activated(Request $request)
{
    $user = $request->session()->get('user');
    $activated = \ShopifyBilling::driver('RecurringBilling')
            ->activate($user->name, $user->token, $request->get('charge_id'));
    
    $usageOption = [ 'price' => '10', 'description' => 'my awesome app description'];
    
    \ShopifyBilling::driver('UsageCharge')->setBase($activated)->create($user, $usageOption);
    
    return redirect('/myapp-homepage');

}
bash
php artisan vendor:publish --provider='BNMetrics\Shopify\ShopifyServiceProvider'