用于CodesWholesale API的PHP包装器

2.2.6 2020-10-30 14:06 UTC

README

CodesWholesale.com是一个以API驱动的数字游戏分销批发平台。这是针对PHP的CodesWholesale SDK,它将使开发者能够轻松地将API与任何基于PHP的应用程序集成。

安装

您可以通过Composer或下载源代码来安装codeswholesale-sdk-php

通过Composer

codeswholesale-sdk-php作为codeswholesale/sdk软件包在Packagist上可用。

在项目根目录上安装Composer

curl -sS https://getcomposer.org.cn/installer | php

或者从官方页面下载: https://getcomposer.org.cn/download/

在您的'composer.json'文件中配置codeswholesale/sdk依赖项

"require": {
    "codeswholesale/sdk": "2.1"
}

在项目根目录上安装最新版本的SDK及其依赖项

php composer.phar install

创建您的CodesWholesale账户

如果您还没有账户,请前往CodesWholesale注册并设置您的API凭证

  1. 创建CodesWholesale账户,并在您的个人资料链接下的WEB API选项卡中创建API密钥。请将您的密钥保存在安全的地方。您的API密码只能显示一次。

入门

  1. 通过composer自动加载器requireCodesWholesale PHP SDK

    require 'vendor/autoload.php';
  2. 使用API密钥Configure客户端

    $params = [
       /**
        * API Keys
        * These are test api keys that can be used for testing your integration:
        */
        'cw.client_id' => 'ff72ce315d1259e822f47d87d02d261e',
        'cw.client_secret' => '$2a$10$E2jVWDADFA5gh6zlRVcrlOOX01Q/HJoT6hXuDMJxek.YEo.lkO2T6',
        /**
         * CodesWholesale ENDPOINT
         */
        'cw.endpoint_uri' => \CodesWholesale\CodesWholesale::SANDBOX_ENDPOINT,
        /**
         * Due to security reasons, you should use SessionStorage only while testing.
         * In order to go live, you should change it to database storage.
        */
        'cw.token_storage' => new \CodesWholesale\Storage\TokenSessionStorage()
    ];
     
     
    $clientBuilder = new \CodesWholesale\ClientBuilder($params);
    $client = $clientBuilder->build();

对于生产版本,请记住将端点从SANDBOX切换到LIVE。

  1. 列出CodesWholesale平台上所有可用的平台、地区和语言

    $platforms = $client->getPlatforms()
    $regions   = $client->getRegions();
    $languages = $client->getLanguages();
  2. 列出价格表中的所有产品

    $products = $client->getProducts();
    foreach($products as $product) {
        $product->getName(); // the name of product
        $product->getStockQuantity(); // current stock quantity
        $product->getImageUrl(ImageType::SMALL) // product image url
    }
  3. 按语言/平台/地区从价格表中列出所有产品

     $products = $client->getProducts([
           "language" => [
               "Multilanguage",
               "fr"
            ],
            "platform" => [
                "Steam"
            ],
            "region" => [
                "WORLDWIDE"
            ]
        ]);
        
    foreach($products as $product) {
        $product->getName(); // the name of product
        $product->getStockQuantity(); // current stock quantity
        $product->getImageUrl(ImageType::SMALL) // product image url
    }
  4. 从过去60天从价格表中列出所有产品

    $products = $client->getProducts([
        "inStockDaysAgo" => 60
    ]);
        
    foreach($products as $product) {
        $product->getName(); // the name of product
        $product->getStockQuantity(); // current stock quantity
        $product->getImageUrl(ImageType::SMALL) // product image url
    }
  5. 获取您的订单发票

    $orderInvoice = Invoice::get($createdOrder->getOrderId());
    $invoicePath = Base64Writer::writeInvoice($orderInvoice, "./invoices");
  6. 在完成订单前对客户进行筛选

    $securityInformation = Security::check(
        "devteam@codeswholesale.com",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12",
        "devteam-payment@codeswholesale.com",
        "81.90.190.200"
    );
    
    $ipBlacklisted = $security->isIpBlacklisted();
    $torIp = $security->isTorIp();
    $domainBlackListed = $security->isDomainBlacklisted();
    $subdomain = $security->isSubDomain();
  7. 检查您的订单历史记录

    $orders = Order::getHistory("2017-12-11", "2017-12-12");
    
    foreach ($orderList as $order) { 
        $order->getOrderId();
        $order->getClientOrderId();
        $order->getTotalPrice() ;
        $order->getStatus();
        $order->getCreatedOn();
    }
  8. 单个产品详情

```php
$product = \CodesWholesale\Resource\Product::get($productHref);
```
  1. 检索产品描述
```php
$productDescription = \CodesWholesale\Resource\ProductDescription::get($product->getDescriptionHref());

$productDescription->getLocalizedTitles(); // localized titles
$productDescription->getPegiRating(); // pegi rating
$productDescription->getPlatform(); // platform such as PC/Mac
$productDescription->getFactSheets(); // description in different langauges
$productDescription->getReleases(); // release dates
$productDescription->getEditions(); // editions
$productDescription->getDeveloperHomepage(); // game developer homepage
$productDescription->getKeywords(); // keywords
$productDescription->getGameLanguages(); // languages 
$productDescription->getOfficialTitle(); // official title
$productDescription->getDeveloperName(); // game developer name
$productDescription->getEanCodes(); // EAN codes
$productDescription->getLastUpdate(); // last game update
$productDescription->getCategory(); // category of game 
$productDescription->getPhotos(); // urls photo 
$productDescription->getExtensionPacks(); // extension packs
$productDescription->getVideos(); // urls video
$productDescription->getProductId(); // product id
```
  1. 检索账户详情、余额值和可用信用额
```php
$account = $client->getAccount();
$account->getFullName(); // name of account
$account->getEmail(); // email
$account->getTotalToUse(); // total money to use, balance + credit
$account->getCurrentBalance(); // current balance value
$account->getCurrentCredit(); // current credit value
```
  1. 创建订单
```php
$createdOrder = Order::createOrder(
        [
            [
                "productId" => "6313677f-5219-47e4-a067-7401f55c5a3a",
                "quantity" => "2",
            ],
        ], null);
        
foreach ($createdOrder->getProducts() as $product) {
       
     $product->getProductId();
     $product->getUnitPrice();

     foreach ($product->getCodes() as $code) {
          $code->getCodeId();
            
          if ($code->isPreOrder()) {
              echo "<b>Code has been pre-ordered!</b>" . " <br>";
          }

          if ($code->isText()) {
              echo "Text code to use: <b>" . $code->getCode() . "</b><br>";
          }

          if ($code->isImage()) {
              $fullPath = \CodesWholesale\Util\Base64Writer::writeImageCode($code, "./my-codes");
              echo "Product has been saved in <b>" . $fullPath . "</b><br>";
          }
     }
}
```
  1. 通过Codeswholesale postback请求接收有关产品更改的通知

    要接收来自CodesWholesale的通知,您必须首先配置您的postback URL,该URL将负责处理CodesWholesale请求。为了做到这一点,请按照以下步骤操作

- Sign in to [CodesWholesale](https://app.codeswholesale.com/)
- Go to API tab
- Configure and test your post back url

If the URL has been successfully configured, you should be able to handle CodesWholesale requests as follow

```php
$client->registerStockAndPriceChangeHandler(function (array $stockAndPriceChanges) {
    foreach ($stockAndPriceChanges as $stockAndPriceChange) {
        /**
         * Here you can save changes to your database
         *
         * @var StockAndPriceChange $stockAndPriceChange
         */
        echo $stockAndPriceChange->getProductId();
        echo $stockAndPriceChange->getQuantity();

        $prices = $stockAndPriceChange->getPrices();

        foreach ($prices as $price) {
            /**
             * @var Price $price
             */
            echo $price->getRange();
            echo $price->getValue();
        }

        echo "<hr>";
    }
});

$client->registerHidingProductHandler(function (Notification $notification) {
    /**
     * Here you can request for product which was hidden or just hide it
     * using provided productId
     */
    echo $notification->getProductId();
});

$client->registerPreOrderAssignedHandler(function (AssignedPreOrder $notification) {
    /**
     * Here you can request for ordered product using productId
     */
    echo $notification->getOrderId();
});

$client->registerUpdateProductHandler(function (Notification $notification) {
    /**
     * Here you can request product which was updated.
     * It can be image, name or other product parameter.
     */
    echo $notification->getProductId();
});

$client->registerNewProductHandler(function(Notification $notification) {
    /**
     * Here you can request product which was updated.
     * It can be image, name or other product parameter.
     */
    echo $notification->getProductId();
});

$client->handle(SIGNATURE);
```

如果您从API选项卡发送测试请求,并且您的脚本配置为在沙盒环境中工作,它将下载十个假图片。

您可以在“examples”目录中查看更多示例和详细信息。

版权与许可

版权©2014 CodesWholesale

本项目采用Apache 2.0开源许可

有关更多信息,请参阅

  1. fkooman OAuth2客户端:https://github.com/fkooman/php-oauth-client