PHP code example of salymdev / moshipay-php

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

    

salymdev / moshipay-php example snippets




oshiPay\Client;

$apiKey = getenv('MOSHIPAY_API_KEY') ?: '';
$apiSecret = getenv('MOSHIPAY_API_SECRET') ?: '';

$client = getenv('MOSHIPAY_ENV') === 'live'
    ? Client::live($apiKey, $apiSecret)
    : Client::sandbox($apiKey, $apiSecret);

$client = new Client(
    apiKey: $apiKey,
    apiSecret: $apiSecret,
    baseUrl: getenv('MOSHIPAY_BASE_URL') ?: 'https://sandbox.moshipay.co.tz',
    timeout: 30
);



use MoshiPay\Exception\ApiException;
use MoshiPay\Exception\ValidationException;

try {
    $payment = $client->createMobileMoneyPayment(
        amount: 5000,
        currency: 'TZS',
        phoneNumber: '255781000000',
        customer: [
            'firstname' => 'Jane',
            'lastname' => 'Customer',
            'email' => '[email protected]',
        ],
        description: 'Order ORD-1001',
        callbackUrl: 'https://merchant.example.com/webhooks/moshipay',
        returnUrl: 'https://merchant.example.com/orders/ORD-1001',
        metadata: [
            'order_id' => 'ORD-1001',
        ],
        idempotencyKey: 'ORD-1001'
    );

    $moshipayReference = $payment['moshipay_reference'] ?? null;
} catch (ValidationException $exception) {
    // Local payload validation failed before the API request was sent.
    $errors = $exception->errors();
} catch (ApiException $exception) {
    // MoshiPay returned a non-2xx response.
    $statusCode = $exception->statusCode();
    $response = $exception->response();
}



$payment = $client->createCardPayment(
    amount: 10000,
    currency: 'TZS',
    customer: [
        'firstname' => 'Jane',
        'lastname' => 'Customer',
        'email' => '[email protected]',
        'mobile' => '255781000000',
        'address' => 'Customer Address',
        'city' => 'Dar es Salaam',
        'state' => 'DSM',
        'postcode' => '14101',
        'country' => 'TZ',
    ],
    redirectUrl: 'https://merchant.example.com/payment/success',
    cancelUrl: 'https://merchant.example.com/payment/cancel',
    description: 'Order ORD-1002',
    callbackUrl: 'https://merchant.example.com/webhooks/moshipay',
    metadata: [
        'order_id' => 'ORD-1002',
    ],
    idempotencyKey: 'ORD-1002'
);

if (! empty($payment['payment_url'])) {
    header('Location: ' . $payment['payment_url'], true, 302);
    exit;
}



$payment = $client->createPayment([
    'payment_type' => 'mobile_money',
    'amount' => 5000,
    'currency' => 'TZS',
    'phone_number' => '255781000000',
    'customer' => [
        'firstname' => 'Jane',
        'lastname' => 'Customer',
        'email' => '[email protected]',
    ],
    'callback_url' => 'https://merchant.example.com/webhooks/moshipay',
    'return_url' => 'https://merchant.example.com/orders/ORD-1001',
    'metadata' => [
        'order_id' => 'ORD-1001',
    ],
], 'ORD-1001');



$payment = $client->getPayment('MP-260709-ABCDEFGH');



http_response_code(200);

$rawBody = file_get_contents('php://input') ?: '';
$timestamp = $_SERVER['HTTP_X_MOSHIPAY_TIMESTAMP'] ?? '';
$signature = $_SERVER['HTTP_X_MOSHIPAY_SIGNATURE'] ?? '';
$webhookSecret = getenv('MOSHIPAY_WEBHOOK_SECRET') ?: null;

if (! $client->verifyWebhookSignature($rawBody, $timestamp, $signature, $webhookSecret)) {
    http_response_code(401);
    echo 'Invalid signature';
    exit;
}

$payload = $client->parseWebhook($rawBody);
$event = $_SERVER['HTTP_X_MOSHIPAY_EVENT'] ?? null;
$reference = $_SERVER['HTTP_X_MOSHIPAY_REFERENCE'] ?? ($payload['moshipay_reference'] ?? null);

// Look up your local order using metadata or reference, then update it idempotently.
// Return a 2xx response only after your handler has completed successfully.



use MoshiPay\Exception\ApiException;
use MoshiPay\Exception\MoshiPayException;
use MoshiPay\Exception\ValidationException;

try {
    $payment = $client->createMobileMoneyPayment(
        amount: 5000,
        currency: 'TZS',
        phoneNumber: '255781000000',
        customer: ['firstname' => 'Jane'],
        idempotencyKey: 'ORD-1001'
    );
} catch (ValidationException $exception) {
    foreach ($exception->errors() as $field => $message) {
        // Handle invalid local input.
    }
} catch (ApiException $exception) {
    $statusCode = $exception->statusCode();
    $apiResponse = $exception->response();
} catch (MoshiPayException $exception) {
    // Non-JSON responses and other SDK-level failures.
}

$payment = $client->createMobileMoneyPayment(
    amount: 5000,
    currency: 'TZS',
    phoneNumber: '255781000000',
    customer: ['firstname' => 'Jane'],
    idempotencyKey: 'ORDER-1001-PAYMENT-1'
);
bash
composer