PHP code example of bankplanet9 / milkyway-payments

1. Go to this page and download the library: Download bankplanet9/milkyway-payments 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/ */

    

bankplanet9 / milkyway-payments example snippets


use Bankplanet9\MilkywayPayments\MilkywayOptions;
use Bankplanet9\MilkywayPayments\MilkywayPaymentsClient;
use Bankplanet9\MilkywayPayments\Model\IdentityDocumentType;
use Bankplanet9\MilkywayPayments\Model\PartyInfo;
use Bankplanet9\MilkywayPayments\Model\PartyName;
use Bankplanet9\MilkywayPayments\Model\PartyType;
use Bankplanet9\MilkywayPayments\Model\PayRequest;
use Bankplanet9\MilkywayPayments\Model\PrecheckRequest;
use Brick\Math\BigDecimal;

$client = new MilkywayPaymentsClient(new MilkywayOptions(
    baseUrl:      'https://milkyway.stage.planet9.ae',
    tokenUrl:     'https://keycloak.ac8o.planet9.ae/realms/planet9-stage/protocol/openid-connect/token',
    clientId:     'your-client-id',      // issued to your institution
    clientSecret: 'your-client-secret',
));

// 1. Is the recipient bank's service online?
$client->healthcheck('bank-beta', 'card-payout');

// Your customer (the payer). Fill what you hold; the service's party profile in
// discovery says which fields the recipient bank : 'bank-beta',
    serviceId:         'card-payout',
    senderId:          'sender-0001',
    recipientId:       'recipient-9999',
    amountCredit:      BigDecimal::of('100.00'),
    currencyCredit:    'USD',
    senderInfo:        $sender,
), idempotencyKey: bin2hex(random_bytes(16)));

// 4. Poll until the payment reaches a terminal status.
$result = $client->waitForCompletion($transactionId);
printf("Final status: %s\n", $result->status->name);

$client = new MilkywayPaymentsClient(
    options:    $options,
    httpClient: $myPsr18Client,          // used for API calls
    // tokenHttpClient: a SEPARATE plain client for the token endpoint (must not be
    //                  wrapped in retry/auth); defaults to its own discovered client.
    requestFactory: $myPsr17RequestFactory,
    streamFactory:  $myPsr17StreamFactory,
);

// List every destination, or pass an ISO 3166-1 alpha-2 country to filter.
foreach ($client->discovery(country: 'TJ') as $destination) {
    printf("%s (%s)\n", $destination->partnerName ?? $destination->thirdPartyId, $destination->country);
    foreach ($destination->services as $service) {
        printf("  - %s via %s, pays out in %s\n",
            $service->serviceId,
            $service->payoutMethod,
            implode(', ', $service->payoutCurrencies ?? []));
        // Which senderInfo fields the recipient bank 

use Bankplanet9\MilkywayPayments\Model\PartyAddress;

$request = new PayRequest(
    // ...
    senderInfo:    $sender,
    recipientInfo: new PartyInfo(
        name:    new PartyName(first: 'Aziz', last: 'Karimov'),
        address: new PartyAddress(country: 'TJ', city: 'Dushanbe'),
    ),
);

use Bankplanet9\MilkywayPayments\Exception\MilkywayPaymentRejectedException;
use Bankplanet9\MilkywayPayments\Exception\MilkywayRecipientBankException;
use Bankplanet9\MilkywayPayments\Exception\MilkywayServiceUnavailableException;
use Bankplanet9\MilkywayPayments\Exception\MilkywayValidationException;

try {
    $transactionId = $client->pay($request, idempotencyKey: $orderId);
} catch (MilkywayValidationException $e) {
    // Fix the request: $e->getField() names the field, e.g. "recipient_id".
} catch (MilkywayPaymentRejectedException $e) {
    // The recipient bank declined. Do not resend the same payment.
} catch (MilkywayRecipientBankException $e) {
    // The recipient bank's connector failed. Retry later or route elsewhere.
} catch (MilkywayServiceUnavailableException $e) {
    // Recipient service down: $e->getServiceId(), $e->getThirdPartyId().
}

use Bankplanet9\MilkywayPayments\StatusReason;
use Bankplanet9\MilkywayPayments\TransactionStatus;

$result = $client->waitForCompletion($transactionId);
if ($result->status === TransactionStatus::Failed && $result->error === StatusReason::RECIPIENT_NOT_FOUND) {
    // Ask your customer to check the recipient's account / card / phone.
}