Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:mysiar/omnipay-przelewy24v1
Browse files Browse the repository at this point in the history
  • Loading branch information
mysiar committed May 18, 2022
2 parents ea0a7b7 + 7a03232 commit ad8c822
Show file tree
Hide file tree
Showing 17 changed files with 554 additions and 20 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

## API endpoints implemented

| API endpoint | Gateway method |
|:-----------------------------|:------------------|
| /api/v1/testAccess | testAccess |
| /api/v1/payment/methods | methods |
| /api/v1/transaction/register | purchase |
| /api/v1/transaction/verify | completePurchase |
| API endpoint | Gateway method |
|:---------------------------------|:-----------------|
| /api/v1/testAccess | testAccess |
| /api/v1/payment/methods | methods |
| /api/v1/transaction/register | purchase |
| /api/v1/transaction/verify | completePurchase |
| /api/v1/transaction/by/sessionId | purchaseInfo |

## Install

Expand Down
11 changes: 11 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Przelewy24\Message\CompletePurchaseRequest;
use Omnipay\Przelewy24\Message\MethodsRequest;
use Omnipay\Przelewy24\Message\PurchaseInfoRequest;
use Omnipay\Przelewy24\Message\PurchaseRequest;
use Omnipay\Przelewy24\Message\TestAccessRequest;

Expand Down Expand Up @@ -140,4 +141,14 @@ public function completePurchase(array $options = []): CompletePurchaseRequest
{
return $this->createRequest(CompletePurchaseRequest::class, $options);
}

/**
* @return AbstractRequest|PurchaseInfoRequest
*/
public function purchaseInfo(string $sessionId): PurchaseInfoRequest
{
return $this->createRequest(PurchaseInfoRequest::class, [
'sessionId' => $sessionId,
]);
}
}
10 changes: 8 additions & 2 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ protected function sendRequest(string $method, string $endpoint, $data): Respons
);
}

protected function internalAmountValue(): int
/**
* @param mixed $value
* @throws
*/
protected function internalAmountValue($value = null): int
{
return (int) bcmul($this->getAmount(), '100', 2);
$amount = $value ?? $this->getAmount();

return (int) bcmul((string) $amount, '100', 2);
}
}
5 changes: 5 additions & 0 deletions src/Message/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function isSuccessful(): bool
{
return self::HTTP_OK === $this->getCode();
}

protected function getAmountFromInternal(int $amount): string
{
return bcdiv((string) $amount, '100', 2);
}
}
36 changes: 36 additions & 0 deletions src/Message/PurchaseInfoRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

class PurchaseInfoRequest extends AbstractRequest
{
public function getSessionId(): string
{
return $this->getParameter('sessionId');
}

public function setSessionId(string $value): self
{
return $this->setParameter('sessionId', $value);
}

public function getData(): array
{
$this->validate('sessionId');

return [
'sessionId' => $this->getSessionId(),
];
}

public function sendData($data)
{
$httpResponse = $this->sendRequest('GET', sprintf('transaction/by/sessionId/%s', $data['sessionId']), []);

$responseData = json_decode($httpResponse->getBody()->getContents(), true);

return $this->response = new PurchaseInfoResponse($this, $responseData);
}
}
82 changes: 82 additions & 0 deletions src/Message/PurchaseInfoResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Omnipay\Przelewy24\Message;

use Omnipay\Common\Message\RequestInterface;
use Symfony\Component\HttpFoundation\Response;

class PurchaseInfoResponse extends AbstractResponse
{
/**
* @var string[]
*/
private $info = [];

public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);
if (isset($data['data'])) {
$this->info = $this->formatInfo($data['data']);
}
}

/**
* @return string[]
*/
public function getInfo(): array
{
return $this->info;
}

public function getCode(): int
{
if (isset($this->data['responseCode']) && isset($this->data['error']) && strlen($this->data['error']) > 0) {
return Response::HTTP_NOT_FOUND;
}

if (isset($this->data['code'])) {
return $this->data['code'];
}

return self::HTTP_OK;
}

/**
* @param string[] $data
* @return string[]
*/
private function formatInfo(array $data): array
{
$formatted = $data;

// format
if (isset($formatted['amount'])) {
$formatted['amount'] = $this->getAmountFromInternal((int) $formatted['amount']);
}

// replace keys
$formatted = $this->replaceInfoKeys($formatted, 'clientEmail', 'email');
$formatted = $this->replaceInfoKeys($formatted, 'clientName', 'name');
$formatted = $this->replaceInfoKeys($formatted, 'clientAddress', 'address');
$formatted = $this->replaceInfoKeys($formatted, 'clientCity', 'city');
$formatted = $this->replaceInfoKeys($formatted, 'clientPostcode', 'postcode');

return $formatted;
}

/**
* @param string[] $data
* @return string[]
*/
private function replaceInfoKeys(array $data, string $oldKey, string $newKey): array
{
if (isset($data[$oldKey])) {
$data[$newKey] = $data[$oldKey];
unset($data[$oldKey]);
}

return $data;
}
}
159 changes: 153 additions & 6 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,136 @@ public function setCountry(string $value): self
return $this->setParameter('country', $value);
}

public function getCardNotifyUrl(): ?string
{
return $this->getParameter('cardNotifyUrl');
}

public function setCardNotifyUrl(string $value): self
{
return $this->setParameter('cardNotifyUrl', $value);
}

public function getName(): ?string
{
return $this->getParameter('name');
}

public function setName(string $value): self
{
return $this->setParameter('name', $value);
}

public function getAddress(): ?string
{
return $this->getParameter('address');
}

public function setAddress(string $value): self
{
return $this->setParameter('address', $value);
}

public function getPostcode(): ?string
{
return $this->getParameter('postcode');
}

public function setPostcode(string $value): self
{
return $this->setParameter('postcode', $value);
}

public function getCity(): ?string
{
return $this->getParameter('city');
}

public function setCity(string $value): self
{
return $this->setParameter('city', $value);
}

public function getPhone(): ?string
{
return $this->getParameter('phone');
}

public function setPhone(string $value): self
{
return $this->setParameter('phone', $value);
}

public function getTimeLimit(): ?int
{
return $this->getParameter('timeLimit');
}

public function setTimeLimit(int $value): self
{
return $this->setParameter('timeLimit', $value);
}

public function getWaitForResult(): ?bool
{
return $this->getParameter('waitForResult');
}

public function setWaitForResult(bool $value): self
{
return $this->setParameter('waitForResult', $value);
}

public function getRegulationAccept(): ?bool
{
return $this->getParameter('regulationAccept');
}

public function setRegulationAccept(bool $value): self
{
return $this->setParameter('regulationAccept', $value);
}

public function getShipping()
{
return $this->getParameter('shipping');
}

public function setShipping($value): self
{
return $this->setParameter('regulationAccept', $value);
}

public function getTransactionLabel(): ?string
{
return $this->getParameter('transactionLabel');
}

public function setTransactionLabel(string $value): self
{
return $this->setParameter('transactionLabel', $value);
}

public function getEncoding(): ?string
{
return $this->getParameter('encoding');
}

public function setEncoding(string $value): self
{
return $this->setParameter('encoding', $value);
}

public function getMethodRefId(): ?string
{
return $this->getParameter('methodRefId');
}

public function setMethodRefId(string $value): self
{
return $this->setParameter('methodRefId', $value);
}

public function getData()
{
$this->validate(
Expand All @@ -57,21 +187,38 @@ public function getData()
'currency' => $this->getCurrency(),
'description' => $this->getDescription(),
'email' => $this->getEmail(),
'client' => $this->getName(),
'address' => $this->getAddress(),
'zip' => $this->getPostcode(),
'city' => $this->getCity(),
'country' => $this->getCountry(),
'phone' => $this->getPhone(),
'language' => $this->getLanguage(),
'method' => $this->getPaymentMethod() ? (int) $this->getPaymentMethod() : null,
'urlReturn' => $this->getReturnUrl(),
'urlStatus' => $this->getNotifyUrl(),
'urlCardPaymentNotification' => $this->getCardNotifyUrl(),
'timeLimit' => $this->getTimeLimit(),
'channel' => $this->getChannel(),
'waitForResult' => $this->getWaitForResult(),
'regulationAccept' => $this->getRegulationAccept(),
'shipping' => $this->getShipping() ? $this->internalAmountValue($this->getShipping()) : null,
'transferLabel' => $this->getTransactionLabel(),
'encoding' => $this->getEncoding(),
'methodRefId' => $this->getMethodRefId(),
// alias for credit card
// TODO
// 'cart' => ???
// 'additional' => ???
'sign' => $this->generateSignature(),
];

if (null !== $this->getChannel()) {
$data['channel'] = $this->getChannel();
}

return $data;
return array_filter($data, function ($val) {
return ! is_null($val);
});
}

public function sendData($data)
public function sendData($data): PurchaseResponse
{
$httpResponse = $this->sendRequest('POST', 'transaction/register', $data);

Expand Down
6 changes: 3 additions & 3 deletions src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class PurchaseResponse extends AbstractResponse
{
public function getToken()
public function getToken(): string
{
if (isset($this->data['data'])) {
return trim($this->data['data']['token']);
Expand All @@ -15,12 +15,12 @@ public function getToken()
return '';
}

public function getRedirectUrl()
public function getRedirectUrl(): string
{
return $this->request->getRedirectEndpoint() . 'trnRequest/' . $this->getToken();
}

public function getRedirectMethod()
public function getRedirectMethod(): string
{
return 'GET';
}
Expand Down
Loading

0 comments on commit ad8c822

Please sign in to comment.