Conditions | 13 |
Paths | 85 |
Total Lines | 134 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function start( Payment $payment ) { |
||
70 | // Amount. |
||
71 | try { |
||
72 | $amount = AmountTransformer::transform( $payment->get_total_amount() ); |
||
73 | } catch ( InvalidArgumentException $e ) { |
||
74 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
75 | |||
76 | return; |
||
77 | } |
||
78 | |||
79 | // Payment method type. |
||
80 | $payment_method_type = PaymentMethodType::transform( $payment->get_method() ); |
||
81 | |||
82 | // Country. |
||
83 | $locale = get_locale(); |
||
84 | |||
85 | $customer = $payment->get_customer(); |
||
86 | |||
87 | if ( null !== $customer ) { |
||
88 | $locale = $customer->get_locale(); |
||
89 | } |
||
90 | |||
91 | $locale = strval( $locale ); |
||
92 | |||
93 | $country_code = Locale::getRegion( $locale ); |
||
94 | |||
95 | // Set country from billing address. |
||
96 | $billing_address = $payment->get_billing_address(); |
||
97 | |||
98 | if ( null !== $billing_address ) { |
||
99 | $country = $billing_address->get_country_code(); |
||
100 | |||
101 | if ( ! empty( $country ) ) { |
||
102 | $country_code = $country; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /* |
||
107 | * API Integration |
||
108 | * |
||
109 | * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments |
||
110 | */ |
||
111 | $api_integration_payment_method_types = array( |
||
112 | PaymentMethodType::IDEAL, |
||
113 | PaymentMethodType::DIRECT_EBANKING, |
||
114 | ); |
||
115 | |||
116 | if ( in_array( $payment_method_type, $api_integration_payment_method_types, true ) ) { |
||
117 | $payment_method = new PaymentMethod( $payment_method_type ); |
||
118 | |||
119 | if ( PaymentMethodType::IDEAL === $payment_method_type ) { |
||
120 | $payment_method = new PaymentMethodIDeal( $payment_method_type, (string) $payment->get_issuer() ); |
||
121 | } |
||
122 | |||
123 | // API integration. |
||
124 | $payment_request = new PaymentRequest( |
||
125 | $amount, |
||
126 | $this->config->get_merchant_account(), |
||
127 | strval( $payment->get_id() ), |
||
128 | $payment->get_return_url(), |
||
129 | $payment_method |
||
130 | ); |
||
131 | |||
132 | $payment_request->set_country_code( $country_code ); |
||
133 | |||
134 | PaymentRequestHelper::complement( $payment, $payment_request ); |
||
135 | |||
136 | try { |
||
137 | $payment_response = $this->client->create_payment( $payment_request ); |
||
138 | } catch ( Exception $e ) { |
||
139 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
140 | |||
141 | return; |
||
142 | } |
||
143 | |||
144 | $payment->set_transaction_id( $payment_response->get_psp_reference() ); |
||
145 | |||
146 | $redirect = $payment_response->get_redirect(); |
||
147 | |||
148 | if ( null !== $redirect ) { |
||
149 | $payment->set_action_url( $redirect->get_url() ); |
||
150 | } |
||
151 | |||
152 | // Return early so SDK integration code will not be executed for API integration. |
||
153 | return; |
||
154 | } |
||
155 | |||
156 | /* |
||
157 | * SDK Integration |
||
158 | * |
||
159 | * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession |
||
160 | */ |
||
161 | $payment_session_request = new PaymentSessionRequest( |
||
162 | $amount, |
||
163 | $this->config->get_merchant_account(), |
||
164 | strval( $payment->get_id() ), |
||
165 | $payment->get_return_url(), |
||
166 | $country_code |
||
167 | ); |
||
168 | |||
169 | PaymentRequestHelper::complement( $payment, $payment_session_request ); |
||
170 | |||
171 | // Origin. |
||
172 | $origin = home_url(); |
||
173 | |||
174 | $origin_url = wp_parse_url( home_url() ); |
||
175 | |||
176 | if ( is_array( $origin_url ) && isset( $origin_url['scheme'], $origin_url['host'] ) ) { |
||
177 | $origin = sprintf( |
||
178 | '%s://%s', |
||
179 | $origin_url['scheme'], |
||
180 | $origin_url['host'] |
||
181 | ); |
||
182 | } |
||
183 | |||
184 | $payment_session_request->set_origin( $origin ); |
||
185 | $payment_session_request->set_sdk_version( self::SDK_VERSION ); |
||
186 | |||
187 | if ( null !== $payment_method_type ) { |
||
188 | $payment_session_request->set_allowed_payment_methods( array( $payment_method_type ) ); |
||
189 | } |
||
190 | |||
191 | try { |
||
192 | $payment_session_response = $this->client->create_payment_session( $payment_session_request ); |
||
193 | } catch ( Exception $e ) { |
||
194 | $this->error = new WP_Error( 'adyen_error', $e->getMessage() ); |
||
195 | |||
196 | return; |
||
197 | } |
||
198 | |||
199 | $payment->set_meta( 'adyen_sdk_version', self::SDK_VERSION ); |
||
200 | $payment->set_meta( 'adyen_payment_session', $payment_session_response->get_payment_session() ); |
||
201 | |||
202 | $payment->set_action_url( $payment->get_pay_redirect_url() ); |
||
203 | } |
||
422 |