|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Locastic\SyliusHTPayWayPlugin\Behat\Page\External; |
| 4 | + |
| 5 | +use Behat\Mink\Session; |
| 6 | +use Locastic\TcomPayWay\AuthorizeForm\Helpers\SignatureGenerator; |
| 7 | +use Payum\Core\Security\TokenInterface; |
| 8 | +use Sylius\Behat\Page\Page; |
| 9 | +use Sylius\Bundle\CoreBundle\Doctrine\ORM\PaymentRepository; |
| 10 | +use Sylius\Component\Core\Model\Payment; |
| 11 | +use Sylius\Component\Resource\Repository\RepositoryInterface; |
| 12 | +use Symfony\Component\BrowserKit\Client; |
| 13 | + |
| 14 | +final class HTPayWayPaymentPage extends Page |
| 15 | +{ |
| 16 | + private $securityTokenRepository; |
| 17 | + |
| 18 | + private $client; |
| 19 | + |
| 20 | + private $paymentRepository; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + Session $session, |
| 24 | + array $parameters, |
| 25 | + RepositoryInterface $securityTokenRepository, |
| 26 | + Client $client, |
| 27 | + PaymentRepository $paymentRepository |
| 28 | + ) { |
| 29 | + parent::__construct($session, $parameters); |
| 30 | + |
| 31 | + $this->securityTokenRepository = $securityTokenRepository; |
| 32 | + $this->client = $client; |
| 33 | + $this->paymentRepository = $paymentRepository; |
| 34 | + } |
| 35 | + |
| 36 | + public function paySuccessfully(): void |
| 37 | + { |
| 38 | + $token = $this->findToken(); |
| 39 | + |
| 40 | + $data = $this->getSuccessResultData($token); |
| 41 | + |
| 42 | + $this->client->request('POST', $token->getTargetUrl(), $data); |
| 43 | + $this->getDriver()->visit($token->getAfterUrl()); |
| 44 | + } |
| 45 | + |
| 46 | + public function payCanceled(): void |
| 47 | + { |
| 48 | + $token = $this->findToken(); |
| 49 | + |
| 50 | + $data = $this->getFailedResultData($token, 3); |
| 51 | + |
| 52 | + $this->client->request('POST', $token->getTargetUrl(), $data); |
| 53 | + $this->getDriver()->visit($token->getAfterUrl()); |
| 54 | + } |
| 55 | + |
| 56 | + private function getSuccessResultData(TokenInterface $token): array |
| 57 | + { |
| 58 | + /** @var Payment $payment */ |
| 59 | + $payment = $this->paymentRepository->find($token->getDetails()->getId()); |
| 60 | + |
| 61 | + $details = $payment->getDetails(); |
| 62 | + |
| 63 | + $resultData = [ |
| 64 | + 'pgw_merchant_data' => '', |
| 65 | + 'pgw_transaction_id' => 'test-123-123', |
| 66 | + 'pgw_order_id' => $details['pgwOrderId'], |
| 67 | + 'pgw_amount' => $details['pgwAmount'], |
| 68 | + 'pgw_card_type_id' => 1, |
| 69 | + 'pgw_signature' => '', |
| 70 | + 'pgw_installments' => 1, |
| 71 | + 'pgw_trace_ref' => '20000445-4d1618b5c0bc4f4b904231176e301966-20180505160557499', |
| 72 | + ]; |
| 73 | + |
| 74 | + $resultData['pgw_signature'] = SignatureGenerator::generateSignatureFromArray( |
| 75 | + 'test_key', |
| 76 | + $resultData |
| 77 | + ); |
| 78 | + |
| 79 | + return $resultData; |
| 80 | + } |
| 81 | + |
| 82 | + private function getFailedResultData(TokenInterface $token, int $resultCode = 1): array |
| 83 | + { |
| 84 | + /** @var Payment $payment */ |
| 85 | + $payment = $this->paymentRepository->find($token->getDetails()->getId()); |
| 86 | + |
| 87 | + $details = $payment->getDetails(); |
| 88 | + |
| 89 | + $resultData = [ |
| 90 | + 'pgw_merchant_data' => '', |
| 91 | + 'pgw_order_id' => $details['pgwOrderId'], |
| 92 | + 'pgw_result_code' => $resultCode, |
| 93 | + 'pgw_signature' => '', |
| 94 | + 'pgw_trace_ref' => '20000445-4d1618b5c0bc4f4b904231176e301966-20180505160557499', |
| 95 | + ]; |
| 96 | + |
| 97 | + $resultData['pgw_signature'] = SignatureGenerator::generateSignatureFromArray( |
| 98 | + 'test_key', |
| 99 | + $resultData |
| 100 | + ); |
| 101 | + |
| 102 | + return $resultData; |
| 103 | + } |
| 104 | + |
| 105 | + protected function getUrl(array $urlParameters = []): string |
| 106 | + { |
| 107 | + return 'https://pgwtest.ht.hr/services/payment/api/authorize-form'; |
| 108 | + } |
| 109 | + |
| 110 | + private function findToken(string $type = 'capture'): TokenInterface |
| 111 | + { |
| 112 | + $tokens = []; |
| 113 | + |
| 114 | + /** @var TokenInterface $token */ |
| 115 | + foreach ($this->securityTokenRepository->findAll() as $token) { |
| 116 | + if (strpos($token->getTargetUrl(), $type)) { |
| 117 | + $tokens[] = $token; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (count($tokens) > 0) { |
| 122 | + return end($tokens); |
| 123 | + } |
| 124 | + |
| 125 | + throw new \RuntimeException('Cannot find capture token, check if you are after proper checkout steps'); |
| 126 | + } |
| 127 | +} |
0 commit comments