1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Apache OSL-2 |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\AmazonPay\Business\Order; |
9
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\AmazonpayCallTransfer; |
11
|
|
|
use Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay; |
12
|
|
|
use Propel\Runtime\ActiveQuery\Criteria; |
13
|
|
|
use SprykerEco\Shared\AmazonPay\AmazonPayConfig; |
14
|
|
|
use SprykerEco\Zed\AmazonPay\Business\Converter\AmazonPayTransferToEntityConverterInterface; |
15
|
|
|
use SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface; |
16
|
|
|
|
17
|
|
|
class PaymentProcessorModel implements PaymentProcessorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface |
21
|
|
|
*/ |
22
|
|
|
protected $queryContainer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \SprykerEco\Zed\AmazonPay\Business\Converter\AmazonPayTransferToEntityConverterInterface |
26
|
|
|
*/ |
27
|
|
|
protected $converter; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \SprykerEco\Zed\AmazonPay\Persistence\AmazonPayQueryContainerInterface $queryContainer |
31
|
|
|
* @param \SprykerEco\Zed\AmazonPay\Business\Converter\AmazonPayTransferToEntityConverterInterface $converter |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
AmazonPayQueryContainerInterface $queryContainer, |
35
|
|
|
AmazonPayTransferToEntityConverterInterface $converter |
36
|
|
|
) { |
37
|
|
|
$this->queryContainer = $queryContainer; |
38
|
|
|
$this->converter = $converter; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $orderReferenceId |
43
|
|
|
* @param string $status |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function updateStatus($orderReferenceId, $status) |
48
|
|
|
{ |
49
|
|
|
$payments = $this->getPayments($orderReferenceId); |
50
|
|
|
foreach ($payments as $payment) { |
51
|
|
|
$payment->setStatus($status); |
52
|
|
|
$payment->save(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $orderReferenceId |
58
|
|
|
* |
59
|
|
|
* @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay[]|\Propel\Runtime\Collection\ObjectCollection |
60
|
|
|
*/ |
61
|
|
|
protected function getPayments($orderReferenceId) |
62
|
|
|
{ |
63
|
|
|
return $this->queryContainer->queryPaymentByOrderReferenceId($orderReferenceId) |
64
|
|
|
->filterByStatus(AmazonPayConfig::STATUS_CLOSED, Criteria::NOT_EQUAL) |
65
|
|
|
->find(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay $paymentAmazonPayEntity |
70
|
|
|
* |
71
|
|
|
* @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay |
72
|
|
|
*/ |
73
|
|
|
public function duplicatePaymentEntity(SpyPaymentAmazonpay $paymentAmazonPayEntity) |
74
|
|
|
{ |
75
|
|
|
$newPaymentAmazonpay = new SpyPaymentAmazonpay(); |
76
|
|
|
|
77
|
|
|
$paymentAmazonPayEntity->setAuthorizationReferenceId(null); |
78
|
|
|
$newPaymentAmazonpay->fromArray($paymentAmazonPayEntity->toArray()); |
79
|
|
|
$paymentAmazonPayEntity->setAmazonAuthorizationId(null); |
80
|
|
|
$paymentAmazonPayEntity->save(); |
81
|
|
|
|
82
|
|
|
$newPaymentAmazonpay->setIdPaymentAmazonpay(null); |
83
|
|
|
|
84
|
|
|
return $newPaymentAmazonpay; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer |
89
|
|
|
* |
90
|
|
|
* @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay |
91
|
|
|
*/ |
92
|
|
|
public function createPaymentEntity(AmazonpayCallTransfer $amazonPayCallTransfer) |
93
|
|
|
{ |
94
|
|
|
return $this->converter->mapTransferToEntity($amazonPayCallTransfer->getAmazonpayPayment()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer |
99
|
|
|
* |
100
|
|
|
* @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay|null |
101
|
|
|
*/ |
102
|
|
|
public function loadPaymentEntity(AmazonpayCallTransfer $amazonPayCallTransfer) |
103
|
|
|
{ |
104
|
|
|
$paymentEntity = null; |
105
|
|
|
|
106
|
|
|
if ($amazonPayCallTransfer->getItems()->count()) { |
107
|
|
|
$paymentEntity = $this->queryContainer->queryPaymentBySalesOrderItemId( |
108
|
|
|
$amazonPayCallTransfer->getItems()[0]->getIdSalesOrderItem() |
109
|
|
|
) |
110
|
|
|
->lastCreatedFirst() |
111
|
|
|
->findOne(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($paymentEntity === null) { |
115
|
|
|
$paymentEntity = $this->queryContainer->queryPaymentByOrderReferenceId( |
116
|
|
|
$amazonPayCallTransfer->getAmazonpayPayment()->getOrderReferenceId() |
117
|
|
|
) |
118
|
|
|
->lastCreatedFirst() |
119
|
|
|
->findOne(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $paymentEntity; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay $paymentEntity |
127
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function assignAmazonpayPaymentToItems(SpyPaymentAmazonpay $paymentEntity, AmazonpayCallTransfer $amazonPayCallTransfer) |
132
|
|
|
{ |
133
|
|
|
foreach ($amazonPayCallTransfer->getItems() as $itemTransfer) { |
134
|
|
|
$paymentForItemEntity = $this->queryContainer->queryBySalesOrderItemId($itemTransfer->getIdSalesOrderItem()) |
135
|
|
|
->findOneOrCreate(); |
136
|
|
|
$paymentForItemEntity->setSpyPaymentAmazonpay($paymentEntity); |
137
|
|
|
$paymentForItemEntity->save(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|