1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Models\PurchaseSessions; |
4
|
|
|
|
5
|
|
|
use ByTIC\Payments\Actions\GatewayNotifications\CreateSessionFromResponse; |
6
|
|
|
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits\CompletePurchaseResponseTrait; |
7
|
|
|
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait; |
8
|
|
|
use ByTIC\Payments\Models\PurchaseSessions\Traits\Cleanup\RecordsTrait as CleanupRecordsTrait; |
9
|
|
|
use ByTIC\Payments\Utility\PaymentsModels; |
10
|
|
|
use Omnipay\Common\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait PurchaseSessionsTrait |
14
|
|
|
* @package ByTIC\Payments\Models\PurchaseSessions |
15
|
|
|
* |
16
|
|
|
* @method PurchaseSessionTrait getNew |
17
|
|
|
*/ |
18
|
|
|
trait PurchaseSessionsTrait |
19
|
|
|
{ |
20
|
|
|
use CleanupRecordsTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $type |
24
|
|
|
* @param CompletePurchaseResponseTrait|ResponseInterface $response |
25
|
|
|
* @return PurchaseSessionTrait |
26
|
|
|
*/ |
27
|
|
|
public function createFromResponse($response, $type) |
28
|
|
|
{ |
29
|
|
|
return CreateSessionFromResponse::handle($response, $response->getModel(), $type); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $type |
34
|
|
|
* @param IsPurchasableModelTrait $payment |
35
|
|
|
* @return PurchaseSessionTrait |
36
|
|
|
*/ |
37
|
|
|
public function createFromPurchase($payment, $type) |
38
|
|
|
{ |
39
|
|
|
$session = $this->generateFromPurchaseType($payment, $type); |
40
|
|
|
$session->insert(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
return $session; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $params |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public static function decodeParams($params) |
50
|
|
|
{ |
51
|
|
|
if (empty($params)) { |
52
|
|
|
return $params; |
53
|
|
|
} |
54
|
|
|
return unserialize(gzuncompress(base64_decode($params))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function encodeParams($params) |
58
|
|
|
{ |
59
|
|
|
return base64_encode(gzcompress(serialize($params))); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function initRelations() |
63
|
|
|
{ |
64
|
|
|
parent::initRelations(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function initRelationsCommon() |
68
|
|
|
{ |
69
|
|
|
$this->initRelationsPurchase(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function initRelationsPurchase() |
73
|
|
|
{ |
74
|
|
|
$this->belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $params |
79
|
|
|
*/ |
80
|
|
|
protected function injectParams(&$params = []) |
81
|
|
|
{ |
82
|
|
|
$params['order'][] = ['created', 'desc']; |
83
|
|
|
|
84
|
|
|
parent::injectParams($params); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param IsPurchasableModelTrait $payment |
89
|
|
|
* @param string $type |
90
|
|
|
* @return PurchaseSessionTrait |
91
|
|
|
*/ |
92
|
|
|
public function generateFromPurchaseType($payment, $type) |
93
|
|
|
{ |
94
|
|
|
$session = $this->generateFromPurchase($payment); |
95
|
|
|
$session->type = $type; |
96
|
|
|
return $session; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param IsPurchasableModelTrait $payment |
101
|
|
|
* @return PurchaseSessionTrait |
102
|
|
|
*/ |
103
|
|
|
protected function generateFromPurchase($payment) |
104
|
|
|
{ |
105
|
|
|
$session = $this->getNew(); |
106
|
|
|
$session->populateFromPayment($payment); |
107
|
|
|
$session->populateFromGateway($payment->getPaymentMethod()->getType()->getGateway()); |
108
|
|
|
$session->populateFromRequest(); |
109
|
|
|
|
110
|
|
|
return $session; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return mixed|\Nip\Config\Config |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
|
|
protected function generateTable() |
118
|
|
|
{ |
119
|
|
|
return config('payments.tables.purchases_sessions', 'purchases_sessions'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|