1. Go to this page and download the library: Download machour/clictopay 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/ */
machour / clictopay example snippets
use Machour\ClicToPay\Gateway;
use Machour\ClicToPay\Endpoints\Register;
use Machour\ClicToPay\Exception;
// Initialize the gateway
$gateway = Gateway::make(
login: 'your-username',
password: 'your-password',
testMode: true // Use false for production
);
try {
// Create a payment
$response = $gateway->register(Register::from([
'orderNumber' => 'ORDER-123',
'amount' => 10000, // Amount in millimes (100 TND)
'returnUrl' => 'https://yoursite.com/payment/success',
'description' => 'Order #123',
]));
if ($response->isOk()) {
// Store $response->orderId in your database
// Redirect customer to payment page
header('Location: ' . $response->formUrl);
exit;
}
} catch (Exception $e) {
echo "Payment error: " . $e->getMessage();
}
use Machour\ClicToPay\Endpoints\Register;
$response = $gateway->register(Register::from([
// Required fields
'orderNumber' => 'ORDER-123', // Your unique order number
'amount' => 10000, // Amount in millimes (100 TND)
'returnUrl' => 'https://example.com/success',
// Optional fields
'failUrl' => 'https://example.com/fail',
'description' => 'Order description',
'language' => 'fr', // 'fr' or 'en'
'clientId' => 'client-123',
'jsonParams' => ['email' => '[email protected]'],
'sessionTimeoutSecs' => 1800,
'bindingId' => 'binding-id',
]));
// Response: UrlResponse
echo $response->orderId; // Store this in your database
echo $response->formUrl; // Redirect customer here
use Machour\ClicToPay\Endpoints\PreAuthorize;
$response = $gateway->preAuthorize(PreAuthorize::from([
'orderNumber' => 'ORDER-123',
'amount' => 10000,
'returnUrl' => 'https://example.com/success',
// ... same optional fields as register
]));
// Response: UrlResponse
echo $response->orderId;
echo $response->formUrl;
use Machour\ClicToPay\Endpoints\Deposit;
$response = $gateway->deposit(Deposit::from([
'orderId' => '70906e55-7114-41d6-8332-4609dc6590f4',
'amount' => 10000, // Can be less than pre-authorized amount
]));
// Response: Response
if ($response->isOk()) {
echo "Payment captured successfully";
}
use Machour\ClicToPay\Endpoints\Cancel;
$response = $gateway->cancel(Cancel::from([
'orderId' => '70906e55-7114-41d6-8332-4609dc6590f4',
]));
// Response: Response
if ($response->isOk()) {
echo "Payment cancelled";
}
use Machour\ClicToPay\Endpoints\Refund;
$response = $gateway->refund(Refund::from([
'orderId' => '70906e55-7114-41d6-8332-4609dc6590f4',
'amount' => 5000, // Refund amount (can be partial)
]));
// Response: Response
if ($response->isOk()) {
echo "Refund processed";
}
use Machour\ClicToPay\Endpoints\Status;
$response = $gateway->status(Status::from([
'orderId' => '70906e55-7114-41d6-8332-4609dc6590f4',
]));
// Response: StatusResponse
echo $response->OrderStatus; // 0=registered, 1=held, 2=authorized, etc.
echo $response->OrderNumber; // Your order number
echo $response->Pan; // Masked card number (411111**1111)
echo $response->amount;
echo $response->currency;
echo $response->approvalCode;
echo $response->cardholderName;
echo $response->expiration;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Log\LoggerInterface;
$stack = HandlerStack::create();
$stack->push(Middleware::log($logger, new MessageFormatter()));
$client = new Client([
'handler' => $stack,
]);
$gateway = new Gateway(
login: 'username',
password: 'password',
endpoint: 'https://test.clictopay.com/payment/rest/',
client: $client
);