1. Go to this page and download the library: Download paymos/php-sdk 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/ */
paymos / php-sdk example snippets
use Paymos\Client;
use Paymos\ClientConfig;
$client = new Client(new ClientConfig(
'pk_test_REPLACE_WITH_YOUR_KEY', // API Key
'sk_test_REPLACE_WITH_YOUR_SECRET', // API Secret
'https://api.paymos.io', // Base URL (omit for default)
30 // Request timeout (seconds)
));
$channel = $client->paymentChannels()->create(array(
'project_id' => 'prj_xxxxxxxxxxxx',
'external_id' => 'customer_42', // your own stable id for this payer
));
foreach ($channel['networks'] as $rail) {
if (!isset($rail['address'])) {
continue; // this rail has not provisioned yet
}
echo $rail['network'] . ' ' . $rail['address'] . PHP_EOL;
foreach ($rail['tokens'] as $token) {
$minimum = isset($token['minimum_deposit']) ? $token['minimum_deposit'] : null;
echo ' ' . $token['symbol'] . ' min: ' . ($minimum === null ? 'unavailable' : $minimum) . PHP_EOL;
}
}
$feed = $client->paymentChannelDeposits()->readPage(array(
'cursor' => $savedCursor, // omit (or null) on the very first poll
'limit' => 100,
));
foreach ($feed['items'] as $deposit) {
$this->credit($deposit); // idempotently, by $deposit['id']
}
$this->saveCursor($feed['next_cursor']); // always present, even on an empty page
$client->paymentChannels()->block('pc_xxxxxxxxxxxx'); // returns the updated channel
$client->paymentChannels()->unblock('pc_xxxxxxxxxxxx');
// Sandbox only ('network' is
use Paymos\IdempotencyKey;
$key = IdempotencyKey::externalOrderId('wc'); // "wc_550e8400-e29b-41d4-a716-446655440000"
use Paymos\Webhook\InMemoryEventStore;
use Paymos\Webhook\WebhookEventProcessor;
use Paymos\Webhook\WebhookVerifier;
use Paymos\Exception\DuplicateEventException;
use Paymos\Exception\SignatureMismatchException;
use Paymos\Exception\TimestampSkewException;
$verifier = new WebhookVerifier('whsec_xxxxxxxxxxxx', 300);
$processor = new WebhookEventProcessor($verifier, new InMemoryEventStore());
$signature = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
$rawBody = file_get_contents('php://input');
try {
$event = $processor->process($signature, $rawBody);
// $event === ['event_id' => '...', 'event_type' => '...', 'data' => [...], ...]
} catch (DuplicateEventException $e) {
http_response_code(200); // already processed - ack
exit;
} catch (SignatureMismatchException $e) {
http_response_code(401);
exit;
} catch (TimestampSkewException $e) {
http_response_code(401);
exit;
}
// Map the event to a precise business action and update your order/payout state.
use Paymos\Plugin\StatusMapper;
if (strpos($event['event_type'], 'invoice.') === 0) {
$action = StatusMapper::invoiceAction($event['event_type']);
switch ($action) {
case StatusMapper::ACTION_CONFIRMING:
// On-chain transfer detected, waiting for confirmations.
break;
case StatusMapper::ACTION_AWAITING_PAYMENT:
// Partial payment received, waiting for the rest.
break;
case StatusMapper::ACTION_PAYMENT_COMPLETE:
// Terminal: invoice paid (or paid_over) - mark order paid, fulfil.
break;
case StatusMapper::ACTION_FAIL_ORDER:
// Terminal: invoice underpaid past deadline.
break;
case StatusMapper::ACTION_CANCEL_ORDER:
// Terminal: invoice expired or cancelled.
break;
case StatusMapper::ACTION_IGNORE:
// Unrecognized / future invoice event - no state change.
break;
}
} else {
$action = StatusMapper::withdrawalAction($event['event_type']);
switch ($action) {
case StatusMapper::ACTION_PROCESSING:
// Withdrawal broadcast on-chain.
break;
case StatusMapper::ACTION_COMPLETED:
// Terminal success.
break;
case StatusMapper::ACTION_FAILED:
// Terminal failure - reversed back to balance.
break;
case StatusMapper::ACTION_CANCELLED:
// Cancelled before broadcast.
break;
case StatusMapper::ACTION_IGNORE:
// Informational event (withdrawal.created).
break;
}
}
http_response_code(200);
use Paymos\Webhook\EventStoreInterface;
final class WordPressEventStore implements EventStoreInterface
{
public function remember($eventId, $ttlSeconds)
{
$key = 'paymos_evt_' . $eventId;
if (get_transient($key)) {
return false;
}
set_transient($key, 1, (int) $ttlSeconds);
return true;
}
}
use Paymos\Exception\ApiException;
use Paymos\Exception\ConflictException;
use Paymos\Exception\GoneException;
use Paymos\Exception\NotFoundException;
use Paymos\Exception\RateLimitException;
use Paymos\Exception\UnavailableException;
use Paymos\Exception\ValidationException;
try {
$client->invoices()->create($payload);
} catch (ValidationException $e) {
foreach ($e->errors() as $err) {
// $err = ['code' => '...', 'field' => '...|null', 'message' => '...']
}
} catch (NotFoundException $e) {
// 404
} catch (ConflictException $e) {
// 409 - e.g. insufficient_balance. $e->errorCode() / $e->detail() (flat envelope).
} catch (GoneException $e) {
// 410 - resource is in a terminal state (cancel after Paid, etc.)
} catch (RateLimitException $e) {
// 429 - SDK retries automatically (any method); surfaces only after RetryPolicy
// is exhausted. $e->retryAfterSeconds() gives the server's Retry-After hint.
} catch (UnavailableException $e) {
// 503 - upstream / transient. Retried only on idempotent methods (GET/HEAD);
// a 503 on a POST surfaces immediately (it may already have taken effect).
} catch (ApiException $e) {
// any other API error
}
use Paymos\Client;
use Paymos\ClientConfig;
use Paymos\Http\CurlTransport;
use Paymos\Http\RetryPolicy;
use Paymos\Http\RetryingTransport;
$client = new Client(
new ClientConfig('pk_test_...', 'sk_test_...'),
new RetryingTransport(new CurlTransport(), new RetryPolicy(/* maxRetries */ 4, /* baseMs */ 250))
);
use Paymos\Client;
use Paymos\ClientConfig;
use Paymos\Http\MockTransport;
use Paymos\Http\HttpResponse;
$transport = new MockTransport(array(
new HttpResponse(200, '{"invoice_id":"inv_123","status":"awaiting_client"}', array()),
));
$client = new Client(new ClientConfig('pk_test_a', 'sk_test_b'), $transport);
$client->invoices()->get('inv_123');
print_r($transport->requests()); // captured method/url/headers/body