diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index fb12a15..2165300 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -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); @@ -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( @@ -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']; } @@ -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; + } }