1. Go to this page and download the library: Download dionyyy/maya library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dionyyy / maya example snippets
use Dionyyy\Maya\Environment;
use Dionyyy\Maya\Maya;
use Dionyyy\Maya\MayaConfig;
$config = new MayaConfig(
environment: Environment::Sandbox, // or Environment::Production
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
// For Cash-in (optional)
cashInSecretKey: 'sk-your-secret-key',
// For Profile decryption (optional)
jwePrivateKey: ['kty' => 'RSA', 'n' => '...', 'e' => '...', 'd' => '...'],
jwsSecretKey: ['kty' => 'oct', 'k' => '...'],
);
$maya = new Maya($config);
use Dionyyy\Maya\DTO\Money;
// Simple: create and execute in one call
$result = $maya->payments()->pay($customerAccessToken, 'your-p2m-id', Money::php(150.00));
echo "Payment ID: {$result->id}";
echo "Fee: {$result->fee->value}";
// Or step by step (if you want to inspect the fee first)
$created = $maya->payments()->create($customerAccessToken, 'your-p2m-id', Money::php(150.00));
echo "Fee will be: {$created->fee->value}";
$executed = $maya->payments()->execute($customerAccessToken, $created->id);
use Dionyyy\Maya\DTO\Money;
use Dionyyy\Maya\Exception\TransferException;
// Check your balance
$balance = $maya->cashIn()->getBalance();
echo "Balance: {$balance->totalBalance->value}";
// Send money
$transfer = $maya->cashIn()->initiate(Money::php(500.00), $customerAccessToken, 'Reward');
echo "Transfer ID: {$transfer->transferId}"; // status: CREATED
// Execute the transfer
try {
$result = $maya->cashIn()->execute($transfer->transferId);
echo "Done! Status: {$result->status}"; // APPROVED
} catch (TransferException $e) {
if ($e->isAmbiguous()) {
// Timeout/server error — check what actually happened
$status = $maya->cashIn()->retrieve($e->getTransferId());
echo "Actual status: {$status->status}";
} else {
echo "Transfer failed: {$e->getMessage()}";
}
}
use Dionyyy\Maya\Exception\AuthException;
try {
$newTokens = $maya->tokens()->refresh($customerRefreshToken);
// Save the new tokens (old ones are now invalid)
$newTokens->accessToken;
$newTokens->refreshToken;
$newTokens->expiresIn;
} catch (AuthException $e) {
// Refresh token expired — customer needs to re-authenticate
}
use Dionyyy\Maya\MayaException;
use Dionyyy\Maya\Exception\AuthException;
use Dionyyy\Maya\Exception\ApiException;
use Dionyyy\Maya\Exception\PaymentException;
use Dionyyy\Maya\Exception\TransferException;
try {
$profile = $maya->profile()->get($token);
} catch (AuthException $e) {
// Token expired or invalid credentials
} catch (ApiException $e) {
// Maya API returned an error
echo $e->getHttpStatusCode(); // 400, 404, 500, etc.
echo $e->getErrorCode(); // Maya's error code
echo $e->getResponseBody(); // Raw response for debugging
} catch (MayaException $e) {
// Catch-all for any SDK error
}
use Dionyyy\Maya\Environment;
use Dionyyy\Maya\Maya;
use Dionyyy\Maya\MayaConfig;
public function register(): void
{
$this->app->singleton(Maya::class, function () {
return new Maya(new MayaConfig(
environment: config('services.maya.environment') === 'production'
? Environment::Production
: Environment::Sandbox,
clientId: config('services.maya.client_id'),
clientSecret: config('services.maya.client_secret'),
cashInSecretKey: config('services.maya.cashin_secret_key'),
jwePrivateKey: json_decode(config('services.maya.jwe_private_key', '{}'), true),
jwsSecretKey: json_decode(config('services.maya.jws_secret_key', '{}'), true),
));
});
}