/
create_simple_order.php
52 lines (44 loc) · 1.39 KB
/
create_simple_order.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
require_once './vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
use Utrust\ApiClient;
use Utrust\Validator;
// Load the env var API_KEY (using phpdotenv package)
$api_key = getenv('API_KEY');
// Init Utrust API
$utrustApi = new ApiClient($api_key, 'sandbox');
// Build Order data array
$orderData = [
'reference' => 'REF-12345678',
'amount' => [
'total' => '0.99',
'currency' => 'EUR',
],
'return_urls' => [
'return_url' => 'http://example.com/order_success',
'cancel_url' => 'http://example.com/order_canceled',
'callback_url' => 'http://example.com/webhook_url',
],
];
// Build Customer data array
$customerData = [
'first_name' => 'Daniel',
'last_name' => 'Coelho',
'email' => 'daniel+php@utrust.com',
'country' => 'PT',
];
try {
// Validate data
$orderIsValid = Validator::order($orderData);
$customerIsValid = Validator::customer($customerData);
// Make the API request
if ($orderIsValid == true && $customerIsValid == true) {
$response = $utrustApi->createOrder($orderData, $customerData);
}
// Use the $redirect_url to redirect the customer to our Payment Widget
echo $response->attributes->redirect_url;
} catch (Exception $e) {
// Handle error (e.g.: show message to the customer)
echo 'Something went wrong: ' . $e->getMessage();
}