Conditions | 8 |
Paths | 9 |
Total Lines | 166 |
Code Lines | 101 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
35 | public function preparePage() |
||
|
|||
36 | { |
||
37 | $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager); |
||
38 | $this->P->cb_pagetype = 'content'; |
||
39 | |||
40 | \PayPal\Auth\OAuthTokenCredential::$CACHE_PATH = PATH_CACHE; |
||
41 | \PayPal\Cache\AuthorizationCache::$CACHE_PATH = PATH_CACHE; |
||
42 | |||
43 | |||
44 | $apiContext = new \PayPal\Rest\ApiContext( |
||
45 | new \PayPal\Auth\OAuthTokenCredential( |
||
46 | $this->config->getShop('paypalrestv1')['clientid'], // ClientID |
||
47 | $this->config->getShop('paypalrestv1')['secret'] // ClientSecret |
||
48 | ) |
||
49 | ); |
||
50 | |||
51 | if (!empty($_GET['execute'])) { |
||
52 | $paymentId = filter_input(INPUT_GET, 'paymentId'); |
||
53 | $token = filter_input(INPUT_GET, 'token'); |
||
54 | $payerId = filter_input(INPUT_GET, 'payerId'); |
||
55 | |||
56 | try { |
||
57 | $payment = Payment::get($paymentId, $apiContext); |
||
58 | } catch (\Exception $e) { |
||
59 | $this->P->oPayload->cl_html = print_r($e, true); |
||
60 | } |
||
61 | |||
62 | $execution = new PaymentExecution(); |
||
63 | $execution->setPayerId($payerId); |
||
64 | |||
65 | try { |
||
66 | $result = $payment->execute($execution, $apiContext); |
||
67 | } catch (\Exception $e) { |
||
68 | $this->P->oPayload->cl_html = print_r($e, true); |
||
69 | } |
||
70 | |||
71 | $this->P->oPayload->cl_html = '<pre>'.print_r($result, true).'</pre>'; |
||
72 | |||
73 | } else { |
||
74 | if (empty($_GET['success'])) { |
||
75 | $payer = new Payer(); |
||
76 | $payer->setPaymentMethod("paypal"); |
||
77 | |||
78 | $item1 = new Item(); |
||
79 | $item1->setName('Ground Coffee 40 oz') |
||
80 | ->setCurrency('EUR') |
||
81 | ->setQuantity(1) |
||
82 | ->setSku("123123") // Similar to `item_number` in Classic API |
||
83 | ->setPrice('7'); |
||
84 | |||
85 | $itemList = new ItemList(); |
||
86 | $itemList->setItems(array($item1,)); |
||
87 | |||
88 | $details = new Details(); |
||
89 | $details->setSubtotal('7') |
||
90 | ->setShipping('1') |
||
91 | ->setTax('0') |
||
92 | ; |
||
93 | |||
94 | $amount = new Amount(); |
||
95 | $amount->setCurrency("EUR") |
||
96 | ->setDetails($details) |
||
97 | ->setTotal('8') |
||
98 | ; |
||
99 | |||
100 | $transaction = new Transaction(); |
||
101 | $transaction->setAmount($amount) |
||
102 | ->setItemList($itemList) |
||
103 | ->setDescription("Payment description") |
||
104 | ->setInvoiceNumber(uniqid()) |
||
105 | |||
106 | ; |
||
107 | |||
108 | $baseUrl = 'https://dev13.haase-it.com/_misc/sandbox.html'; |
||
109 | |||
110 | $redirectUrls = new RedirectUrls(); |
||
111 | $redirectUrls->setReturnUrl($baseUrl."?success=true") |
||
112 | ->setCancelUrl($baseUrl."?success=false"); |
||
113 | |||
114 | $payment = new Payment(); |
||
115 | $payment->setIntent("sale") |
||
116 | ->setPayer($payer) |
||
117 | ->setRedirectUrls($redirectUrls) |
||
118 | ->setTransactions(array($transaction)) |
||
119 | ; |
||
120 | |||
121 | |||
122 | try { |
||
123 | $payment->create($apiContext); |
||
124 | } catch (\Exception $e) { |
||
125 | $this->P->oPayload->cl_html = print_r($e, true); |
||
126 | } |
||
127 | |||
128 | $approvalLink = $payment->getApprovalLink(); |
||
129 | |||
130 | $html = <<< HTML |
||
131 | <script src="https://www.paypalobjects.com/webstatic/ppplus/ppplus.min.js" type="text/javascript"></script> |
||
132 | <div id="ppplus"> |
||
133 | </div> |
||
134 | <script type="application/javascript"> |
||
135 | var ppp = PAYPAL.apps.PPP({ |
||
136 | "approvalUrl": "$approvalLink", |
||
137 | "placeholder": "ppplus", |
||
138 | "mode": "sandbox", |
||
139 | "country": "DE" |
||
140 | }); |
||
141 | </script> |
||
142 | HTML; |
||
143 | |||
144 | |||
145 | |||
146 | $this->P->oPayload->cl_html = $html; |
||
147 | |||
148 | |||
149 | |||
150 | } else { |
||
151 | $success = filter_input(INPUT_GET, 'success'); |
||
152 | if ($success === 'true') { |
||
153 | $paymentId = filter_input(INPUT_GET, 'paymentId'); |
||
154 | $token = filter_input(INPUT_GET, 'token'); |
||
155 | $payerId = filter_input(INPUT_GET, 'PayerID'); |
||
156 | |||
157 | try { |
||
158 | $payment = Payment::get($paymentId, $apiContext); |
||
159 | } catch (\Exception $e) { |
||
160 | $this->P->oPayload->cl_html = print_r($e, true); |
||
161 | } |
||
162 | |||
163 | $payer = $payment->getPayer(); |
||
164 | $transactions = $payment->getTransactions(); |
||
165 | $payerinfo = $payer->getPayerInfo(); |
||
166 | $billing = $payerinfo->getBillingAddress(); |
||
167 | $itemlist = $transactions[0]->getItemList(); |
||
168 | $shipping = $itemlist->getShippingAddress(); |
||
169 | $items = $itemlist->getItems(); |
||
170 | $payee = $transactions[0]->getPayee(); |
||
171 | $amount = $transactions[0]->getAmount(); |
||
172 | |||
173 | |||
174 | $debug = '<pre>' |
||
175 | .print_r($shipping, true).'<br><br>' |
||
176 | .print_r($billing, true).'<br><br>' |
||
177 | .print_r($payerinfo, true).'<br><br>' |
||
178 | // .print_r($payer, true).'<br><br>' |
||
179 | // .print_r($transactions, true).'<br><br>' |
||
180 | // .print_r($payment, true).'<br><br>' |
||
181 | .print_r($items, true).'<br><br>' |
||
182 | .print_r($payee, true).'<br><br>' |
||
183 | .print_r($amount, true).'<br><br>' |
||
184 | .'</pre>' |
||
185 | ; |
||
186 | |||
187 | |||
188 | $html = <<< HTML |
||
189 | <a href="/_misc/sandbox.html?execute=true&payerId=$payerId&paymentId=$paymentId" style="background: black; color: white;padding:10px;">Execute Payment</a> |
||
190 | HTML; |
||
191 | |||
192 | |||
193 | $this->P->oPayload->cl_html = $html.'<br><br>'.$debug; |
||
194 | |||
195 | } else { |
||
196 | echo 'false'; |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: