m4aax16/kinguin-laravel

Kinguin API 的 Laravel 封装包

v1.2-alpha 2021-03-23 12:53 UTC

This package is auto-updated.

Last update: 2024-10-03 09:12:50 UTC


README

描述

m4aax16/kinguin-laravel 是 Laravel 的非官方 Kinguin API 封装

安装

composer require m4aax16/kinguin-laravel "^1.2-alpha"

函数

Product:
	- GET products per page
	- GET count of pages
	- GET single product

Region:
	- GET all regions
	- GET one region by Id

User:
	- GET user balance

Order:
	- Place order
	- Dispatch order
	- Get order keys
Handle Exceptions

用法

初始化对象

use m4aax16\kinguin\Client;
use m4aax16\kinguin\Product;
use m4aax16\kinguin\Order;
use m4aax16\kinguin\User;
use m4aax16\kinguin\Region; 

客户端

客户端初始化

 $kinguinClient = new Client("YOUR-API-KEY-HERE");

产品

按页码获取产品

//This function will fetch 25 products per Page, it's a default value by Kinguin

 $page = 1; 
 $products = Product::getPage($page); // $page is NOT required, default is $page = 1
 
 return $products;
此函数将输出产品的 JSON 对象

访问获取的结果

foreach($products['results'] as $product)
{
	$product['kinguinId']; //Accessing product attribute Id => Output : INT
	$product['name']; //Accessing product attribute name => Output : STRING
	
	//accessing other objects of product
	//Example : Screenshots
	$screenshots = $product['screenshots'];
	
	foreach($screenshots as $screenshot)
	{
		$screenshot['url_original'];
	}
}

获取页数计数

$itemsPerPage = 100; // Max. products per page is 100
$pageCount = Product::getPagesCount($itemsPerPage); // $itemsPerPage is NOT required, default $itemsPerPage = 25

获取单个产品

$productId = 5; //set the kinguinId of product

$product = Product::getProduct($productId); //returns detailed information about this product

区域

获取所有区域

注意:输出不是对 Kinguin API 端点的调用。
所有区域都保存为数组在 Region::class
Region::getRegions(); //returns all regions, the array key is the id of the region

访问 ID 和名称

$regions = Region::getRegions();

foreach($regions as $regionId => $value)
{
	echo $regionId; //region id
	echo " ";
	echo $value; //region name
	echo "</br>";
}

通过 ID 获取区域

$regionId = 1; // Id 1 is "Europe"
$region = Region::getRegionById($regionId); 

echo $region; //Output will be a STRING

用户

获取用户余额

$balance = User::getBalance(); // Output is a FLOAT, Example: 20439.99

订单

注意:Kinguin-Endpoint 支持一个订单中的多个项目
但此包不支持每个订单中的多个项目

下订单

$productId = 195; // ProductId is Warhammer for Steam
$qty = 1;
$price = 0.59;

//All parameter all required
$order = Order::placeOrder($productId,$qty,$price,$couponCode*);  // returns a json with order "orderId: 26983294"
								  // $couponCode is a optional parameter

派发订单

$dispatch = Order::dispatchOrder($orderId);    //returns:  "dispatchId:	19036724"
$dispatchId = $dispatch['dispatchId'];

获取已订购的密钥

$keys = Order::getOrderKeys($dispatchId);

/* Example Output :
        0:
        serial	"SOME-ACTIVATION-KEY"
        type	"text/plain" //Key types are text or image
        name	"Warhammer 40,000: Space Marine Steam CD Key"
        kinguinId	195
*/

注意:为了节省您和 Kinguin 服务器资源,强烈建议订阅订单 postback。它也更为高效。如果想要使用此 URL,请替换您的 storeId:https://www.kinguin.net/integration/dashboard/stores/{storeId}/postback

一旦收到 Post Back 通知,就可以使用此函数

$kgOrderId = "9XQ3FFRQKVL";
$keys = Order::getOrderKeyById($kgOrderId);

/* Example Output :
	0	
	serial	"Real_1942  XYYG8-4GBCC-ILMFM"
	type	"text/plain"
	name	"Real 1942 Steam CD Key"
	kinguinId	57083
	productId	"5c9b787a2539a4e8f184354a"
	offerId	"5fa56a6d9c2506000196e12a"
*/

处理异常

注意:它适用于所有方法,除了 Region::class

	
   $productId = 0; //set the kinguinId of product

    $product = Product::getProduct($productId); //returns detailed information about this product

    //Check the method getStatusCode exists
    if(method_exists($product,'getStatusCode')){
      //If the status code is higher than 200
      if($product->getStatusCode() > 200){
         
        //This method return the full error message inline
        $errFull = $product->getData()->error;

        //This method return only the Kinguin Error Code
        $errCode = $product->getData()->response->code;
        
        //This method return the error message
        $errMessage = $product->getData()->response->message;

        echo "Full Error : ".$errFull."</br>";
        echo "Error Code : ".$errCode."</br>";
        echo "Error Message : ".$errMessage."</br>";

        /* Example ouputs : 
                1. If the client is not authorized =>

                Full Error : Client error: `GET https://gateway.kinguin.net/esa/api/v1/products/0` resulted in a `401 Unauthorized` response: {"code":2401,"message":"Full authentication is required to access this resource."}
                Error Code : 2401
                Error Message : Full authentication is required to access this resource.

                2. If the client is authorized but something goes wrong. For example the requested product does not exist =>

                Full Error : Client error: `GET https://gateway.kinguin.net/esa/api/v1/products/0` resulted in a `404 Not Found` response: {"code":3404,"message":"Product not found"}
                Error Code : 3404
                Error Message : Product not found
        */

      }
    }
    else{
       //Do something clever with the requested product
       return $product;

    }