Conditions | 4 |
Paths | 3 |
Total Lines | 142 |
Code Lines | 109 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |
||
24 | function createOrder() |
||
25 | { |
||
26 | // There are 3 objects which are mandatory: User object, ShoppingCart object and Configuration object. |
||
27 | //1. User Object |
||
28 | writeLog('Creating User object'); |
||
29 | writeLog('Adding the address of the user'); |
||
30 | $userAddress = array(); |
||
31 | $userAddress['zip_code'] = '28008'; |
||
32 | $userAddress['full_name'] = 'María Sanchez Escudero'; |
||
33 | $userAddress['country_code'] = 'ES'; |
||
34 | $userAddress['city'] = 'Madrid'; |
||
35 | $userAddress['address'] = 'Paseo de la Castellana, 95'; |
||
36 | $userAddress['dni'] = '59661738Z'; |
||
37 | $userAddress['fix_phone'] = '911231234'; |
||
38 | $userAddress['mobile_phone'] = '600123123'; |
||
39 | $userAddress['national_id'] = '59661738Z'; |
||
40 | |||
41 | $orderBillingAddress = $userAddress; |
||
42 | |||
43 | $orderShippingAddress = array(); |
||
44 | $orderShippingAddress['zip_code'] = '08029'; |
||
45 | $orderShippingAddress['full_name'] = 'Alberto Escudero Sanchez'; |
||
46 | $orderShippingAddress['country_code'] = 'ES'; |
||
47 | $orderShippingAddress['city'] = 'Barcelona'; |
||
48 | $orderShippingAddress['address'] = 'Avenida de la diagonal 525'; |
||
49 | $orderShippingAddress['dni'] = '77695544A'; |
||
50 | $orderShippingAddress['fix_phone'] = '931232345'; |
||
51 | $orderShippingAddress['mobile_phone'] = '600123124'; |
||
52 | $orderShippingAddress['national_id'] = '77695544A'; |
||
53 | |||
54 | writeLog('Adding the purchases of the customer, if there are.'); |
||
55 | $orderHistory = array ( |
||
56 | 0 => array ( |
||
57 | 'date' => '2020-01-31', |
||
58 | 'amount' => 989, |
||
59 | ), |
||
60 | 1 => array ( |
||
61 | 'date' => '2020-01-31', |
||
62 | 'amount' => 898, |
||
63 | ) |
||
64 | ); |
||
65 | |||
66 | writeLog('Adding the information of the user'); |
||
67 | $orderUser = array(); |
||
68 | $orderUser['full_name'] = 'María Sanchez Escudero'; |
||
69 | $orderUser['email'] = '[email protected]'; |
||
70 | $orderUser['date_of_birth'] = '1985-12-30'; |
||
71 | $orderUser['address'] = ''; |
||
72 | $orderUser['dni'] = '59661738Z'; |
||
73 | $orderUser['national_id'] = '59661738Z'; |
||
74 | $orderUser['fix_phone'] = '911231234'; |
||
75 | $orderUser['mobile_phone'] = '600123123'; |
||
76 | $orderUser['address'] = $userAddress; |
||
77 | $orderUser['billing_address'] = $orderBillingAddress; |
||
78 | $orderUser['shipping_address'] = $orderShippingAddress; |
||
79 | $orderUser['order_history'] = $orderHistory; |
||
80 | writeLog('Created User object'); |
||
81 | |||
82 | //2. ShoppingCart Object |
||
83 | writeLog('Creating ShoppingCart object'); |
||
84 | writeLog('Adding cart products. Minimum 1 required'); |
||
85 | |||
86 | $product = array(); |
||
87 | $product['amount'] = '59999'; |
||
88 | $product['quantity'] = 1; |
||
89 | $product['description'] = 'TV LG UltraPlana'; |
||
90 | |||
91 | $details = array(); |
||
92 | $details['shipping_cost'] = 0; |
||
93 | $details['products'][0] = $product; |
||
94 | |||
95 | $orderShoppingCart = array(); |
||
96 | $orderShoppingCart['details'] = $details; |
||
97 | $orderShoppingCart['order_reference'] = ORDER_ID; |
||
98 | $orderShoppingCart['promoted_amount'] = 0; // This amount means that the merchant will asume the interests. |
||
99 | $orderShoppingCart['total_amount'] = 59999; |
||
100 | writeLog('Created OrderShoppingCart object'); |
||
101 | |||
102 | //3. Configuration Object |
||
103 | writeLog('Creating Configuration object'); |
||
104 | writeLog('Adding urls to redirect the user according each case'); |
||
105 | $confirmUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=confirmOrder"; |
||
106 | $errorUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=cancelOrder"; |
||
107 | |||
108 | $orderConfigurationUrls = array(); |
||
109 | $orderConfigurationUrls['cancel'] = $errorUrl; |
||
110 | $orderConfigurationUrls['ko'] = $errorUrl; |
||
111 | $orderConfigurationUrls['authorized_notification_callback'] = $confirmUrl; |
||
112 | $orderConfigurationUrls['rejected_notification_callback'] = $confirmUrl; |
||
113 | $orderConfigurationUrls['ok'] = $confirmUrl; |
||
114 | |||
115 | writeLog('Adding channel info'); |
||
116 | $orderChannel = array(); |
||
117 | $orderChannel['assisted_sale'] = false; |
||
118 | $orderChannel['type'] = 'ONLINE'; |
||
119 | |||
120 | $orderConfiguration = array(); |
||
121 | $orderConfiguration['channel'] = $orderChannel; |
||
122 | $orderConfiguration['urls'] = $orderConfigurationUrls; |
||
123 | writeLog('Created Configuration object'); |
||
124 | |||
125 | $order = array(); |
||
126 | $order['configuration'] = $orderConfiguration; |
||
127 | $order['shopping_cart'] = $orderShoppingCart; |
||
128 | $order['user'] = $orderUser; |
||
129 | |||
130 | writeLog('Preparing connection'); |
||
131 | if (PUBLIC_KEY=='' || PRIVATE_KEY == '') { |
||
|
|||
132 | throw new \Exception('You need set the public and private key'); |
||
133 | } |
||
134 | |||
135 | writeLog('Creating Pagantis order'); |
||
136 | $params_json = json_encode($order); |
||
137 | |||
138 | $cliente = curl_init(); |
||
139 | curl_setopt($cliente, CURLOPT_URL, "https://api.pagamastarde.com/v2/orders/"); |
||
140 | curl_setopt($cliente, CURLOPT_POST, 1); |
||
141 | curl_setopt($cliente, CURLOPT_SSL_VERIFYPEER, false); |
||
142 | curl_setopt($cliente, CURLOPT_POSTFIELDS, $params_json); |
||
143 | curl_setopt($cliente, CURLOPT_RETURNTRANSFER, true); |
||
144 | curl_setopt($cliente, CURLOPT_HTTPHEADER, array( |
||
145 | "Content-Type:application/json", |
||
146 | "Authorization: Bearer ".PRIVATE_KEY |
||
147 | )); |
||
148 | |||
149 | $raw_response = curl_exec($cliente); |
||
150 | $order = json_decode($raw_response); |
||
151 | if (is_object($order)) { |
||
152 | //If the order is correct and created then we have the redirection URL here: |
||
153 | $url = $order->action_urls->form; |
||
154 | $_SESSION['order_id'] = $order->id; |
||
155 | writeLog(json_encode( |
||
156 | $order, |
||
157 | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT |
||
158 | )); |
||
159 | } else { |
||
160 | throw new \Exception('Order not created'); |
||
161 | } |
||
162 | |||
163 | // You can use our test credit cards to fill the Pagantis form |
||
164 | writeLog("Redirecting to Pagantis form => $url"); |
||
165 | header('Location:'. $url); |
||
166 | } |
||
258 |