|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Actions\GatewayNotifications; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Omnipay\Librapay\Message\ServerCompletePurchaseResponse; |
|
6
|
|
|
use ByTIC\Payments\Utility\PaymentsModels; |
|
7
|
|
|
use Omnipay\Common\Message\AbstractResponse; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class CreateOrUpdateTransactionFromResponse |
|
11
|
|
|
* @package ByTIC\Payments\Actions\GatewayNotifications |
|
12
|
|
|
*/ |
|
13
|
|
|
class CreateOrUpdateTransactionFromResponse |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param $response |
|
17
|
|
|
* @param $model |
|
18
|
|
|
* @param $type |
|
19
|
|
|
* @return \ByTIC\Payments\Models\Transactions\TransactionTrait|\Nip\Records\AbstractModels\Record |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function handle($response, $model, $type) |
|
22
|
|
|
{ |
|
23
|
|
|
$transaction = PaymentsModels::transactions()->findOrCreateForPurchase($model); |
|
24
|
|
|
static::updateTransaction($response, $transaction); |
|
25
|
|
|
return $transaction; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param AbstractResponse|ServerCompletePurchaseResponse $response |
|
31
|
|
|
* @param $transaction |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static function updateTransaction(AbstractResponse $response, $transaction) |
|
34
|
|
|
{ |
|
35
|
|
|
static::setPropertyFromResponse($response, $transaction, 'getCode', 'code'); |
|
36
|
|
|
static::setPropertyFromResponse($response, $transaction, 'getTransactionReference', 'reference'); |
|
37
|
|
|
static::setPropertyFromResponse($response, $transaction, 'getCardMasked', 'card'); |
|
38
|
|
|
|
|
39
|
|
|
$transaction->update(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $response |
|
44
|
|
|
* @param $transaction |
|
45
|
|
|
* @param $method |
|
46
|
|
|
* @param $property |
|
47
|
|
|
*/ |
|
48
|
|
|
protected static function setPropertyFromResponse($response, $transaction, $method, $property) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!method_exists($response, $method)) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
$value = $response->{$method}(); |
|
54
|
|
|
if ($value === null || $value === '') { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
$transaction->{$property} = $value; |
|
58
|
|
|
} |
|
59
|
|
|
} |