Completed
Push — master ( b96e64...221600 )
by Marcus
02:23
created

Sandbox   B

Complexity

Total Complexity 8

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 1
cbo 17
dl 0
loc 169
rs 7.8571

1 Method

Rating   Name   Duplication   Size   Complexity  
C preparePage() 0 166 8
1
<?php
2
3
/*
4
    HCSF - A multilingual CMS and Shopsystem
5
    Copyright (C) 2014  Marcus Haase - [email protected]
6
7
    This program is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace HaaseIT\HCSF\Controller;
22
23
use PayPal\Api\Amount;
24
use PayPal\Api\Details;
25
use PayPal\Api\Item;
26
use PayPal\Api\ItemList;
27
use PayPal\Api\Payer;
28
use PayPal\Api\Payment;
29
use PayPal\Api\RedirectUrls;
30
use PayPal\Api\Transaction;
31
use PayPal\Api\PaymentExecution;
32
33
class Sandbox extends Base
34
{
35
    public function preparePage()
0 ignored issues
show
Coding Style introduced by
preparePage uses the super-global variable $_GET which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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');
0 ignored issues
show
Unused Code introduced by
$token is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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');
0 ignored issues
show
Unused Code introduced by
$token is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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>'
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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&amp;payerId=$payerId&amp;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