nikapps/bazaar-api-php

为 CafeBazaar Rest Api v2 提供的 PHP API 封装

2.0.1 2016-02-29 21:23 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:50:16 UTC


README

PHP API 封装用于 Cafebazaar REST API (v2)

如果您正在寻找 1.x 版本,请访问 分支 v1

CafeBazaar Logo

目录

安装

如果您没有 Composer,首先应在您的系统上安装它

https://getcomposer.org.cn

现在运行此命令以安装 此包

composer require nikapps/bazaar-api-php
  • 注意:如果您对 composer 一无所知,请阅读此 文章

配置

创建客户端

首先,您应前往您的 CafeBazaar 控制面板并创建一个客户端。

  • 登录到您的面板并访问此 URL:https://pardakht.cafebazaar.ir/panel/developer-api/?l=fa&nd=False

  • 单击 new client 并输入您的重定向 URI(它用于获取返回的 coderefresh_token。请参阅下一节)

现在您有了 client-idclient-secret

获取刷新令牌

  • 在浏览器中打开此 URL
https://pardakht.cafebazaar.ir/devapi/v2/auth/authorize/?response_type=code&access_type=offline&redirect_uri=<REDIRECT_URI>&client_id=<CLIENT_ID>

别忘了更改 <REDIRECT_URI><CLIENT_ID>

  • 点击接受/确认按钮后,您将被重定向到:<REDIRECT_URI>?code=<CODE>

  • <REDIRECT_URI> 是此文件的 URL

$bazaar = new Bazaar(new Config([
    'client-secret' => 'your-client-secret',
    'client-id' => 'your-client-id'
]));

$token = $bazaar->token('<REDIRECT_URI>');

echo "Refresh Token: " . $token->refreshToken();

以下是完整示例: authorization.php

设置配置

如前所述,我们创建了一个 Config 实例并设置了 client-idclient-secret

对于其他 API 调用,我们还需要设置 refresh-tokenstorage

$bazaar = new Bazaar(new Config([
    'client-secret' => 'your-client-secret',
    'client-id' => 'your-client-id',
    'refresh-token' => 'refresh-token-123456',
    'storage' => new FileTokenStorage(__DIR__ . '/token.json')
]));

storage 负责存储和检索 access_token。在此包中,我们有两种不同的存储方式

  • FileTokenStorage,它将令牌存储在文件中。
  • MemoryTokenStorage,它不会持久化令牌,您只能在当前请求中使用它。

使用方法

购买

以下是获取购买状态的示例

$purchase = $bazaar->purchase('com.example.app', 'product-id (sku)', 'purchase-token');

if ($purchase->failed()) {
    echo $purchase->errorDescription();
} else {
    echo "Purchased: " . $purchase->purchased();
    echo "Consumed: " . $purchase->consumed();
    echo "Developer Payload: " . $purchase->developerPayload();
    echo "Purchase Time (Timestamp in ms): " . $purchase->time();
}

完整示例: purchase.php

订阅

以下是获取订阅状态的示例

$subscription = $bazaar->subscription('com.example.app', 'subscription-id (sku)', 'purchase-token');

if ($subscription->failed()) {
    echo $subscription->errorDescription();
} else {
    echo "Start Time (Timestamp in ms): " . $subscription->startTime(); // initiationTime()
    echo "End Time (Timestamp in ms): " . $subscription->endTime(); // expirationTime(), nextTime()
    echo "Is auto renewing? " . $subscription->autoRenewing();
    echo "Is expired? (end time is past) " . $subscription->expired();
}

完整示例: subscription.php

取消订阅(退订)

以下是取消订阅的示例

$unsubscribe = $bazaar->unsubscribe('com.example.app', 'subscription-id (sku)', 'purchase-token');

if ($unsubscribe->successful()) {
    echo "The subscription has been successfully cancelled!";
} else {
    echo $unsubscribe->errorDescription();
}

完整示例: unsubscribe.php

自定义

自定义令牌存储

如果您想将令牌存储在其他地方(可能是数据库或 Redis?!),您可以实现 TokenStorageInterface

class CustomTokenStorage implements TokenStorageInterface 
{

    public function save(Token $token)
    {
    	// store access token
    }

    public function retrieve()
    {
    	// return access token
    }

    public function expired()
    {
    	// is token expired?
    }

}

示例

参见:https://github.com/nikapps/bazaar-api-php/blob/master/examples/

依赖项

测试

运行

phpunit

官方文档

贡献

想要贡献?只需fork此项目并提交一个pull request!

许可协议

本项目遵循MIT许可证发布。

/*
 * Copyright (C) 2015-2016 NikApps Team.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * 1- The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * 2- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

捐赠

Donate via Paypal