-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathinitPaymentApi.php
More file actions
78 lines (68 loc) · 2.49 KB
/
Copy pathinitPaymentApi.php
File metadata and controls
78 lines (68 loc) · 2.49 KB
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
<?php
header('Content-Type: text/html; charset=UTF-8');
/**
* API integration
*
* @link https://help.unitpay.ru/payments/create-payment
*/
use Unitpay\Exception\UnitpayExceptionInterface;
use Unitpay\Model\Enum\PaymentType;
use Unitpay\Unitpay;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/order.php';
$unitpay = new Unitpay($domain, $secretKey);
/**
* initPayment takes its required params as arguments — account, sum, projectId,
* paymentType — and everything else (desc, currency, ...) in the options array.
* paymentType is a payment method code from the reference (the PaymentType constants):
* card, cardInvoice, sbp, sberpay, tinkoffpay, paypal, webmoney.
*
* @link https://help.unitpay.ru/payments/create-payment
* @link https://help.unitpay.ru/book-of-reference/payment-system-codes
*/
try {
$response = $unitpay->payments()->initPayment(
$orderId,
$orderSum,
$projectId,
PaymentType::CARD,
[
'desc' => $orderDesc,
'currency' => $orderCurrency,
]
);
// The initPayment response comes in three types: redirect, invoice, response.
switch ($response->result->type ?? null) {
case 'redirect':
// paymentId is in $response->result->paymentId; save it on your side.
if (isset($response->result->redirectUrl)) {
header('Location: ' . $response->result->redirectUrl);
exit;
}
var_dump($response);
break;
case 'invoice':
// Besides receiptUrl, $response->result->paymentId and ->invoiceId are available.
if (isset($response->result->receiptUrl)) {
header('Location: ' . $response->result->receiptUrl);
exit;
}
var_dump($response);
break;
case 'response':
// No redirect (e.g. a recurring charge); the status is in ->statusUrl.
print $response->result->message ?? '';
break;
default:
// No type — usually an API-level error.
if (isset($response->error->message)) {
print 'Error: ' . $response->error->message;
} else {
var_dump($response);
}
}
} catch (UnitpayExceptionInterface $exception) {
// SDK-side failure: network, disabled allow_url_fopen, malformed JSON, etc.
print 'SDK error: ' . $exception->getMessage();
}