Total Complexity | 41 |
Total Lines | 427 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PaypalGateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PaypalGateway, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class PaypalGateway extends Gateway |
||
32 | { |
||
33 | /** |
||
34 | * Paypal constructor. |
||
35 | */ |
||
36 | public function __construct() |
||
37 | { |
||
38 | parent::__construct(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Returns information about the payment gateway |
||
43 | */ |
||
44 | public function setGatewayInformation() |
||
45 | { |
||
46 | $gateway = []; |
||
47 | $gateway['name'] = 'Paypal'; |
||
48 | $gateway['foldername'] = 'Paypal'; |
||
49 | $gateway['version'] = '1.1'; |
||
50 | $gateway['description'] = 'PayPal is the safer, easier way to pay and get paid online'; |
||
51 | $gateway['author'] = 'Instant Zero (http://www.herve-thouzard.com/)'; |
||
52 | $gateway['credits'] = 'Hervé Thouzard'; |
||
53 | $gateway['releaseDate'] = 20081215; |
||
54 | $this->gatewayInformation = $gateway; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Returns the form used to set up the payment gateway |
||
59 | * |
||
60 | * @param $postUrl |
||
61 | * @return \XoopsThemeForm |
||
62 | */ |
||
63 | public function getParametersForm($postUrl) |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Backing up payment gateway settings |
||
117 | * |
||
118 | * @param array $data The data of the form |
||
119 | * @return bool The result of the data recording |
||
120 | */ |
||
121 | public function saveParametersForm($data) |
||
122 | { |
||
123 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
124 | $gatewaysOptionsHandler = new Oledrion\GatewaysOptionsHandler($db); |
||
125 | $parameters = ['paypal_email', 'paypal_money', 'paypal_test', 'use_ipn']; |
||
126 | // We start by deleting the current values |
||
127 | $gatewayName = $this->gatewayInformation['foldername']; |
||
128 | $gatewaysOptionsHandler->deleteGatewayOptions($gatewayName); |
||
129 | foreach ($parameters as $parameter) { |
||
130 | if (!$gatewaysOptionsHandler->setGatewayOptionValue($gatewayName, $parameter, $data[$parameter])) { |
||
131 | return false; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | return true; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Formats the amount in Paypal format |
||
140 | * @param $amount |
||
141 | * @return string |
||
142 | */ |
||
143 | private function formatAmount($amount) |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Returns the url to which to redirect the user for online payment |
||
150 | * |
||
151 | * @param $cmd_total |
||
152 | * @param $cmd_id |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getRedirectURL($cmd_total, $cmd_id) |
||
156 | { |
||
157 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
158 | $gatewaysOptionsHandler = new Oledrion\GatewaysOptionsHandler($db); |
||
159 | $test_mode = (int)$gatewaysOptionsHandler->getGatewayOptionValue($this->gatewayInformation['foldername'], 'paypal_test'); |
||
160 | if (1 === $test_mode) { |
||
161 | return 'https://www.sandbox.paypal.com/cgi-bin/webscr'; |
||
162 | } |
||
163 | |||
164 | return 'https://www.paypal.com/cgi-bin/webscr'; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Returns the elements to add to the form as hidden areas |
||
169 | * |
||
170 | * @param array $order The sales order |
||
171 | * @param array |
||
172 | * @return array |
||
173 | */ |
||
174 | public function getCheckoutFormContent($order) |
||
175 | { |
||
176 | // global $xoopsConfig; |
||
177 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
178 | $gatewaysOptionsHandler = new Oledrion\GatewaysOptionsHandler($db); |
||
179 | $gatewayName = $this->gatewayInformation['foldername']; |
||
180 | $paypal_money = $gatewaysOptionsHandler->getGatewayOptionValue($gatewayName, 'paypal_money'); |
||
181 | $paypal_email = $gatewaysOptionsHandler->getGatewayOptionValue($gatewayName, 'paypal_email'); |
||
182 | $use_ipn = (int)$gatewaysOptionsHandler->getGatewayOptionValue($gatewayName, 'use_ipn'); |
||
183 | |||
184 | // B.R. Start |
||
185 | // Need array of product_id's for optional DB update |
||
186 | $caddyHandler = new Oledrion\CaddyHandler($db); |
||
187 | $caddy = $caddyHandler->getCaddyFromCommand($order->getVar('cmd_id')); |
||
188 | $products = []; |
||
189 | foreach ($caddy as $item) { |
||
190 | $products[] = $item->getVar('caddy_product_id'); |
||
191 | } |
||
192 | $product_ids = implode(',', $products); |
||
193 | // B.R. End |
||
194 | |||
195 | $ret = []; |
||
196 | $ret['cmd'] = '_xclick'; |
||
197 | $ret['upload'] = '1'; |
||
198 | $ret['currency_code'] = $paypal_money; |
||
199 | $ret['business'] = $paypal_email; |
||
200 | $ret['return'] = OLEDRION_URL . 'thankyou.php'; // (Generic) thank you page after payment |
||
201 | $ret['image_url'] = XOOPS_URL . '/images/logo.gif'; |
||
202 | $ret['cpp_header_image'] = XOOPS_URL . '/images/logo.gif'; |
||
203 | $ret['invoice'] = $order->getVar('cmd_id'); |
||
204 | |||
205 | // B.R. $ret['item_name'] = _OLEDRION_COMMAND . $order->getVar('cmd_id') . ' - ' . Oledrion\Utility::makeHrefTitle($xoopsConfig['sitename']); |
||
206 | // B.R. Start |
||
207 | $ret['item_name'] = $product_ids; |
||
208 | // B.R. End |
||
209 | |||
210 | $ret['item_number'] = $order->getVar('cmd_id'); |
||
211 | $ret['tax'] = 0; // added 25/03/2008 |
||
212 | $ret['amount'] = $this->formatAmount((float)$order->getVar('cmd_total', 'n')); |
||
213 | $ret['custom'] = $order->getVar('cmd_id'); |
||
214 | //$ret['rm'] = 2; // Resend data by POST (normally) |
||
215 | $ret['email'] = $order->getVar('cmd_email'); |
||
216 | if ('' !== xoops_trim($order->getVar('cmd_cancel'))) { |
||
217 | // URL to which the client's browser is brought back if the payment is canceled |
||
218 | $ret['cancel_return'] = OLEDRION_URL . 'cancel-payment.php?id=' . $order->getVar('cmd_cancel'); |
||
219 | } |
||
220 | if (1 === $use_ipn) { |
||
221 | $ret['notify_url'] = OLEDRION_URL . 'gateway-notify.php'; // paypal-notify.php |
||
222 | } |
||
223 | |||
224 | return $ret; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns the list of countries to use in the customer information entry form (checkout.php) |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | public function getCountriesList() |
||
233 | { |
||
234 | require_once XOOPS_ROOT_PATH . '/class/xoopslists.php'; |
||
235 | |||
236 | return \XoopsLists::getCountryList(); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Used during the dialog with Paypal in the case of the use of the IPN |
||
241 | * Note : Specific Paypal |
||
242 | * |
||
243 | * @return string The URL at Paypal to call for information |
||
244 | */ |
||
245 | private function getDialogURL() |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Dialogue with the payment gateway to indicate the status of the order |
||
259 | * The caller is responsible for checking that the log file exists |
||
260 | * |
||
261 | * @param string $gatewaysLogPath The full path to the log file |
||
262 | */ |
||
263 | public function gatewayNotify($gatewaysLogPath) |
||
458 | } |
||
459 | } |
||
460 | } |
||
461 |