Passed
Push — master ( 9e4973...351893 )
by Alexander
03:18
created

printPreformatted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
error_reporting(E_ALL);
5
ini_set('display_errors', '1');
6
7
// Load Composer
8
require_once dirname(__DIR__).'/vendor/autoload.php';
9
10
// Load params from Dotenv
11
$dotenv = new Dotenv\Dotenv(dirname(__DIR__));
12
$dotenv->load();
13
14
$authKey        = getenv('AUTH_KEY');
15
$authSecret     = getenv('AUTH_SECRET');
16
$walletID       = (int)getenv('WALLET');
17
$baseCurrency   = getenv('CURRENCY');
18
$lifetime       = (int)getenv('LIFETIME');
19
20
// Create B2Binpay Provider object
21
$provider = new B2Binpay\Provider($authKey, $authSecret, true);
22
23
/*
24
 * Check $_POST for callback and save it to 'callback.txt'
25
 */
26
if (!empty($_POST)) {
27
    // Get POST headers
28
    $headers = getallheaders();
29
30
    // Check callback Authorization
31
    if (empty($headers['Authorization']) || ($headers['Authorization'] !== $provider->getAuthorization())) {
32
        header('HTTP/1.1 401 Unauthorized');
33
        exit();
34
    }
35
36
    $json_string = json_encode($_POST);
37
38
    // Write it to 'callback.txt'
39
    file_put_contents(
40
        'callback.txt',
41
        date('Y/M/d H:i:s').PHP_EOL.$json_string.PHP_EOL.PHP_EOL,
42
        FILE_APPEND | LOCK_EX
43
    );
44
45
    // Send proper response to callback
46
    header('HTTP/1.1 200 OK');
47
    exit('OK');
48
}
49
50
/*
51
 * If there no callback, let's create test payment
52
 */
53
54
// Get Wallet info
55
$wallet = $provider->getWallet($walletID);
56
57
// Get actual Rates
58
$rates = $provider->getRates($baseCurrency);
59
60
// Convert currency
61
$cleanAmount = $provider->convertCurrency(
62
    '1',
63
    $baseCurrency,
64
    $wallet->currency->alpha,
65
    $rates
66
);
67
68
// Add 10% markup
69
$amount = $provider->addMarkup($cleanAmount, $wallet->currency->alpha, 10);
70
71
// Generate callback Url from $_SERVER
72
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http');
73
$callbackUrl = $protocol.'://'.$_SERVER['HTTP_HOST'];
74
75
// Generate tracking ID from timestamp
76
$trackingID = (string)time();
77
78
// Create Bill
79
$bill = $provider->createBill(
80
    $wallet->id,
81
    $amount,
82
    $wallet->currency->alpha,
83
    $lifetime,
84
    $trackingID,
85
    $callbackUrl
86
);
87
88
// Check Bill
89
$billUpd = $provider->getBill($bill->id);
90
91
echo '<pre>';
92
print_r($wallet);
93
print_r($billUpd);
94
echo '</pre>';
95