amazon_login_pay.php (28916B)
1 <?php 2 class ModelExtensionPaymentAmazonLoginPay extends Model { 3 public function getCountry($iso2) { 4 return $this->db->query("SELECT `country_id`, `name`, `iso_code_2`, `iso_code_3`, `address_format` FROM `" . DB_PREFIX . "country` WHERE `iso_code_2` = '" . $this->db->escape(strtoupper($iso2)) . "' AND `status` = 1 LIMIT 1")->row; 5 } 6 7 public function getZone($name, $country_id) { 8 return $this->db->query("SELECT `zone_id`, `code` FROM `" . DB_PREFIX . "zone` WHERE (LOWER(`name`) LIKE '" . $this->db->escape(strtolower($name)) . "' OR `code` LIKE '" . $this->db->escape(strtolower($name)) . "') AND `country_id` = " . (int)$country_id . " LIMIT 1")->row; 9 } 10 11 public function addTaxesForTotals($order_id, $totals) { 12 foreach ($totals as $total) { 13 $this->db->query("INSERT INTO `" . DB_PREFIX . "amazon_login_pay_order_total_tax` (`order_total_id`, `code`, `tax`) SELECT `order_total_id`, `code`, " . (float)$total['lpa_tax'] . " FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = " . (int)$order_id . " AND `code` = '" . $this->db->escape($total['code']) . "' AND `title` = '" . $this->db->escape($total['title']) . "'"); 14 } 15 } 16 17 public function addCustomer($data) { 18 $customer_group_id = $this->config->get('config_customer_group_id'); 19 20 $this->load->model('account/customer_group'); 21 22 $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id); 23 24 $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$customer_group_id . "', store_id = '" . (int)$this->config->get('config_store_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']['account']) ? json_encode($data['custom_field']['account']) : '') . "', salt = '" . $this->db->escape($salt = token(9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', status = '1', approved = '" . (int)!$customer_group_info['approval'] . "', date_added = NOW()"); 25 26 $customer_id = $this->db->getLastId(); 27 28 $this->load->language('mail/customer'); 29 30 $subject = sprintf($this->language->get('text_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')); 31 32 $message = sprintf($this->language->get('text_welcome'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')) . "\n\n"; 33 34 if (!$customer_group_info['approval']) { 35 $message .= $this->language->get('text_login') . "\n"; 36 } else { 37 $message .= $this->language->get('text_approval') . "\n"; 38 } 39 40 $message .= $this->url->link('account/login', '', true) . "\n\n"; 41 $message .= $this->language->get('text_services') . "\n\n"; 42 $message .= $this->language->get('text_thanks') . "\n"; 43 $message .= html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'); 44 45 $mail = new Mail($this->config->get('config_mail_engine')); 46 $mail->parameter = $this->config->get('config_mail_parameter'); 47 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); 48 $mail->smtp_username = $this->config->get('config_mail_smtp_username'); 49 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); 50 $mail->smtp_port = $this->config->get('config_mail_smtp_port'); 51 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); 52 53 $mail->setTo($data['email']); 54 $mail->setFrom($this->config->get('config_email')); 55 $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')); 56 $mail->setSubject($subject); 57 $mail->setText($message); 58 $mail->send(); 59 60 // Send to main admin email if new account email is enabled 61 if (in_array('account', (array)$this->config->get('config_mail_alert'))) { 62 $message = $this->language->get('text_signup') . "\n\n"; 63 $message .= $this->language->get('text_website') . ' ' . html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8') . "\n"; 64 $message .= $this->language->get('text_firstname') . ' ' . $data['firstname'] . "\n"; 65 $message .= $this->language->get('text_lastname') . ' ' . $data['lastname'] . "\n"; 66 $message .= $this->language->get('text_customer_group') . ' ' . $customer_group_info['name'] . "\n"; 67 $message .= $this->language->get('text_email') . ' ' . $data['email'] . "\n"; 68 $message .= $this->language->get('text_telephone') . ' ' . $data['telephone'] . "\n"; 69 70 $mail = new Mail($this->config->get('config_mail_engine')); 71 $mail->parameter = $this->config->get('config_mail_parameter'); 72 $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); 73 $mail->smtp_username = $this->config->get('config_mail_smtp_username'); 74 $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); 75 $mail->smtp_port = $this->config->get('config_mail_smtp_port'); 76 $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); 77 78 $mail->setTo($this->config->get('config_email')); 79 $mail->setFrom($this->config->get('config_email')); 80 $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')); 81 $mail->setSubject(html_entity_decode($this->language->get('text_new_customer'), ENT_QUOTES, 'UTF-8')); 82 $mail->setText($message); 83 $mail->send(); 84 85 // Send to additional alert emails if new account email is enabled 86 $emails = explode(',', $this->config->get('config_mail_alert_email')); 87 88 foreach ($emails as $email) { 89 if (utf8_strlen($email) > 0 && filter_var($email, FILTER_VALIDATE_EMAIL)) { 90 $mail->setTo($email); 91 $mail->send(); 92 } 93 } 94 } 95 96 return $customer_id; 97 } 98 99 public function getAddress() { 100 $address_paramter_data['AddressConsentToken'] = $this->session->data['access_token']; 101 $address = $this->model_extension_payment_amazon_login_pay->offAmazon('GetOrderReferenceDetails', $address_paramter_data); 102 $xml = simplexml_load_string($address['ResponseBody']); 103 if (isset($xml->GetOrderReferenceDetailsResult->OrderReferenceDetails->Destination->PhysicalDestination)) { 104 return $xml->GetOrderReferenceDetailsResult->OrderReferenceDetails->Destination->PhysicalDestination; 105 } 106 } 107 108 public function addAddress($address) { 109 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "address` WHERE `firstname` = '" . $this->db->escape($address['firstname']) . "' AND `lastname` = '" . $this->db->escape($address['lastname']) . "' AND `address_1` = '" . $this->db->escape($address['address_1']) . "' AND `address_2` = '" . $this->db->escape($address['address_2']) . "' AND `postcode` = '" . $this->db->escape($address['postcode']) . "' AND `city` = '" . $this->db->escape($address['city']) . "' AND `zone_id` = '" . $this->db->escape($address['zone_id']) . "' AND `country_id` = '" . $this->db->escape($address['country_id']) . "'"); 110 if (!$query->num_rows) { 111 $this->load->model('account/address'); 112 $this->model_account_address->addAddress($this->session->data['lpa']['address']); 113 } 114 } 115 116 public function setOrderShipping($order_id, $has_free_shipping) { 117 $this->db->query("INSERT INTO `" . DB_PREFIX . "amazon_login_pay_order` SET `order_id` = '" . (int)$order_id . "', `free_shipping` = '" . (int)$has_free_shipping . "',`date_added` = now(), `modified` = now() "); 118 return $this->db->getLastId(); 119 } 120 121 public function hasFreeShipping($order_id) { 122 return $this->db->query("SELECT `free_shipping` FROM `" . DB_PREFIX . "amazon_login_pay_order` WHERE `order_id` = " . (int)$order_id)->row['free_shipping'] == '1'; 123 } 124 125 public function getShippingPrice($order_id) { 126 $query = $this->db->query("SELECT `value` + IF(`tax` IS NULL, 0, `tax`) AS 'price' FROM `" . DB_PREFIX . "order_total` `ot` LEFT JOIN `" . DB_PREFIX . "amazon_login_pay_order_total_tax` `ott` USING(`order_total_id`) WHERE `ot`.`code` = 'shipping' AND `order_id` = " . (int)$order_id); 127 if ($query->num_rows) { 128 return $query->row['price']; 129 } 130 } 131 132 public function getAdditionalCharges($order_id) { 133 return $this->db->query("SELECT `ot`.`title`, `ot`.`order_total_id`, `value` + IF(`tax` IS NULL, 0, `tax`) AS 'price' FROM `" . DB_PREFIX . "order_total` `ot` LEFT JOIN `" . DB_PREFIX . "amazon_login_pay_order_total_tax` `ott` USING(`order_total_id`) WHERE `ott`.`code` NOT IN ('shipping', 'total', 'sub_total', 'tax') AND `order_id` = " . (int)$order_id)->rows; 134 } 135 136 public function addAmazonOrderId($order_id, $amazon_authorization_id, $capture_status, $total, $currency_code) { 137 $this->db->query("UPDATE `" . DB_PREFIX . "amazon_login_pay_order` SET `amazon_order_reference_id` = '" . $this->db->escape($this->session->data['lpa']['AmazonOrderReferenceId']) . "', `amazon_authorization_id` = '" . $this->db->escape($amazon_authorization_id) . "', `modified` = now(), `capture_status` = '" . $this->db->escape($capture_status) . "', `currency_code` = '" . $this->db->escape($currency_code) . "', `total` = '" . $total . "' WHERE `order_id` = '" . (int)$order_id . "'"); 138 } 139 140 public function addTransaction($amazon_login_pay_order_id, $amazon_authorization_id, $amazon_capture_id, $type, $status, $total) { 141 $this->db->query("INSERT INTO `" . DB_PREFIX . "amazon_login_pay_order_transaction` SET `amazon_login_pay_order_id` = '" . (int)$amazon_login_pay_order_id . "', `amazon_authorization_id` = '" . $this->db->escape($amazon_authorization_id) . "', `amazon_capture_id` = '" . $this->db->escape($amazon_capture_id) . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `status` = '" . $this->db->escape($status) . "', `amount` = '" . $total . "'"); 142 } 143 144 public function closeOrderRef($amazon_order_reference_id) { 145 $close_paramter_data = array(); 146 $close_paramter_data['AmazonOrderReferenceId'] = $amazon_order_reference_id; 147 $close_details = $this->offAmazon('CloseOrderReference', $close_paramter_data); 148 $this->validateResponse('CloseOrderReference', $close_details); 149 } 150 151 public function sendOrder($order_id, $total, $currency_code) { 152 153 $update_paramter_data = array(); 154 $update_paramter_data['OrderReferenceAttributes.OrderTotal.Amount'] = $total; 155 $update_paramter_data['OrderReferenceAttributes.OrderTotal.CurrencyCode'] = $currency_code; 156 $update_paramter_data['OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId'] = $order_id; 157 $update_paramter_data['OrderReferenceAttributes.SellerOrderAttributes.StoreName'] = $this->config->get('config_name'); 158 if ($this->config->get('payment_amazon_login_pay_payment_region') == 'USD') { 159 $update_paramter_data['OrderReferenceAttributes.PlatformId'] = 'A3GK1RS09H3A7D'; 160 } else { 161 $update_paramter_data['OrderReferenceAttributes.PlatformId'] = 'A3EIRX2USI2KJV'; 162 } 163 164 $address_paramter_data['AddressConsentToken'] = $this->session->data['access_token']; 165 $address = $this->offAmazon('GetOrderReferenceDetails', $address_paramter_data); 166 $address_details_response = simplexml_load_string($address['ResponseBody']); 167 168 $reason_code = (string)$address_details_response->GetOrderReferenceDetailsResult->OrderReferenceDetails->OrderReferenceStatus->ReasonCode; 169 170 if ($reason_code != 'InvalidPaymentMethod') { 171 $update_details = $this->offAmazon('SetOrderReferenceDetails', $update_paramter_data); 172 $update_details_response = $this->validateResponse('Update', $update_details); 173 if ($update_details_response['redirect']) { 174 return $update_details_response; 175 } 176 } 177 178 $confirm_details = $this->offAmazon('ConfirmOrderReference'); 179 $confirm_details_response = $this->validateResponse('Confirm', $confirm_details); 180 if ($confirm_details_response['redirect']) { 181 return $confirm_details_response; 182 } 183 184 $response['capture_status'] = 0; 185 $authorize_paramter_data = array(); 186 if ($this->config->get('payment_amazon_login_pay_mode') == 'payment') { 187 $authorize_paramter_data['CaptureNow'] = true; 188 $authorize_paramter_data['CaptureReferenceId'] = 'capture_' . mt_rand(); 189 $response['capture_status'] = 1; 190 } 191 192 if ($this->config->get('payment_amazon_login_pay_declined_code')) { 193 $authorize_paramter_data['SellerAuthorizationNote'] = '{"SandboxSimulation": {"State":"Declined", "ReasonCode":"' . $this->config->get('payment_amazon_login_pay_declined_code') . '"}}'; 194 } 195 196 $authorize_paramter_data['AuthorizationAmount.Amount'] = $total; 197 $authorize_paramter_data['AuthorizationAmount.CurrencyCode'] = $currency_code; 198 $authorize_paramter_data['AuthorizationReferenceId'] = 'auth_' . mt_rand(); 199 $authorize_paramter_data['TransactionTimeout'] = 0; 200 $authorize_details = $this->offAmazon('Authorize', $authorize_paramter_data); 201 $authorize_details_response = $this->validateResponse('Authorize', $authorize_details); 202 if (isset($authorize_details_response['redirect'])) { 203 return $authorize_details_response; 204 } 205 206 $response['amazon_authorization_id'] = (string)$authorize_details_response->AuthorizeResult->AuthorizationDetails->AmazonAuthorizationId; 207 $response['status'] = (string)$authorize_details_response->AuthorizeResult->AuthorizationDetails->AuthorizationStatus->State; 208 if (isset($authorize_details_response->AuthorizeResult->AuthorizationDetails->IdList->member)) { 209 $response['amazon_capture_id'] = (string)$authorize_details_response->AuthorizeResult->AuthorizationDetails->IdList->member; 210 } 211 212 if (isset($authorize_details_response->AuthorizeResult->AuthorizationDetails->AuthorizationBillingAddress)) { 213 $response['billing_address'] = $authorize_details_response->AuthorizeResult->AuthorizationDetails->AuthorizationBillingAddress; 214 } 215 216 return $response; 217 } 218 219 public function editOrder($order_id, $order) { 220 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET payment_firstname = '" . $this->db->escape($order['payment_firstname']) . "', payment_lastname = '" . $this->db->escape($order['payment_lastname']) . "', payment_address_1 = '" . $this->db->escape($order['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($order['payment_address_2']) . "', payment_city = '" . $this->db->escape($order['payment_city']) . "', payment_zone = '" . $this->db->escape($order['payment_zone']) . "', payment_zone_id = " . (int)$order['payment_zone_id'] . ", payment_country = '" . $this->db->escape($order['payment_country']) . "', payment_country_id = " . (int)$order['payment_country_id'] . ", payment_postcode = '" . $this->db->escape($order['payment_postcode']) . "' WHERE order_id = " . (int)$order_id); 221 } 222 223 public function updateStatus($amazon_id, $type, $status) { 224 $this->db->query("UPDATE `" . DB_PREFIX . "amazon_login_pay_order_transaction` SET `status` = '" . $this->db->escape($status) . "' WHERE `amazon_" . $type . "_id` = '" . $this->db->escape($amazon_id) . "' AND `type` = '" . $this->db->escape($type) . "'"); 225 } 226 227 public function authorizationIpn($xml) { 228 $status = (string)$xml->AuthorizationDetails->AuthorizationStatus->State; 229 $amazon_authorization_id = (string)$xml->AuthorizationDetails->AmazonAuthorizationId; 230 $this->updateStatus($amazon_authorization_id, 'authorization', $status); 231 if ($status == 'Declined' || $status == 'Closed') { 232 $this->logger($status . ': ' . (string)$xml->AuthorizationDetails->AuthorizationStatus->ReasonCode); 233 } 234 } 235 236 public function captureIpn($xml) { 237 $status = (string)$xml->CaptureDetails->CaptureStatus->State; 238 $amazon_capture_id = (string)$xml->CaptureDetails->AmazonCaptureId; 239 $this->updateStatus($amazon_capture_id, 'capture', $status); 240 if ($status == 'Declined' || $status == 'Canceled' || $status == 'Closed') { 241 $this->logger($status . ': ' . (string)$xml->CaptureDetails->CaptureStatus->ReasonCode); 242 } 243 } 244 245 public function refundIpn($xml) { 246 $status = (string)$xml->RefundDetails->RefundStatus->State; 247 $amazon_refund_id = (string)$xml->RefundDetails->AmazonRefundId; 248 $this->updateStatus($amazon_refund_id, 'refund', $status); 249 if ($status == 'Declined') { 250 $this->logger($status . ': ' . (string)$xml->RefundDetails->RefundStatus->ReasonCode); 251 } 252 } 253 254 public function capture($amazon_login_pay_order) { 255 $this->logger($amazon_login_pay_order); 256 $this->logger(count($amazon_login_pay_order['transactions'])); 257 if (count($amazon_login_pay_order['transactions']) == 1) { 258 $capture_paramter_data = array(); 259 $capture_paramter_data['AmazonOrderReferenceId'] = $amazon_login_pay_order['amazon_order_reference_id']; 260 $capture_paramter_data['AmazonAuthorizationId'] = $amazon_login_pay_order['amazon_authorization_id']; 261 $capture_paramter_data['CaptureAmount.Amount'] = $amazon_login_pay_order['total']; 262 $capture_paramter_data['CaptureAmount.CurrencyCode'] = $amazon_login_pay_order['currency_code']; 263 $capture_paramter_data['CaptureReferenceId'] = 'capture_' . mt_rand(); 264 $capture_paramter_data['TransactionTimeout'] = 0; 265 266 $capture_details = $this->offAmazon('Capture', $capture_paramter_data); 267 $capture_response = $this->validateResponse('Capture', $capture_details); 268 if (isset($capture_response->CaptureResult)) { 269 $response['status'] = (string)$capture_response->CaptureResult->CaptureDetails->CaptureStatus->State; 270 $response['amazon_capture_id'] = (string)$capture_response->CaptureResult->CaptureDetails->AmazonCaptureId; 271 return $response; 272 } 273 274 return $capture_response; 275 } else { 276 return false; 277 } 278 } 279 280 public function updateCaptureStatus($amazon_login_pay_order_id, $status) { 281 $this->db->query("UPDATE `" . DB_PREFIX . "amazon_login_pay_order` SET `capture_status` = '" . (int)$status . "' WHERE `amazon_login_pay_order_id` = '" . (int)$amazon_login_pay_order_id . "'"); 282 } 283 284 public function getOrder($order_id) { 285 286 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_login_pay_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 287 288 if ($qry->num_rows) { 289 $order = $qry->row; 290 $order['transactions'] = $this->getTransactions($order['amazon_login_pay_order_id'], $qry->row['currency_code']); 291 return $order; 292 } else { 293 return false; 294 } 295 } 296 297 private function getTransactions($amazon_login_pay_order_id, $currency_code) { 298 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_login_pay_order_transaction` WHERE `amazon_login_pay_order_id` = '" . (int)$amazon_login_pay_order_id . "'"); 299 300 $transactions = array(); 301 302 if ($query->num_rows) { 303 foreach ($query->rows as $row) { 304 $row['amount'] = $this->currency->format($row['amount'], $currency_code, true, true); 305 306 $transactions[] = $row; 307 } 308 return $transactions; 309 } else { 310 return false; 311 } 312 } 313 314 public function getUserInfo($access_token) { 315 if ($this->config->get('payment_amazon_login_pay_test') == 'sandbox') { 316 if ($this->config->get('payment_amazon_login_pay_payment_region') == 'GBP') { 317 $curl_token = curl_init('https://api.sandbox.amazon.co.uk/auth/o2/tokeninfo?access_token=' . urlencode($access_token)); 318 $curl_profile = curl_init('https://api.sandbox.amazon.co.uk/user/profile'); 319 } elseif ($this->config->get('payment_amazon_login_pay_payment_region') == 'EUR') { 320 $curl_token = curl_init('https://api.sandbox.amazon.de/auth/o2/tokeninfo?access_token=' . urlencode($access_token)); 321 $curl_profile = curl_init('https://api.sandbox.amazon.de/user/profile'); 322 } else { 323 $curl_token = curl_init('https://api.sandbox.amazon.com/auth/o2/tokeninfo?access_token=' . urlencode($access_token)); 324 $curl_profile = curl_init('https://api.sandbox.amazon.com/user/profile'); 325 } 326 } else { 327 if ($this->config->get('payment_amazon_login_pay_payment_region') == 'GBP') { 328 $curl_token = curl_init('https://api.amazon.co.uk/auth/o2/tokeninfo?access_token=' . urlencode($access_token)); 329 $curl_profile = curl_init('https://api.amazon.co.uk/user/profile'); 330 } elseif ($this->config->get('payment_amazon_login_pay_payment_region') == 'EUR') { 331 $curl_token = curl_init('https://api.amazon.de/auth/o2/tokeninfo?access_token=' . urlencode($access_token)); 332 $curl_profile = curl_init('https://api.amazon.de/user/profile'); 333 } else { 334 $curl_token = curl_init('https://api.amazon.com/auth/o2/tokeninfo?access_token=' . urlencode($access_token)); 335 $curl_profile = curl_init('https://api.amazon.com/user/profile'); 336 } 337 } 338 339 curl_setopt($curl_token, CURLOPT_RETURNTRANSFER, true); 340 341 $response_token = curl_exec($curl_token); 342 curl_close($curl_token); 343 $decoded_token = json_decode($response_token); 344 345 if (!isset($decoded_token->aud) || $decoded_token->aud != $this->config->get('payment_amazon_login_pay_client_id')) { 346 $this->logger($decoded_token); 347 $this->logger('the access token does not belong to us'); 348 return; 349 } 350 351 curl_setopt($curl_profile, CURLOPT_HTTPHEADER, array('Authorization: bearer ' . $access_token)); 352 curl_setopt($curl_profile, CURLOPT_RETURNTRANSFER, true); 353 354 $response_profile = curl_exec($curl_profile); 355 curl_close($curl_profile); 356 $decoded_profile = json_decode($response_profile); 357 358 return $decoded_profile; 359 } 360 361 private function validateResponse($action, $details) { 362 $details_xml = simplexml_load_string($details['ResponseBody']); 363 $this->logger($action); 364 $this->logger($details_xml); 365 366 $details_xml->registerXPathNamespace('m', 'http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01'); 367 $error_set = $details_xml->xpath('//m:ReasonCode'); 368 369 if (isset($details_xml->Error)) { 370 $this->logger($action . ' : ' . (string)$details_xml->Error->Message); 371 $response['redirect'] = 'failure'; 372 return $response; 373 } elseif (!empty($error_set)) { 374 $reason_code = (string)$error_set[0]; 375 $response['redirect'] = 'failure'; 376 if ($reason_code == 'InvalidPaymentMethod') { 377 $response['redirect'] = 'paymentMethod'; 378 } elseif ($reason_code == 'MaxCapturesProcessed') { 379 return $details_xml; 380 } 381 $this->logger($action . ' : ' . $reason_code); 382 return $response; 383 } 384 return $details_xml; 385 } 386 387 public function offAmazon($Action, $parameter_data = array()) { 388 if ($this->config->get('payment_amazon_login_pay_test') == 'sandbox') { 389 if ($this->config->get('payment_amazon_login_pay_payment_region') == 'USD') { 390 $url = 'https://mws.amazonservices.com/OffAmazonPayments_Sandbox/2013-01-01/'; 391 } else { 392 $url = 'https://mws-eu.amazonservices.com/OffAmazonPayments_Sandbox/2013-01-01/'; 393 } 394 } else { 395 if ($this->config->get('payment_amazon_login_pay_payment_region') == 'USD') { 396 $url = 'https://mws.amazonservices.com/OffAmazonPayments/2013-01-01/'; 397 } else { 398 $url = 'https://mws-eu.amazonservices.com/OffAmazonPayments/2013-01-01/'; 399 } 400 } 401 402 $parameters = array(); 403 $parameters['AWSAccessKeyId'] = $this->config->get('payment_amazon_login_pay_access_key'); 404 $parameters['Action'] = $Action; 405 if (isset($parameter_data['AmazonOrderReferenceId'])) { 406 $parameters['AmazonOrderReferenceId'] = $parameter_data['AmazonOrderReferenceId']; 407 } else { 408 $parameters['AmazonOrderReferenceId'] = $this->session->data['lpa']['AmazonOrderReferenceId']; 409 } 410 $parameters['SellerId'] = $this->config->get('payment_amazon_login_pay_merchant_id'); 411 $parameters['SignatureMethod'] = 'HmacSHA256'; 412 $parameters['SignatureVersion'] = 2; 413 $parameters['Timestamp'] = date('c', time()); 414 $parameters['Version'] = '2013-01-01'; 415 foreach ($parameter_data as $k => $v) { 416 $parameters[$k] = $v; 417 } 418 419 $query = $this->calculateStringToSignV2($parameters, $url); 420 421 $parameters['Signature'] = base64_encode(hash_hmac('sha256', $query, $this->config->get('payment_amazon_login_pay_access_secret'), true)); 422 423 return $this->sendCurl($url, $parameters); 424 } 425 426 public function sendCurl($url, $parameters) { 427 $query = $this->getParametersAsString($parameters); 428 429 $curl = curl_init($url); 430 431 curl_setopt($curl, CURLOPT_URL, $url); 432 curl_setopt($curl, CURLOPT_PORT, 443); 433 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 434 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 435 curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']); 436 curl_setopt($curl, CURLOPT_POST, true); 437 curl_setopt($curl, CURLOPT_POSTFIELDS, $query); 438 curl_setopt($curl, CURLOPT_HEADER, true); 439 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 440 441 $response = curl_exec($curl); 442 curl_close($curl); 443 444 list($other, $responseBody) = explode("\r\n\r\n", $response, 2); 445 $other = preg_split("/\r\n|\n|\r/", $other); 446 447 list($protocol, $code, $text) = explode(' ', trim(array_shift($other)), 3); 448 449 return array('Status' => (int)$code, 'ResponseBody' => $responseBody); 450 } 451 452 private function getParametersAsString(array $parameters) { 453 $queryParameters = array(); 454 foreach ($parameters as $key => $value) { 455 $queryParameters[] = $key . '=' . $this->urlencode($value); 456 } 457 return implode('&', $queryParameters); 458 } 459 460 private function calculateStringToSignV2(array $parameters, $url) { 461 $data = 'POST'; 462 $data .= "\n"; 463 $endpoint = parse_url($url); 464 $data .= $endpoint['host']; 465 $data .= "\n"; 466 $uri = array_key_exists('path', $endpoint) ? $endpoint['path'] : null; 467 if (!isset($uri)) { 468 $uri = "/"; 469 } 470 $uriencoded = implode("/", array_map(array($this, "urlencode"), explode("/", $uri))); 471 $data .= $uriencoded; 472 $data .= "\n"; 473 uksort($parameters, 'strcmp'); 474 $data .= $this->getParametersAsString($parameters); 475 return $data; 476 } 477 478 private function urlencode($value) { 479 return str_replace('%7E', '~', rawurlencode($value)); 480 } 481 482 public function parseRawMessage($body) { 483 $snsMessage = $this->buildMessage($body); 484 $ipnMessage = $this->getField("Message", $snsMessage); 485 486 return $this->parseIpnMessage($ipnMessage); 487 } 488 489 private function buildMessage($json) { 490 $message = json_decode($json, true); 491 $json_error = json_last_error(); 492 493 if ($json_error != 0) { 494 $errorMsg = "Error with message - content is not in json format" . 495 $json_error . " " . 496 $json; 497 $this->logger($errorMsg); 498 } 499 return $message; 500 } 501 502 private function parseIpnMessage($ipnMsg) { 503 $xmlDocumentElement = $this->getXmlFromIpnMessage($ipnMsg); 504 return $xmlDocumentElement; 505 } 506 507 private function getXmlFromIpnMessage($ipnMsg) { 508 $notificationData = $this->getField("NotificationData", $this->buildMessage($ipnMsg)); 509 libxml_use_internal_errors(true); 510 try { 511 $xml = simplexml_load_string($notificationData); 512 } catch (Exception $ex) { 513 $this->logger($notificationData); 514 } 515 return $xml; 516 } 517 518 private function getField($fieldName, $snsMessage) { 519 if (array_key_exists($fieldName, $snsMessage)) { 520 return $snsMessage[$fieldName]; 521 } else { 522 return null; 523 } 524 } 525 526 public function getWidgetJs() { 527 if ($this->config->get('payment_amazon_login_pay_test') == 'sandbox') { 528 if ($this->config->get('payment_amazon_login_pay_payment_region') == 'GBP') { 529 $amazon_payment_js = 'https://static-eu.payments-amazon.com/OffAmazonPayments/uk/sandbox/lpa/js/Widgets.js'; 530 } elseif ($this->config->get('payment_amazon_login_pay_payment_region') == 'USD') { 531 $amazon_payment_js = 'https://static-na.payments-amazon.com/OffAmazonPayments/us/sandbox/js/Widgets.js'; 532 } else { 533 $amazon_payment_js = 'https://static-eu.payments-amazon.com/OffAmazonPayments/de/sandbox/lpa/js/Widgets.js'; 534 } 535 } else { 536 if ($this->config->get('payment_amazon_login_pay_payment_region') == 'GBP') { 537 $amazon_payment_js = 'https://static-eu.payments-amazon.com/OffAmazonPayments/uk/lpa/js/Widgets.js'; 538 } elseif ($this->config->get('payment_amazon_login_pay_payment_region') == 'USD') { 539 $amazon_payment_js = 'https://static-na.payments-amazon.com/OffAmazonPayments/us/js/Widgets.js'; 540 } else { 541 $amazon_payment_js = 'https://static-eu.payments-amazon.com/OffAmazonPayments/de/lpa/js/Widgets.js'; 542 } 543 } 544 return $amazon_payment_js . '?sellerId=' . $this->config->get('payment_amazon_login_pay_merchant_id'); 545 } 546 547 public function getMethod($address, $total) { 548 // Not shown in the payment method list 549 return array(); 550 } 551 552 public function logger($data) { 553 if ($this->config->get('payment_amazon_login_pay_debug')) { 554 $log = new Log('amazon_login_pay.log'); 555 $backtrace = debug_backtrace(); 556 $log->write($backtrace[6]['class'] . '::' . $backtrace[6]['function'] . ' Data: ' . print_r($data, 1)); 557 } 558 } 559 560 }