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