1 | <?php |
||
12 | class CompletePurchaseResponse extends AbstractResponse |
||
13 | { |
||
14 | /** |
||
15 | * Constructor |
||
16 | * |
||
17 | * @param RequestInterface $request the initiating request. |
||
18 | * @param mixed $data |
||
19 | * |
||
20 | * @throws InvalidResponseException If merchant data or order number is missing, or signature does not match |
||
21 | */ |
||
22 | 13 | public function __construct(RequestInterface $request, $data) |
|
23 | { |
||
24 | 13 | parent::__construct($request, $data); |
|
25 | |||
26 | 13 | foreach (array('decision', 'signed_field_names', 'signature') as $key) { |
|
27 | 13 | if (empty($data[$key])) { |
|
28 | 5 | throw new InvalidResponseException('Invalid response from payment gateway (no data)'); |
|
29 | } |
||
30 | } |
||
31 | |||
32 | 8 | $security = new Security(); |
|
33 | |||
34 | 8 | $returnSignature = $security->createSignature( |
|
35 | 8 | $data, |
|
36 | 8 | explode(',', $data['signed_field_names']), |
|
37 | 8 | $this->request->getSecretKey() |
|
|
|||
38 | ); |
||
39 | |||
40 | 8 | if ($returnSignature != $data['signature']) { |
|
41 | 1 | throw new InvalidResponseException('Invalid response from payment gateway (signature mismatch)'); |
|
42 | } |
||
43 | 7 | } |
|
44 | |||
45 | /** |
||
46 | * Is the response successful? |
||
47 | * |
||
48 | * Possible values: 'ACCEPT', 'DECLINE', 'REVIEW', 'ERROR', 'CANCEL' |
||
49 | * |
||
50 | * @return boolean |
||
51 | */ |
||
52 | 7 | public function isSuccessful() |
|
56 | |||
57 | /** |
||
58 | * Get the transaction identifier if available. |
||
59 | * |
||
60 | * @return null|string |
||
61 | */ |
||
62 | 4 | public function getTransactionReference() |
|
66 | |||
67 | /** |
||
68 | * Get the merchant-supplied transaction identifier if available. |
||
69 | * |
||
70 | * @return null|string |
||
71 | */ |
||
72 | 1 | public function getTransactionId() |
|
76 | |||
77 | /** |
||
78 | * Get the authorisation code if available. |
||
79 | * |
||
80 | * @return null|string |
||
81 | */ |
||
82 | 3 | public function getAuthCode() |
|
86 | |||
87 | /** |
||
88 | * Get the reason code if available. |
||
89 | * |
||
90 | * @return null|string |
||
91 | */ |
||
92 | 3 | public function getReasonCode() |
|
96 | |||
97 | /** |
||
98 | * Get the merchant response message if available. |
||
99 | * |
||
100 | * @return null|string |
||
101 | */ |
||
102 | 4 | public function getMessage() |
|
106 | |||
107 | /** |
||
108 | * Get the card number if available. |
||
109 | * |
||
110 | * @return null|string |
||
111 | */ |
||
112 | 3 | public function getCardNumber() |
|
116 | |||
117 | /** |
||
118 | * Get the card type if available. |
||
119 | * |
||
120 | * @return null|string 3-digit number that maps to CyberSource-specific list of card types |
||
121 | */ |
||
122 | 3 | public function getCardType() |
|
126 | |||
127 | /** |
||
128 | * Get the card expiry if available. |
||
129 | * |
||
130 | * @return null|string Format 'mm-yyyy' |
||
131 | */ |
||
132 | 3 | public function getCardExpiry() |
|
136 | |||
137 | /** |
||
138 | * Helper method to get a specific response parameter if available. |
||
139 | * |
||
140 | * @param string $key The key to look up |
||
141 | * |
||
142 | * @return null|mixed |
||
143 | */ |
||
144 | 7 | protected function getKey($key) |
|
148 | } |
||
149 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: