Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 5 | class PurchaseService extends PaymentService{ |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Attempt to make a payment. |
||
| 9 | * |
||
| 10 | * @param array $data returnUrl/cancelUrl + customer creditcard and billing/shipping details. |
||
| 11 | * Some keys (e.g. "amount") are overwritten with data from the associated {@link $payment}. |
||
| 12 | * If this array is constructed from user data (e.g. a form submission), please take care |
||
| 13 | * to whitelist accepted fields, in order to ensure sensitive gateway parameters like "freeShipping" can't be set. |
||
| 14 | * If using {@link Form->getData()}, only fields which exist in the form are returned, |
||
| 15 | * effectively whitelisting against arbitrary user input. |
||
| 16 | * @return ResponseInterface omnipay's response class, specific to the chosen gateway. |
||
| 17 | */ |
||
| 18 | public function purchase($data = array()) { |
||
| 19 | if ($this->payment->Status !== "Created") { |
||
|
|
|||
| 20 | return null; //could be handled better? send payment response? |
||
| 21 | } |
||
| 22 | if (!$this->payment->isInDB()) { |
||
| 23 | $this->payment->write(); |
||
| 24 | } |
||
| 25 | //update success/fail urls |
||
| 26 | $this->update($data); |
||
| 27 | |||
| 28 | //set the client IP address, if not already set |
||
| 29 | if(!isset($data['clientIp'])){ |
||
| 30 | $data['clientIp'] = Controller::curr()->getRequest()->getIP(); |
||
| 31 | } |
||
| 32 | |||
| 33 | $gatewaydata = array_merge($data,array( |
||
| 34 | 'card' => $this->getCreditCard($data), |
||
| 35 | 'amount' => (float) $this->payment->MoneyAmount, |
||
| 36 | 'currency' => $this->payment->MoneyCurrency, |
||
| 37 | //set all gateway return/cancel/notify urls to PaymentGatewayController endpoint |
||
| 38 | 'returnUrl' => $this->getEndpointURL("complete", $this->payment->Identifier), |
||
| 39 | 'cancelUrl' => $this->getEndpointURL("cancel", $this->payment->Identifier), |
||
| 40 | 'notifyUrl' => $this->getEndpointURL("notify", $this->payment->Identifier) |
||
| 41 | )); |
||
| 42 | |||
| 43 | if(!isset($gatewaydata['transactionId'])){ |
||
| 44 | $gatewaydata['transactionId'] = $this->payment->Identifier; |
||
| 45 | } |
||
| 46 | |||
| 47 | $request = $this->oGateway()->purchase($gatewaydata); |
||
| 48 | |||
| 49 | $message = $this->createMessage('PurchaseRequest', $request); |
||
| 50 | $message->SuccessURL = $this->returnurl; |
||
| 51 | $message->FailureURL = $this->cancelurl; |
||
| 52 | $message->write(); |
||
| 53 | |||
| 54 | $gatewayresponse = $this->createGatewayResponse(); |
||
| 55 | try { |
||
| 56 | $response = $this->response = $request->send(); |
||
| 57 | $gatewayresponse->setOmnipayResponse($response); |
||
| 58 | //update payment model |
||
| 59 | if (GatewayInfo::is_manual($this->payment->Gateway)) { |
||
| 60 | //initiate manual payment |
||
| 61 | $this->createMessage('AuthorizedResponse', $response); |
||
| 62 | $this->payment->Status = 'Authorized'; |
||
| 63 | $this->payment->write(); |
||
| 64 | $gatewayresponse->setMessage("Manual payment authorised"); |
||
| 65 | View Code Duplication | } elseif ($response->isSuccessful()) { |
|
| 66 | //successful payment |
||
| 67 | $this->createMessage('PurchasedResponse', $response); |
||
| 68 | $this->payment->Status = 'Captured'; |
||
| 69 | $gatewayresponse->setMessage("Payment successful"); |
||
| 70 | $this->payment->write(); |
||
| 71 | $this->payment->extend('onCaptured', $gatewayresponse); |
||
| 72 | } elseif ($response->isRedirect()) { |
||
| 73 | // redirect to off-site payment gateway |
||
| 74 | $this->createMessage('PurchaseRedirectResponse', $response); |
||
| 75 | $this->payment->Status = 'Authorized'; |
||
| 76 | $this->payment->write(); |
||
| 77 | $gatewayresponse->setMessage("Redirecting to gateway"); |
||
| 78 | View Code Duplication | } else { |
|
| 79 | //handle error |
||
| 80 | $this->createMessage('PurchaseError', $response); |
||
| 81 | $gatewayresponse->setMessage( |
||
| 82 | "Error (".$response->getCode()."): ".$response->getMessage() |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | } catch (Omnipay\Common\Exception\OmnipayException $e) { |
||
| 86 | $this->createMessage('PurchaseError', $e); |
||
| 87 | $gatewayresponse->setMessage($e->getMessage()); |
||
| 88 | } |
||
| 89 | $gatewayresponse->setRedirectURL($this->getRedirectURL()); |
||
| 90 | |||
| 91 | return $gatewayresponse; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Finalise this payment, after off-site external processing. |
||
| 96 | * This is ususally only called by PaymentGatewayController. |
||
| 97 | * @return PaymentResponse encapsulated response info |
||
| 98 | */ |
||
| 99 | public function completePurchase($data = array()) { |
||
| 100 | $gatewayresponse = $this->createGatewayResponse(); |
||
| 101 | |||
| 102 | //set the client IP address, if not already set |
||
| 103 | if(!isset($data['clientIp'])){ |
||
| 104 | $data['clientIp'] = Controller::curr()->getRequest()->getIP(); |
||
| 105 | } |
||
| 106 | |||
| 107 | $gatewaydata = array_merge($data, array( |
||
| 108 | 'amount' => (float) $this->payment->MoneyAmount, |
||
| 109 | 'currency' => $this->payment->MoneyCurrency |
||
| 110 | )); |
||
| 111 | |||
| 112 | $this->payment->extend('onBeforeCompletePurchase', $gatewaydata); |
||
| 113 | |||
| 114 | $request = $this->oGateway()->completePurchase($gatewaydata); |
||
| 115 | $this->createMessage('CompletePurchaseRequest', $request); |
||
| 116 | $response = null; |
||
| 117 | try { |
||
| 118 | $response = $this->response = $request->send(); |
||
| 119 | $gatewayresponse->setOmnipayResponse($response); |
||
| 120 | View Code Duplication | if ($response->isSuccessful()) { |
|
| 121 | $this->createMessage('PurchasedResponse', $response); |
||
| 122 | $this->payment->Status = 'Captured'; |
||
| 123 | $this->payment->write(); |
||
| 124 | $this->payment->extend('onCaptured', $gatewayresponse); |
||
| 125 | } else { |
||
| 126 | $this->createMessage('CompletePurchaseError', $response); |
||
| 127 | } |
||
| 128 | } catch (Omnipay\Common\Exception\OmnipayException $e) { |
||
| 129 | $this->createMessage("CompletePurchaseError", $e); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $gatewayresponse; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function cancelPurchase() { |
||
| 136 | //TODO: do lookup? / try to complete purchase? |
||
| 137 | //TODO: omnipay void call |
||
| 138 | $this->payment->Status = 'Void'; |
||
| 139 | $this->payment->write(); |
||
| 140 | $this->createMessage('VoidRequest', array( |
||
| 141 | "Message" => "The payment was cancelled." |
||
| 142 | )); |
||
| 143 | |||
| 144 | //return response |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return \Omnipay\Common\CreditCard |
||
| 149 | */ |
||
| 150 | protected function getCreditCard($data) { |
||
| 153 | |||
| 154 | } |
||
| 155 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.