forked from Ticketpark/SaferpayJsonApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-initialize.php
79 lines (63 loc) · 2.13 KB
/
example-initialize.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php declare(strict_types=1);
use Ticketpark\SaferpayJson\Request\Exception\SaferpayErrorException;
use Ticketpark\SaferpayJson\Request\PaymentPage\InitializeRequest;
use Ticketpark\SaferpayJson\Request\RequestConfig;
use Ticketpark\SaferpayJson\Request\Container;
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../credentials.php';
// -----------------------------
// Step 1:
// Initialize the required payment page data
// See https://saferpay.github.io/jsonapi/#Payment_v1_PaymentPage_Initialize
$requestConfig = new RequestConfig(
$apiKey,
$apiSecret,
$customerId,
true
);
$amount = new Container\Amount(
5000, // amount in cents
'CHF'
);
$payment = new Container\Payment($amount);
$payment->setDescription('Order No. 12839');
$returnUrls = new Container\ReturnUrls(
'http://www.mysite.ch/success?orderId=12839',
'http://www.mysite.ch/fail'
);
// -----------------------------
// Step 2:
// Create the request with required data
$initializeRequest = new InitializeRequest(
$requestConfig,
$terminalId,
$payment,
$returnUrls
);
// Note: More data can be provided to InitializeRequest with setters,
// for example: $initializeRequest->setPayer()
// See Saferpay documentation for available options.
// -----------------------------
// Step 3:
// Execute and check for successful response
try {
$response = $initializeRequest->execute();
} catch (SaferpayErrorException $e) {
die ($e->getErrorResponse()->getErrorMessage());
}
// -----------------------------
// Step 4:
// Save the response token, you will need it later to verify the payment (see step 7)
echo 'Payment token: ' . $response->getToken() . "<br>\n";
// -----------------------------
// Step 5:
// Redirect to the payment page
echo 'Redirect to: ' . $response->getRedirectUrl() ."<br>\n";
// -----------------------------
// Step 6:
// Fill in test payment page. For dummy credit card numbers see
// https://saferpay.github.io/sndbx/paymentmeans.html
// -----------------------------
// Step 7:
// On success page and notification url, assert that the payment has been successful.
// -> see example-assert.php