|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Diglabby\Doika\Services\PaymentGateways; |
|
4
|
|
|
|
|
5
|
|
|
use Diglabby\Doika\Exceptions\InvalidConfigException; |
|
6
|
|
|
use Diglabby\Doika\Models\Campaign; |
|
7
|
|
|
use Diglabby\Doika\Models\Donator; |
|
8
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
10
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
11
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository; |
|
12
|
|
|
use Money\Money; |
|
13
|
|
|
|
|
14
|
|
|
final class BePaidPaymentGateway implements OffsitePaymentGateway |
|
15
|
|
|
{ |
|
16
|
|
|
private const API_VERSION = '2.1'; |
|
17
|
|
|
private const BASE_API_URI = 'https://checkout.bepaid.by'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ConfigRepository */ |
|
20
|
|
|
private $config; |
|
21
|
|
|
/** @var BePaidApiContext */ |
|
22
|
|
|
private $apiContext; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(BePaidApiContext $apiContext, ConfigRepository $config) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->apiContext = $apiContext; |
|
27
|
|
|
$this->config = $config; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
* @see https://docs.bepaid.by/ru/checkout/payment-token |
|
33
|
|
|
*/ |
|
34
|
|
|
public function tokenizePayment(Money $money, Donator $donator, Campaign $campaign): string |
|
35
|
|
|
{ |
|
36
|
|
|
if ($money->getAmount() < 1) { |
|
37
|
|
|
throw new \InvalidArgumentException('Amount should be a positive number'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$appUrl = $this->config->get('app.url'); |
|
41
|
|
|
$appUrl .= (strpos($appUrl, '?') > 0) ? '&' : '?'; |
|
42
|
|
|
$checkoutParams = [ |
|
43
|
|
|
'checkout' => [ |
|
44
|
|
|
'test' => ! $this->apiContext->live, |
|
45
|
|
|
'transaction_type' => 'payment', |
|
46
|
|
|
'version' => self::API_VERSION, |
|
47
|
|
|
'attempts' => 3, |
|
48
|
|
|
'settings' => [ |
|
49
|
|
|
'success_url' => $appUrl.'status=success', |
|
50
|
|
|
'decline_url' => $appUrl.'status=decline', |
|
51
|
|
|
'fail_url' => $appUrl.'status=fail', |
|
52
|
|
|
'cancel_url' => $appUrl.'status=cancel', |
|
53
|
|
|
'notification_url' => route('webhooks.bepaid.donated', [$campaign->id]), |
|
54
|
|
|
'language' => $this->config->get('app.locale'), |
|
55
|
|
|
], |
|
56
|
|
|
'order' => [ |
|
57
|
|
|
'amount' => $money->getAmount(), |
|
58
|
|
|
'currency' => $money->getCurrency()->getCode(), |
|
59
|
|
|
'description' => sprintf('Donation for "%s" campaign', $campaign->name), |
|
60
|
|
|
], |
|
61
|
|
|
'customer' => [ |
|
62
|
|
|
'email' => $donator->email, |
|
63
|
|
|
], |
|
64
|
|
|
] |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
$httpClient = new HttpClient(['base_uri' => self::BASE_API_URI]); |
|
68
|
|
|
try { |
|
69
|
|
|
$response = $httpClient->request('POST', '/ctp/api/checkouts', [ |
|
70
|
|
|
'auth' => [$this->apiContext->marketId, $this->apiContext->marketKey], |
|
71
|
|
|
'headers' => ['Accept' => 'application/json'], |
|
72
|
|
|
'json' => $checkoutParams, |
|
73
|
|
|
]); |
|
74
|
|
|
} catch (ClientException $exception) { |
|
75
|
|
|
throw new InvalidConfigException("Invalid API request: {$exception->getMessage()}", $exception->getCode(), $exception); |
|
76
|
|
|
} catch (GuzzleException $exception) { |
|
77
|
|
|
throw new \DomainException($exception->getMessage(), $exception->getCode(), $exception); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** @var string $paymentToken */ |
|
81
|
|
|
$paymentToken = json_decode($response->getBody()->getContents())->checkout->token; |
|
82
|
|
|
|
|
83
|
|
|
return $paymentToken; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|