PHP code example of tinker / payments-php-sdk

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

    

tinker / payments-php-sdk example snippets



use Tinker\TinkerPayments;

$tinker = new TinkerPayments('pk_test_xxx', 'sk_test_xxx');

use Tinker\Enum\Gateway;
use Tinker\Model\DTO\InitiatePaymentRequest;
use Tinker\Model\DTO\QueryPaymentRequest;

$payment = $tinker->transactions()->initiate(new InitiatePaymentRequest(
    amount: 100, currency: 'KES', gateway: Gateway::MPESA,
    merchantReference: 'ORDER-12345',
    returnUrl: 'https://merchant.example/payment/return',
    customerPhone: '+254712345678',
));
$result = $tinker->transactions()->query(new QueryPaymentRequest(
    paymentReference: 'TXN-abc123xyz', gateway: Gateway::MPESA,
));

use Tinker\Model\DTO\CreateSubscriptionPlanRequest;
use Tinker\Model\DTO\CreateSubscriptionRequest;
use Tinker\Model\DTO\SubscriptionCustomer;

$plans = $tinker->subscriptions();
$plan = $plans->createPlan(new CreateSubscriptionPlanRequest(
    name: 'Pro Monthly', amount: 29.99, currency: 'USD', intervals: ['monthly'],
));
$subscription = $plans->create(new CreateSubscriptionRequest(
    planId: $plan['id'], gateway: 'stripe', billingPeriod: 'monthly',
    customer: new SubscriptionCustomer(
        externalCustomerId: 'cust_001', name: 'Jane Doe', email: '[email protected]',
    ),
));
$all = $plans->list();
$plans->cancel($subscription['subscription_id']);

use Tinker\Model\DTO\ManagedCheckout;

$billing = $tinker->subscriptions();
$billing->checkout(new ManagedCheckout(
    externalCustomerId: 'workspace-123', email: '[email protected]', name: 'Buyer',
    currency: 'EUR', unitAmount: 15000, quantity: 3, interval: 'year',
    returnUrl: 'https://example.com/billing', idempotencyKey: 'checkout-123',
));
$billing->changeQuantity('resource-id', 4, 'quantity-123');
$billing->setCancelAtRenewal('resource-id', false, 'resume-123');
$billing->paymentMethodPortal('resource-id', 'https://example.com/billing', 'portal-123');
$page = $billing->billingPaymentHistory('resource-id', '', 0);
// After an uncertain command response, recover using the SAME key:
$operation = $billing->billingOperation('quantity-123');

$billing->expireCheckout('checkout-id', 'expire-123');

$billing->checkout(new ManagedCheckout(
    externalCustomerId: 'workspace-123', email: '[email protected]', name: 'Buyer',
    currency: 'EUR', unitAmount: 15000, quantity: 3, interval: 'year',
    returnUrl: 'https://example.com/billing', idempotencyKey: 'checkout-124',
    trialDays: 14,
));

$billing->setTrialEnd('resource-id', new DateTimeImmutable('+30 days'), 'trial-end-123');
$billing->endTrial('resource-id', 'end-trial-123');

use Tinker\Model\DTO\RefundRequest;
use Tinker\Model\DTO\PayoutRequest;
use Tinker\Model\DTO\CancellationRequest;

$refund = $tinker->transactions()->refund($paymentId, new RefundRequest('requested_by_customer', 25), 'refund-order-42');
$history = $tinker->transactions()->refunds($paymentId);
$destinations = array_filter($tinker->payouts()->methods(),
    fn ($method) => $method['is_verified'] && $method['status'] === 'active');
$destination = reset($destinations);
if (!$destination) { throw new \RuntimeException('Configure an active, verified payout method'); }
$payoutMethodId = $destination['id'];
$quote = $tinker->payouts()->quote($payoutMethodId, 100, $destination['currency']);
$payout = $tinker->payouts()->requestPayout(new PayoutRequest($payoutMethodId, 100, $destination['currency'], $quote['fee_amount']), 'withdrawal-42');
$status = $tinker->payouts()->get($payout->withdrawal->getId());
$cancellation = $tinker->transactions()->cancelCheckout($unpaidPaymentId, new CancellationRequest('Order cancelled'), 'cancel-order-42');

$event = $tinker->webhooks()->handleFromRequest();
if (!$tinker->webhooks()->verifySignature($event, $webhookSecret)) {
    http_response_code(401);
    exit('Invalid signature');
}
$transaction = $event->toTransaction(); // null for setup/lifecycle events

try {
    $tinker->transactions()->query(new QueryPaymentRequest('TXN-abc123xyz', Gateway::MPESA));
} catch (\Tinker\Exception\ApiException $error) {
    error_log($error->errorCode . ': ' . $error->getMessage());
}