thienhaxanh2405/alego-api-wrapper

Alego.vn的包装器

v1.1.0 2017-12-11 09:47 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:49:31 UTC


README

我编写这个包装器的目的是为了方便调用Alego的API。

链接 Github

作者

thienhaxanh2405 nguyenductoan2405@gmail.com

http://thienhaxanh.info

要求

安装

通过composer: composer require thienhaxanh2405/alego-api-wrapper

为了使用 require_once "/path/to/vendor/autoload.php";

对象(Object)

  • \AlegoApiWrapper\Resource\Buy 对象用于传递交易参数。根据交易的不同,需要传递的参数可能不同。
  • \AlegoApiWrapper\Resource\ApiResponse 对象包含每次交易的返回信息。
    • message: 交易通知。
    • messageCode: 通知码。
    • rawData: 从API返回的原始数据。
    • result: 对象包含从rawData处理后返回的信息。可能是 \AlegoApiWrapper\Resource\CardResult\AlegoApiWrapper\Resource\Balance,具体取决于当前交易。
  • \AlegoApiWrapper\Resource\CardResult: 包含购买手机卡/充值/游戏卡/预付费Visa/检查交易返回的信息。
    • productCode: Alego的服务/产品码。
    • $referOrder: 您系统中的交易参考码。
    • alegoTransactionId: Alego系统中对应交易的id。
    • time: Alego系统中的交易时间,Unix时间戳格式。
    • responseType: Alego通过以下方式返回结果:1 - 电子邮件;2 - 直接在API中。
    • cardQuantity: 卡的数量。
    • cards: 包含卡信息或空数组的数组。数组中的每个元素都是一个 \AlegoApiWrapper\Resource\PrepaidCard 对象或未来当包装器支持购买预付费Visa卡时的Visa卡。
  • \AlegoApiWrapper\Resource\Balance: 包含代理商账户余额信息。
    • balance: 总余额。
    • availableBalance: 可用余额。
    • frozenBalance: 冻结余额。
  • \AlegoApiWrapper\Resource\PrepaidCard: 包含手机卡、游戏卡信息。
    • code: 充值卡码。
    • serial: 卡号。
    • expirationDate: 卡有效期。

预定义常量(Constant)

  • \AlegoApiWrapper\Constant\AlegoProduct: Alego系统中的产品/服务码。
  • \AlegoApiWrapper\Constant\Telco: 运营商/移动运营商码。
  • \AlegoApiWrapper\Constant\PrepaidCardPrice: 预付费手机卡面值。
  • \AlegoApiWrapper\Constant\AlegoTransactionType: Alego系统中的交易类型。

连接信息

您需要在http://alego.vn注册代理商账户。使用连接信息来初始化Account对象。

use AlegoApiWrapper\Connection\Account;

// thông tin account bên dưới là tài khoản test của Alego
// thay thế bằng thông tin tài khoản của bạn
$account = new Account([
        'agentId' => 1,
        'accountId' => '571a01a0e4b0b96b6f950ad5',
        'keyMD5' => 'adrMjEJArHysrhwM',
        'tripleKey' => 'asdf'
    ]);

初始化Client对象以执行交易

use AlegoApiWrapper\Client;

$client = Client::createClient($account);
// Trường hợp trên môi trường Dev và muốn dump dữ liệu để debug
// truyền thêm 2 tham số isDevelopment và isDebug
// $client = Client::createClient($account, true, 1);

使用

购买手机卡

初始化一个 \AlegoApiWrapper\Resource\Buy 对象,至少需要以下参数

  • referOrder:您系统中的参考码,可以是 id 或是您系统中的一个对应此交易的唯一订单码。
  • productCode:与购买充值卡交易对应的 Alego 产品码。在 AlegoApiWrapper\Constant\AlegoProduct 中已定义了一些常量,方便您调用和使用。
  • telco:网络运营商码。在 AlegoApiWrapper\Constant\Telco 中已定义。
  • cardPrice:充值卡面值。在 AlegoApiWrapper\Constant\PrepaidCardPrice 中已定义。
  • cardQuantity:需要购买的数量(整数)

例如购买一张面值10,000,000越南盾的Viettel充值卡。

use AlegoApiWrapper\Constant\AlegoProduct;
use AlegoApiWrapper\Constant\Telco;
use AlegoApiWrapper\Constant\AlegoTransactionType;
use AlegoApiWrapper\Resource\Buy;
use AlegoApiWrapper\Constant\PrepaidCardPrice;

$buyCard = new Buy([
    'referOrder' => uniqid(),
    'productCode' => \AlegoApiWrapper\Constant\AlegoProduct::PREPAID_CARD_VIETTEL,
    'telco' => \AlegoApiWrapper\Constant\Telco::VIETTEL_CODE,
    'cardPrice' => \AlegoApiWrapper\Constant\PrepaidCardPrice::C_100,
    'cardQuantity' => 1
]);

$res = $client->buyPrepaidCard($buyCard);

手机充值

初始化一个 \AlegoApiWrapper\Resource\Buy 对象,至少需要以下参数

  • referOrder:您系统中的参考码,可以是 id 或是您系统中的一个对应此交易的唯一订单码。
  • productCode:与购买充值卡交易对应的 Alego 产品码。在 AlegoApiWrapper\Constant\AlegoProduct 中已定义了一些常量,方便您调用和使用。
  • telco:网络运营商码。在 AlegoApiWrapper\Constant\Telco 中已定义。
  • cardPrice:充值卡面值。在 AlegoApiWrapper\Constant\PrepaidCardPrice 中已定义。
  • customerCellphone:要充值的手机号码
  • type:Alego系统中的交易类型。为预付费套餐充值:AlegoTransactionType::TOPUP_PREPAID 或为后付费套餐充值:AlegoTransactionType::TOPUP_POSTPAID

例如为Viettel运营商的手机号码0987 802 175充值10,000,000越南盾。

use AlegoApiWrapper\Constant\AlegoProduct;
use AlegoApiWrapper\Constant\Telco;
use AlegoApiWrapper\Constant\AlegoTransactionType;
use AlegoApiWrapper\Resource\Buy;
use AlegoApiWrapper\Constant\PrepaidCardPrice;

$buy = new \AlegoApiWrapper\Resource\BuyPrepaidCard([
    'referOrder' => uniqid(),
    'productCode' => \AlegoApiWrapper\Constant\AlegoProduct::TOPUP_PREPAID_VIETTEL,
    'telco' => \AlegoApiWrapper\Constant\Telco::VIETTEL_CODE,
    'cardPrice' => \AlegoApiWrapper\Constant\PrepaidCardPrice::C_100,
    'customerCellphone' => "0987802175"
]);

$res = $client->prepaidTopUp($buy);

购买游戏卡

初始化对象 \AlegoApiWrapper\Resource\Buy,至少需要以下参数

  • referOrder:您系统中的参考码,可以是 id 或是您系统中的一个对应此交易的唯一订单码。
  • productCode:与购买游戏卡交易对应的 Alego 产品码。在 AlegoApiWrapper\Constant\AlegoProduct 中已定义了一些常量,方便您调用和使用。
  • cardPrice:充值卡面值。每个供应商的面值可能不同,请参阅 Alego 的指导文档以获取更多信息。为了方便使用,您可以使用 AlegoApiWrapper\Constant\PrepaidCardPrice
  • cardQuantity:需要购买的数量(整数)

例如购买面值100,000越南盾的FPT Gate游戏卡。

use AlegoApiWrapper\Constant\AlegoProduct;
use AlegoApiWrapper\Resource\Buy;
use AlegoApiWrapper\Constant\PrepaidCardPrice;

$buy = new \AlegoApiWrapper\Resource\BuyPrepaidCard([
    'referOrder' => uniqid(),
    'productCode' => \AlegoApiWrapper\Constant\AlegoProduct::GAME_PREPAID_CARD_GATE,
    'cardPrice' => \AlegoApiWrapper\Constant\PrepaidCardPrice::C_100,
    'cardQuantity' => 1,
]);

$res = $client->buyGamePrepaidCard($buy);

购买预付费 Visa 卡

稍后将更新

购买 Battle Net 卡

稍后将更新

检查交易

使用您系统中的 referOrder 码来参考 Alego 系统中的交易。

例如您系统中的码是:order_5a119e3c6cfb0

$res = $client->checkOrder("5a119e3c6cfb0");

检查账户余额

$res = $client->getBalance();

测试

测试案例将在稍后添加

支持 - 解答疑问

Wrapper 仍有许多我尚未发现的不足或 bug,请创建 issue 或通过电子邮件联系我: nguyenductoan2405@gmail.com

许可证

我写这个 Wrapper 是为了个人工作目的,但我也不介意公开和分享源代码。希望这个 wrapper 对你们的工作有所帮助。

关于分享、建议、重新分发源代码,请遵循 MIT 许可证。

即使您直接取用并使用,将其放入项目...而不遵循 MIT 许可证,我也欢迎您 =)))