定制 oseintow/laravel-shopify 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oseintow/laravel-shopify

Composer 安装命令:

composer require oseintow/laravel-shopify

包简介

Laravel shopify package

README 文档

README

Laravel Shopify is a simple package which helps to build robust integration into Shopify.

Installation

Add package to composer.json

composer require oseintow/laravel-shopify

Laravel 5.5+

Package auto discovery will take care of setting up the alias and facade for you

Laravel 5.4 <

Add the service provider to config/app.php in the providers array.

<?php

'providers' => [
    ...
    Oseintow\Shopify\ShopifyServiceProvider::class,
],

Setup alias for the Facade

<?php

'aliases' => [
    ...
    'Shopify' => Oseintow\Shopify\Facades\Shopify::class,
],

Configuration

Laravel Shopify requires connection configuration. You will need to publish vendor assets

php artisan vendor:publish

This will create a shopify.php file in the config directory. You will need to set your API_KEY and SECRET

Usage

To install/integrate a shop you will need to initiate an oauth authentication with the shopify API and this require three components.

They are:

1. Shop URL (eg. example.myshopify.com)
2. Scope (eg. write_products, read_orders, etc)
3. Redirect URL (eg. http://mydomain.com/process_oauth_result)

This process will enable us to obtain the shops access token

use Oseintow\Shopify\Facades\Shopify;

Route::get("install_shop",function()
{
    $shopUrl = "example.myshopify.com";
    $scope = ["write_products","read_orders"];
    $redirectUrl = "http://mydomain.com/process_oauth_result";

    $shopify = Shopify::setShopUrl($shopUrl);
    return redirect()->to($shopify->getAuthorizeUrl($scope,$redirectUrl));
});

Let's retrieve access token

Route::get("process_oauth_result",function(\Illuminate\Http\Request $request)
{
    $shopUrl = "example.myshopify.com";
    $accessToken = Shopify::setShopUrl($shopUrl)->getAccessToken($request->code);

    dd($accessToken);
    
    // redirect to success page or billing etc.
});

To verify request(hmac)

public function verifyRequest(Request $request)
{
    $queryString = $request->getQueryString();

    if(Shopify::verifyRequest($queryString)){
        logger("verification passed");
    }else{
        logger("verification failed");
    }
}

To verify webhook(hmac)

public function verifyWebhook(Request $request)
{
    $data = $request->getContent();
    $hmacHeader = $request->server('HTTP_X_SHOPIFY_HMAC_SHA256');

    if (Shopify::verifyWebHook($data, $hmacHeader)) {
        logger("verification passed");
    } else {
        logger("verification failed");
    }
}

To access API resource use

Shopify::get("resource uri", ["query string params"]);
Shopify::post("resource uri", ["post body"]);
Shopify::put("resource uri", ["put body"]);
Shopify::delete("resource uri");

Let use our access token to get products from shopify.

NB: You can use this to access any resource on shopify (be it Product, Shop, Order, etc)

$shopUrl = "example.myshopify.com";
$accessToken = "xxxxxxxxxxxxxxxxxxxxx";
$products = Shopify::setShopUrl($shopUrl)->setAccessToken($accessToken)->get("admin/products.json");

To pass query params

// returns Collection
$shopify = Shopify::setShopUrl($shopUrl)->setAccessToken($accessToken);
$products = $shopify->get("admin/products.json", ["limit"=>20, "page" => 1]);

Controller Example

If you prefer to use dependency injection over facades like me, then you can inject the Class:

use Illuminate\Http\Request;
use Oseintow\Shopify\Shopify;

class Foo
{
    protected $shopify;

    public function __construct(Shopify $shopify)
    {
        $this->shopify = $shopify;
    }

    /*
    * returns Collection
    */
    public function getProducts(Request $request)
    {
        $products = $this->shopify->setShopUrl($shopUrl)
            ->setAccessToken($accessToken)
            ->get('admin/products.json');

        $products->each(function($product){
             \Log::info($product->title);
        });
    }
}

Miscellaneous

To get Response headers

Shopify::getHeaders();

To get specific header

Shopify::getHeader("Content-Type");

Check if header exist

if(Shopify::hasHeader("Content-Type")){
    echo "Yes header exist";
}

To get response status code or status message

Shopify::getStatusCode(); // 200
Shopify::getReasonPhrase(); // ok

oseintow/laravel-shopify 适用场景与选型建议

oseintow/laravel-shopify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34.45k 次下载、GitHub Stars 达 71, 最近一次更新时间为 2016 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 oseintow/laravel-shopify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 oseintow/laravel-shopify 我们能提供哪些服务?
定制开发 / 二次开发

基于 oseintow/laravel-shopify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 34.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 73
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 71
  • Watchers: 9
  • Forks: 41
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-13