Skip to content

Commit

Permalink
Blik zero
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jan 19, 2024
1 parent 4354100 commit aeccf57
Showing 1 changed file with 65 additions and 6 deletions.
71 changes: 65 additions & 6 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class OpenApi extends TpayApi
{
public function create(array $data): array
{
if (!empty($data['blikPaymentData'])) {
return $this->createBlikZero($data);
}

$transactionData = $this->handleDataStructure($data);
$transaction = $this->transactions()->createTransaction($transactionData);

Expand All @@ -24,6 +28,27 @@ public function createWithInstantRedirect(array $data): array
return $this->updateRedirectUrl($transaction);
}

public function createBlikZero(array $data): array
{
$transactionData = $this->handleDataStructure($data);
unset($transactionData['pay']);

$transaction = $this->transactions()->createTransactionWithInstantRedirection($transactionData);

$additional_payment_data = [
'channelId' => 64,
'method' => 'pay_by_link',
'blikPaymentData' => [
'type' => 0,
'blikToken' => $data['blikPaymentData']['blikToken'],
],
];

$result = $this->transactions()->createInstantPaymentByTransactionId($additional_payment_data, $transaction['transactionId']);

return $this->updateRedirectUrl($this->waitForBlikAccept($result));
}

public function makeRefund(InfoInterface $payment, float $amount): array
{
return $this->transactions()->createRefundByTransactionId(
Expand Down Expand Up @@ -78,12 +103,6 @@ private function handleDataStructure(array $data): array
],
];

if (!empty($data['blikPaymentData'])) {
$paymentData['pay']['blikPaymentData'] = [
'blikToken' => $data['blikPaymentData']['blikToken'],
];
}

if ($data['group']) {
$paymentData['pay']['groupId'] = $data['group'];
}
Expand All @@ -105,4 +124,44 @@ private function updateRedirectUrl(array $transactionData): array

return $transactionData;
}

private function waitForBlikAccept(array $result): array
{
if ('success' == $result['result']) {
$stop = false;
$i = 0;
do {
$correct = false;
$tpayStatus = $this->transactions()->getTransactionById($result['transactionId']);
$errors = 0;

foreach ($tpayStatus['payments']['attempts'] as $error) {
if ('' != $error['paymentErrorCode']) {
$errors++;
}
}

if ('correct' == $tpayStatus['status']) {
$correct = true;
}

if (60 == $i || $correct) {
$stop = true;
}

if ($errors > 0 && !$correct) {
$stop = true;
$result['payments']['errors'] = $tpayStatus['payments']['attempts'];
}

sleep(1);
$i++;
} while (!$stop);

return $result;
}
$result['payments']['errors'] = [1];

return $result;
}
}

0 comments on commit aeccf57

Please sign in to comment.