bnmetrics/laravel-shopify-api

laravel的Shopify API包装器

1.0.5 2018-03-19 17:22 UTC

This package is not auto-updated.

Last update: 2024-09-20 06:04:37 UTC


README

Build Status Latest Stable Version

此包为您提供了使用Laravel 5构建Shopify应用的便捷方式。OAuth身份验证在Laravel的Socialite基础上扩展。

此包支持公共和私有应用,包括计费。

安装

您可以通过以下方式使用composer安装此包:

composer require bnmetrics/laravel-shopify-api

或者将以下内容添加到您的Laravel项目中的composer.json文件中

"require": {

    "bnmetrics/laravel-shopify-api" : "~1.0",
}

要发布shopify.php配置文件到app/config,运行

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

配置

在您的.env文件中设置shopify环境变量

SHOPIFY_KEY=YOUR_API_KEY_HERE
SHOPIFY_SECRET=YOUR_API_SECRET_HERE
SHOPIFY_REDIRECT=https://app.example.com/oauth/authorize

在您的app/config/app.php中注册服务提供者

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

此外,在app/config/app.php中的aliases数组中添加Shopify外观

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

基本用法

现在,您已经准备好创建一个shopify应用了!以下是一个示例,展示如何在控制器中使用它通过GET请求从API端点获取信息:

<?php

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']);
  }
}

或者,如果您已经有一个特定shopify域的token,您可以通过以下方式使用retrieve()方法获取API响应:

$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');

对API端点的CRUD请求

截至版本1.0.3,此包支持GET、PUT、POST、DELETE请求。要发出请求,只需调用相应的方法,并遵循每个端点的URL结构,添加后缀如"All"、"Count"、"ById"。

API端点被称为"层",例如:

  • 产品端点('admin/products.json', 'admin/products/{id}.json')是第1层;

  • 产品图片端点('admin/products/{product_id}/images.json', 'admin/products/{product_id}/images/{image_id}.json')是第2层;

  • 目前,只有ordersFulfillmentsEvents有第3层('admin/orders/{order_id}/fulfilments/{fulfillment_id}/events.json', 'admin/orders/{order_id}/fulfilments/{fulfillment_id}/events/{event_id}.json');

以下是如何向端点发出请求的示例

//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);

配置文件

还可以按照相同的模式将更多端点添加到shopify.php配置文件的$endpoints数组中。

  • 第一层名称应该是第一维的数组键,数组值是第2层;
  • 如果端点有一个第3层端点,添加键'tier 3'
  • 如果第2层端点节点不需要第1层ID来访问,则可以在'tierTwoWithoutId'数组中指示端点。

计费

截至版本1.0.2,此包已添加计费支持。它支持Shopify提供的所有三种计费选项:RecurringCharge、ApplicationCharge和UsageCharge。

如果您要为Shopify应用使用计费,请将以下内容添加到您的config/app.php文件中

在您的app/config/app.php中注册服务提供者

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

此外,在app/config/app.php中的aliases数组中添加ShopifyBilling外观

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

计费使用

由于所有Shopify费用都需要在创建后激活,我们将添加一个名为activate的路由

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

对于我们之前的示例控制器,您可以修改getResponse方法如下

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

create()方法接受一个经过验证的Shopify对象或用户对象,以及费用选项。请参阅属性部分了解您可以传递的不同选项。

周期性计费应用费用使用费用

然后我们需要添加激活方法()

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');

}