bosunski/moneywave
Composer 安装命令:
composer require bosunski/moneywave
包简介
A Testimplementation of the Moneywave API emmanix2002 by Flutterwave
README 文档
README
A PHP library for consuming the Moneywave API services.
You can check out the documentation to see all that is available: https://moneywave-doc.herokuapp.com/
Quickstart
To get started, you simply need to install it via composer:
$ composer require emmanix2002/moneywave
This will add it to your composer.json and install it as a project dependency.
Introduction
All the Features and Resources available on the Moneywave service are exposed as Services. Hence, to use any of the services, it first needs to be created.
All Features and Resources on the Moneywave API are exposed as services in this library.
The entry point to this library is the Moneywave class.
$moneywave = new Moneywave();
We'll discuss more about this later.
Configuration
To use the library, you need to get your credentials from your Moneywave account. They provide you two keys:
- API Key
- Secret Key
Your account can be in one of two states: Test or Production. For each of these states, you'll use different
keys.
These keys are required by the Moneywave class (and must be protected -- they are used to authenticate
the merchant account); to use them with this library, you can use one of two possible methods.
Environment Variables
Using this method stores the key in a specific file on your server, meaning the values are not hardcoded into your
code. The library expects to find a file called .env at the same level as your composer vendor directory.
.env
vendor/
composer.json
composer.lock
As you can see above, the setting file should be at the level described. The content of the file should be the same as
you can find in the .env.example like so:
# your account Moneywave API key
MONEYWAVE_API_KEY="your API key goes here"
# your account Moneywave Secret key
MONEYWAVE_SECRET_KEY="your secret key goes here"
# the environment - staging | production
MONEYWAVE_ENV="staging"
Those values must be set to use the library; with this done, you can simply call:
$moneywave = new Moneywave();
Pass into the Constructor
The second way to configure the Moneywave client is to pass all the settings into the constructor.
Unlike method one, you'll need to store the keys somewhere and provide them to the client when you instantiate it.
$moneywave = new Moneywave(null, $apiKey, $secretKey); # this defaults to the STAGING environment
$moneywave = new Moneywave(null, $apiKey, $secretKey, Environment::STAGING);
Usage
When the client is instantiated (see configuration), it automatically starts up the
VerifyMerchant service. This service gets an access token from the Moneywave service that will be used
to authorize every other request you make against the API.
Every access token has a lifespan of 2 hours. In your application, you have one of 2 options:
- Save the retrieved token to your
Sessionto use it across multiple requests - Allow the library request one for every call made to the API
For the first option, take a look at the sample files in the examples directory. You'll see something like
this:
use Emmanix2002\Moneywave\Exception\ValidationException;
use Emmanix2002\Moneywave\Moneywave;
require(dirname(__DIR__).'/vendor/autoload.php');
session_start();
try {
$accessToken = !empty($_SESSION['accessToken']) ? $_SESSION['accessToken'] : null;
$mw = new Moneywave($accessToken);
$_SESSION['accessToken'] = $mw->getAccessToken();
$query = $mw->createWalletBalanceService();
$response = $query->send();
var_dump($response->getData());
var_dump($response->getMessage());
} catch (ValidationException $e) {
var_dump($e->getMessage());
}
This makes it possible to use the same access token for another request from the same machine.
Services
After instantiating the Moneywave object, you follow these steps to use a service:
- create an instance of the required service from it by calling one of the
create*Service()methods - set the properties on the service object
- call the
send()method on the created service object.
Each feature and resource maps to a service; the mappings can be easily inferred from the class name.
The table below describes all the services:
| Class Name | Service Call |
|---|---|
| AccountNumberValidation | createAccountNumberValidationService |
| AccountToAccount | createAccountToAccountService |
| AccountToWallet | createAccountToWalletService |
| AccountTransfer | createAccountTransferService |
| Banks | createBanksService |
| CardToBankAccount | createCardToBankAccountService |
| CardTokenization | createCardTokenizationService |
| CardToWallet | createCardToWalletService |
| CardTransfer | createCardTransferService |
| Disburse | createDisburseService |
| DisburseBulk | createDisburseBulkService |
| InternetBankingToWallet | createInternetBankingToWalletService |
| QueryCardToAccountTransfer | createQueryCardToAccountTransfer |
| QueryDisbursement | createQueryDisbursementService |
| RetryFailedTransfer | createRetryFailedTransferService |
| TotalChargeToCard | createTotalChargeToCardService |
| ValidateCardTransfer | createValidateCardTransferService |
| ValidateTransfer | createValidateTransferService |
| VerifyMerchant | createVerifyMerchantService |
| WalletBalance | createWalletBalanceService |
Each service has a list of properties that must be set on it before it can be sent to the API; if one or more of
these properties are not set, calling send() throws a ValidationException.
Let's use the Account Number validation API resource as an example:
From the documentation, the following properties are required:
| Field Name | Description |
|---|---|
| account_number | the account number of the sender |
| bank_code | the bank code of the account to resolve |
To use the library, we'll do something like this:
$moneywave = new Moneywave();
$accountValidation = $moneywave->createAccountNumberValidationService();
$accountValidation->account_number = '0690000004';
$accountValidation->bank_code = Banks::ACCESS_BANK;
$response = $accountValidation->send();
If one of those fields was not set on the service object, a ValidationException exception would have been thrown.
Every field defined within the Moneywave documentation (for a service) can be set as a property on the created service object.
Special Fields
There are certain fields which are special, and do not need to be set by you (although you can choose to set them); they'll be automatically given their required value by the library. Find them listed below:
| Field | Description |
|---|---|
| apiKey | this will be set to the API key used to instantiate the Moneywave object |
| secret | this will be set to the secret key used for the Moneywave object |
| fee | set by default to 0 |
| recipient | for the createCardToWalletService, this is set to wallet by default |
| currency | automatically set to Naira Currency::NAIRA |
Special Services
Just as there're special fields, there're also some special service objects, that present more than the regular:
send() method.
Transfers (createCardToBankAccountService(), createCardToWalletService())
These transfer services are special because, most times, they're 2-legged. They involve the following steps:
1 The transfer leg 2 The validation leg
These steps work one after the other; and both steps must be completed to complete the transfer.
There're 2 services that take care of the validation leg; they are:
createValidateCardTransferService(): allows you to validate a card to wallet or card to account transfer made with a Verve debit cardcreateValidateTransferService(): allows you to validate a card to wallet or card to account transfer made with an account. That is, the charge_with field was set toChargeMethod::ACCOUNT.
So, when you receive a success response from the API after the first leg; you start the validate leg.
NOTE: For Mastercard and Visa card transfers, you'll receive a authurl key in the successful API response; you
are to redirect to this URL for the payer to validate the transfer. After success or failure, the payer will be
redirected back to the URL set in the redirecturl field of the transfer request.
createDisburseBulkService()
This service is for disbursing cash from your Moneywave wallet to multiple bank accounts. It has a special method on
it for adding the individual beneficiary accounts.
addRecipient(string $bankCode, string $accountNumber, float $amount, string $reference = null);
This method allows you to add each beneficiary account in turn:
$bulkDisbursement = $moneywave->createDisburseBulkService();
$bulkDisbursement->lock = 'wallet password';
$bulkDisbursement->ref = 'unique reference'; # suggestion: you could use a UUID; check out ramsey/uuid package
$bulkDisbursement->senderName = 'MoneywaveSDK';
$bulkDisbursement->addRecipient(Banks::ACCESS_BANK, '0690000004', 1)
->addRecipient(Banks::ACCESS_BANK, '0690000005', 2);
Look at the examples/disburse_bulk.php file for the full example.
Handling Responses
If all the required fields on the service object (see services) have been set, the call to send()
will return a response object which is an instance of the MoneywaveResponse class.
Continuing with the Account Validation example above:
$response = $accountValidation->send();
A successful response JSON will be of this form:
{
status: "success",
data: {
name: "MICHAEL JACKSON"
}
}
While a failure response JSON will be of the form:
{
status: "error",
message: "error message description",
code: "error code string; e.g. INVALID_ID",
data: "data string -- this is absent most of the time"
}
The $response variable presents a few functions that can be used to work with this data:
# was a request successful?
if ($response->isSuccessful()) {
# do something with the returned data
$name = $response->getData()['name'];
} else {
# this was a failure
$message = $response->getMessage();
$code = $response->getCode();
$data = $response->getData();
}
NOTE: All keys within the response JSON are also accessible from the response object as properties:
if ($response->isSuccessful()) {
$name = $response->getData()['name'];
} else {
$message = $response->message;
$code = $response->code;
}
As another example, the response from the VerifyMerchant service looks like this:
{
status: "success",
token: "" // a valid merchant token
}
Using the response, you'll have to do this:
$response = $verifyService->send();
if ($response->isSuccessful()) {
$token = $response->token;
} else {
# process the error response as you normally would
}
The table below describes the methods defined on the MoneywaveResponse object:
| Method | Return Type | Description |
|---|---|---|
| getRawResponse() | string | the JSON response from the API |
| isSuccessful() | bool | checks if the status key === "success" |
| getCode() | string | returns the code key |
| getMessage() | string | returns the message attribute |
| getData() | array | returns the data key |
NOTE: For responses where data is a string; it returns this array [data: string]
bosunski/moneywave 适用场景与选型建议
bosunski/moneywave 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「finance」 「emmanix2002」 「Moneywave」 「flutterwave」 「fintech」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bosunski/moneywave 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bosunski/moneywave 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bosunski/moneywave 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Sila PHP SDK for API Version 0.2
Simple OFX file parser
PHP SDK for Ledga.io - Programmatic double-entry ledgers for finance, gaming and multi-tenant SaaS
Fitbank SDK for PHP
PHP wrapper for the Moneywave API
PHP wrapper of the dekopay finance API
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-05