| 1 | <?php |
||
| 2 | |||
| 3 | namespace Paytic\Omnipay\Paylike\Message; |
||
| 4 | |||
| 5 | use Paytic\Omnipay\Common\Message\Traits\GatewayNotificationRequestTrait; |
||
| 6 | use Paytic\Omnipay\Paylike\Traits\HasApiTrait; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class CompletePurchaseRequest |
||
| 10 | * @package Paytic\Omnipay\Paylike\Message |
||
| 11 | * |
||
| 12 | * @method CompletePurchaseResponse send() |
||
| 13 | */ |
||
| 14 | class CompletePurchaseRequest extends AbstractRequest |
||
| 15 | { |
||
| 16 | use GatewayNotificationRequestTrait; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 17 | use HasApiTrait; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @return mixed |
||
| 21 | */ |
||
| 22 | public function isValidNotification() |
||
| 23 | { |
||
| 24 | return $this->hasGET('pTransactionId'); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @return bool|mixed |
||
| 29 | */ |
||
| 30 | protected function parseNotification() |
||
| 31 | { |
||
| 32 | $this->validate('privateKey'); |
||
| 33 | |||
| 34 | if ($this->isValidTransaction() == false) { |
||
|
0 ignored issues
–
show
|
|||
| 35 | return false; |
||
| 36 | } |
||
| 37 | |||
| 38 | $transaction = $this->getDataItem('transaction'); |
||
| 39 | |||
| 40 | if ($transaction['pendingAmount'] > 0) { |
||
| 41 | $transaction = $this->makeCaptureRequest( |
||
| 42 | [ |
||
| 43 | 'amount' => $transaction['pendingAmount'] / 100, |
||
| 44 | 'currency' => $transaction['currency'], |
||
| 45 | ] |
||
| 46 | ); |
||
| 47 | } |
||
| 48 | |||
| 49 | return $transaction; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | protected function isValidTransaction() |
||
| 56 | { |
||
| 57 | $transactionId = $this->httpRequest->query->get('pTransactionId'); |
||
| 58 | $this->setTransactionId($transactionId); |
||
| 59 | |||
| 60 | $isValid = false; |
||
| 61 | try { |
||
| 62 | $transactions = $this->getApi()->transactions(); |
||
| 63 | $transaction = $transactions->fetch($transactionId); |
||
| 64 | if (is_array($transaction)) { |
||
|
0 ignored issues
–
show
|
|||
| 65 | $this->setDataItem('transaction', $transaction); |
||
| 66 | $isValid = true; |
||
| 67 | } |
||
| 68 | } catch (\Paylike\Exception\NotFound $e) { |
||
| 69 | $this->setDataItem('message', "The transaction was not found"); |
||
| 70 | } catch (\Paylike\Exception\ApiException $e) { |
||
| 71 | $this->setDataItem('message', "Api Error:" . $e->getMessage()); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($isValid) { |
||
| 75 | $this->setDataItem('success', true); |
||
| 76 | } |
||
| 77 | return $isValid; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param array $parameters |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | protected function makeCaptureRequest(array $parameters) |
||
| 85 | { |
||
| 86 | $request = new CaptureRequest($this->httpClient, $this->httpRequest); |
||
| 87 | $request->initialize(array_replace($this->getParameters(), $parameters)); |
||
| 88 | $response = $request->send(); |
||
| 89 | return $response->getDataProperty('transaction'); |
||
| 90 | } |
||
| 91 | } |
||
| 92 |