| Conditions | 15 |
| Paths | 270 |
| Total Lines | 111 |
| Code Lines | 67 |
| 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 |
||
| 173 | public function start( Payment $payment ) { |
||
| 174 | $payment_method = $payment->get_method(); |
||
| 175 | |||
| 176 | $transaction_description = $payment->get_description(); |
||
| 177 | |||
| 178 | if ( empty( $transaction_description ) ) { |
||
| 179 | $transaction_description = $payment->get_id(); |
||
| 180 | } |
||
| 181 | |||
| 182 | // Merchant. |
||
| 183 | $merchant = new Merchant(); |
||
| 184 | $merchant->account = $this->config->account_id; |
||
| 185 | $merchant->site_id = $this->config->site_id; |
||
| 186 | $merchant->site_secure_code = $this->config->site_code; |
||
| 187 | $merchant->notification_url = $payment->get_return_url(); |
||
| 188 | $merchant->redirect_url = $payment->get_return_url(); |
||
| 189 | $merchant->cancel_url = $payment->get_return_url(); |
||
| 190 | $merchant->close_window = 'false'; |
||
| 191 | |||
| 192 | // Customer. |
||
| 193 | $customer = new Customer(); |
||
| 194 | $customer->ip_address = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP ); |
||
| 195 | $customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP ); |
||
| 196 | |||
| 197 | if ( null !== $payment->get_customer() ) { |
||
| 198 | $name = $payment->get_customer()->get_name(); |
||
| 199 | |||
| 200 | if ( null !== $name ) { |
||
| 201 | $customer->first_name = $name->get_first_name(); |
||
| 202 | $customer->last_name = $name->get_last_name(); |
||
| 203 | } |
||
| 204 | |||
| 205 | $customer->locale = $payment->get_customer()->get_locale(); |
||
| 206 | $customer->email = $payment->get_customer()->get_email(); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Transaction. |
||
| 210 | $transaction = new Transaction(); |
||
| 211 | $transaction->id = uniqid(); |
||
| 212 | $transaction->currency = $payment->get_total_amount()->get_currency()->get_alphabetic_code(); |
||
| 213 | $transaction->amount = $payment->get_total_amount()->get_cents(); |
||
| 214 | $transaction->description = $transaction_description; |
||
| 215 | |||
| 216 | switch ( $payment_method ) { |
||
| 217 | case PaymentMethods::IDEAL: |
||
| 218 | $transaction->gateway = Methods::IDEAL; |
||
| 219 | |||
| 220 | $issuer = $payment->get_issuer(); |
||
| 221 | |||
| 222 | if ( empty( $issuer ) ) { |
||
| 223 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
| 224 | } else { |
||
| 225 | $gateway_info = new GatewayInfo(); |
||
| 226 | |||
| 227 | $gateway_info->issuer_id = $issuer; |
||
| 228 | |||
| 229 | $message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info ); |
||
| 230 | } |
||
| 231 | |||
| 232 | break; |
||
| 233 | case PaymentMethods::CREDIT_CARD: |
||
| 234 | $gateway = Methods::transform( $payment_method ); |
||
| 235 | |||
| 236 | $issuer = $payment->get_issuer(); |
||
| 237 | |||
| 238 | if ( empty( $issuer ) ) { |
||
| 239 | if ( $gateway ) { |
||
| 240 | $transaction->gateway = $gateway; |
||
| 241 | } |
||
| 242 | } else { |
||
| 243 | $transaction->gateway = $issuer; |
||
| 244 | } |
||
| 245 | |||
| 246 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
| 247 | |||
| 248 | break; |
||
| 249 | default: |
||
| 250 | $gateway = Methods::transform( $payment_method ); |
||
| 251 | |||
| 252 | if ( $gateway ) { |
||
| 253 | $transaction->gateway = $gateway; |
||
| 254 | } |
||
| 255 | |||
| 256 | if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) { |
||
| 257 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||
| 258 | $transaction->gateway = $payment_method; |
||
| 259 | } |
||
| 260 | |||
| 261 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
| 262 | } |
||
| 263 | |||
| 264 | $signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id ); |
||
| 265 | |||
| 266 | $message->signature = $signature; |
||
| 267 | |||
| 268 | $response = $this->client->start_transaction( $message ); |
||
| 269 | |||
| 270 | if ( $response ) { |
||
| 271 | $transaction = $response->transaction; |
||
| 272 | |||
| 273 | $payment->set_transaction_id( $transaction->id ); |
||
| 274 | |||
| 275 | if ( isset( $transaction->payment_url ) ) { |
||
| 276 | $payment->set_action_url( $transaction->payment_url ); |
||
| 277 | } |
||
| 278 | |||
| 279 | if ( isset( $response->gateway_info->redirect_url ) ) { |
||
| 280 | $payment->set_action_url( $response->gateway_info->redirect_url ); |
||
| 281 | } |
||
| 282 | } else { |
||
| 283 | $this->error = $this->client->get_error(); |
||
| 284 | } |
||
| 316 |