6
6
use Drupal \commerce_payment \Entity \PaymentInterface ;
7
7
use Drupal \commerce_payment \Entity \PaymentMethodInterface ;
8
8
use Drupal \commerce_payment \Exception \HardDeclineException ;
9
- use Drupal \commerce_payment \Exception \InvalidRequestException ;
10
9
use Drupal \commerce_payment \PaymentMethodTypeManager ;
11
10
use Drupal \commerce_payment \PaymentTypeManager ;
12
11
use Drupal \commerce_payment \Plugin \Commerce \PaymentGateway \OnsitePaymentGatewayBase ;
@@ -85,16 +84,9 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
85
84
* {@inheritdoc}
86
85
*/
87
86
public function createPayment (PaymentInterface $ payment , $ capture = TRUE ) {
88
- if ($ payment ->getState ()->value != 'new ' ) {
89
- throw new \InvalidArgumentException ('The provided payment is in an invalid state. ' );
90
- }
87
+ $ this ->assertPaymentState ($ payment , ['new ' ]);
91
88
$ payment_method = $ payment ->getPaymentMethod ();
92
- if (empty ($ payment_method )) {
93
- throw new \InvalidArgumentException ('The provided payment has no payment method referenced. ' );
94
- }
95
- if (\Drupal::time ()->getRequestTime () >= $ payment_method ->getExpiresTime ()) {
96
- throw new HardDeclineException ('The provided payment method has expired ' );
97
- }
89
+ $ this ->assertPaymentMethod ($ payment_method );
98
90
99
91
// Add a built in test for testing decline exceptions.
100
92
/** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $billing_address */
@@ -114,8 +106,6 @@ public function createPayment(PaymentInterface $payment, $capture = TRUE) {
114
106
$ remote_id = '123456 ' ;
115
107
116
108
$ payment ->state = $ capture ? 'capture_completed ' : 'authorization ' ;
117
- $ test = $ this ->getMode () == 'test ' ;
118
- $ payment ->setTest ($ test );
119
109
$ payment ->setRemoteId ($ remote_id );
120
110
$ payment ->setAuthorizedTime (\Drupal::time ()->getRequestTime ());
121
111
if ($ capture ) {
@@ -128,9 +118,7 @@ public function createPayment(PaymentInterface $payment, $capture = TRUE) {
128
118
* {@inheritdoc}
129
119
*/
130
120
public function capturePayment (PaymentInterface $ payment , Price $ amount = NULL ) {
131
- if ($ payment ->getState ()->value != 'authorization ' ) {
132
- throw new \InvalidArgumentException ('Only payments in the "authorization" state can be captured. ' );
133
- }
121
+ $ this ->assertPaymentState ($ payment , ['authorization ' ]);
134
122
// If not specified, capture the entire amount.
135
123
$ amount = $ amount ?: $ payment ->getAmount ();
136
124
@@ -149,10 +137,7 @@ public function capturePayment(PaymentInterface $payment, Price $amount = NULL)
149
137
* {@inheritdoc}
150
138
*/
151
139
public function voidPayment (PaymentInterface $ payment ) {
152
- if ($ payment ->getState ()->value != 'authorization ' ) {
153
- throw new \InvalidArgumentException ('Only payments in the "authorization" state can be voided. ' );
154
- }
155
-
140
+ $ this ->assertPaymentState ($ payment , ['authorization ' ]);
156
141
// Perform the void request here, throw an exception if it fails.
157
142
// See \Drupal\commerce_payment\Exception for the available exceptions.
158
143
$ remote_id = $ payment ->getRemoteId ();
@@ -165,16 +150,10 @@ public function voidPayment(PaymentInterface $payment) {
165
150
* {@inheritdoc}
166
151
*/
167
152
public function refundPayment (PaymentInterface $ payment , Price $ amount = NULL ) {
168
- if (!in_array ($ payment ->getState ()->value , ['capture_completed ' , 'capture_partially_refunded ' ])) {
169
- throw new \InvalidArgumentException ('Only payments in the "capture_completed" and "capture_partially_refunded" states can be refunded. ' );
170
- }
153
+ $ this ->assertPaymentState ($ payment , ['capture_completed ' , 'capture_partially_refunded ' ]);
171
154
// If not specified, refund the entire amount.
172
155
$ amount = $ amount ?: $ payment ->getAmount ();
173
- // Validate the requested amount.
174
- $ balance = $ payment ->getBalance ();
175
- if ($ amount ->greaterThan ($ balance )) {
176
- throw new InvalidRequestException (sprintf ("Can't refund more than %s. " , $ balance ->__toString ()));
177
- }
156
+ $ this ->assertRefundAmount ($ payment , $ amount );
178
157
179
158
// Perform the refund request here, throw an exception if it fails.
180
159
// See \Drupal\commerce_payment\Exception for the available exceptions.
0 commit comments