Ejemplo n.º 1
0
 public function payment()
 {
     $this->language->load('payment/mp_ticket');
     try {
         $this->load->model('checkout/order');
         $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
         $all_products = $this->cart->getProducts();
         $items = array();
         foreach ($all_products as $product) {
             $items[] = array("id" => $product['product_id'], "title" => $product['name'], "description" => $product['quantity'] . ' x ' . $product['name'], "quantity" => intval($product['quantity']), "unit_price" => round(floatval($product['price']), 2), "picture_url" => HTTP_SERVER . 'image/' . $product['image'], "category_id" => $this->config->get('mp_ticket_category_id'));
         }
         $payer = array("email" => $order_info['email']);
         $shipments = array("receiver_address" => array("floor" => "-", "zip_code" => $order_info['shipping_postcode'], "street_name" => $order_info['shipping_address_1'] . " - " . $order_info['shipping_address_2'] . " - " . $order_info['shipping_city'] . " - " . $order_info['shipping_zone'] . " - " . $order_info['shipping_country'], "apartment" => "-", "street_number" => "-"));
         $value = floatval(number_format(floatval($order_info['total']) * floatval($order_info['currency_value']), 2));
         $access_token = $this->config->get('mp_ticket_access_token');
         $mp = new MP($access_token);
         $payment_data = array("payer" => $payer, "external_reference" => $order_info['order_id'], "transaction_amount" => $value, "notification_url" => $order_info['store_url'] . 'index.php?route=payment/mp_ticket/notifications', "description" => 'Products', "payment_method_id" => $this->request->get['payment_method_id']);
         $payment_data['additional_info'] = array('shipments' => $shipments, 'items' => $items);
         $is_test_user = strpos($order_info['email'], '@testuser.com');
         if (!$is_test_user) {
             $payment_data["sponsor_id"] = $this->sponsors[$this->getCountry()];
         }
         $payment_response = $mp->create_payment($payment_data);
         error_log('payment response: ' . json_encode($payment_response));
         $this->model_checkout_order->addOrderHistory($order_info['order_id'], $this->config->get('mp_ticket_order_status_id'), null, false);
         echo json_encode(array("status" => $payment_response['status'], "url" => $payment_response['response']['transaction_details']['external_resource_url']));
     } catch (Exception $e) {
         error_log('deu erro: ' . $e);
         echo json_encode(array("status" => $e->getCode(), "message" => $e->getMessage()));
     }
 }
Ejemplo n.º 2
0
 public function payment()
 {
     $this->language->load('payment/mp_transparente');
     error_log(json_encode($this->request->post));
     try {
         $exclude = $this->config->get('mp_transparente_methods');
         $accepted_methods = preg_split("/[\\s,]+/", $exclude);
         $payment_method_id = $this->request->post['payment_method_id'];
         $this->load->model('checkout/order');
         $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
         $all_products = $this->cart->getProducts();
         $items = array();
         foreach ($all_products as $product) {
             $product_price = floatval(number_format(floatval($product['price']) * floatval($order_info['currency_value']), 2));
             $items[] = array("id" => $product['product_id'], "title" => $product['name'], "description" => $product['quantity'] . ' x ' . $product['name'], "quantity" => intval($product['quantity']), "unit_price" => $product_price, "picture_url" => HTTP_SERVER . 'image/' . $product['image'], "category_id" => $this->config->get('mp_transparente_category_id'));
         }
         $payer = array("email" => $order_info['email']);
         if ($this->config->get("mp_transparente_country") != "MLM") {
             $payer['identification'] = array();
             $payer['identification']['type'] = $this->request->post['docType'];
             $payer['identification']["number"] = $this->request->post['docNumber'];
         }
         $shipments = array("receiver_address" => array("floor" => "-", "zip_code" => $order_info['shipping_postcode'], "street_name" => $order_info['shipping_address_1'] . " - " . $order_info['shipping_address_2'] . " - " . $order_info['shipping_city'] . " - " . $order_info['shipping_zone'] . " - " . $order_info['shipping_country'], "apartment" => "-", "street_number" => "-"));
         $value = number_format(floatval($order_info['total']) * floatval($order_info['currency_value']), 2);
         $access_token = $this->config->get('mp_transparente_access_token');
         $payment_data = array("payer" => $payer, "external_reference" => $order_info['order_id'], "transaction_amount" => floatval($value), "notification_url" => $order_info['store_url'] . 'index.php?route=payment/mp_transparente/notifications', "token" => $this->request->post['token'], "description" => 'Products', "installments" => (int) $this->request->post['installments'], "payment_method_id" => $this->request->post['payment_method_id']);
         $payment_data['additional_info'] = array('shipments' => $shipments, 'items' => $items);
         $payment_data['metadata'] = array('token' => $payment_data['token']);
         $is_test_user = strpos($order_info['email'], '@testuser.com');
         if (!$is_test_user) {
             $sponsor_id = $this->sponsors[$this->config->get('mp_transparente_country')];
             error_log('not test_user. sponsor_id will be sent: ' . $sponsor_id);
             $payment_data["sponsor_id"] = $sponsor_id;
         } else {
             error_log('test_user. sponsor_id will not be sent');
         }
         if (isset($this->request->post['issuer_id']) && strlen($this->request->post['issuer_id']) > 0) {
             error_log('issuer_id set, will be sent: ' . $this->request->post['issuer_id']);
             $payment_data['issuer_id'] = $this->request->post['issuer_id'];
         } else {
             error_log('issuer_id NOT set, will NOT be sent');
             unset($payment_data['issuer_id']);
         }
         $payment_json = json_encode($payment_data);
         error_log("pagamento: " . $payment_json);
         $accepted_status = array('approved', "in_process");
         $mp = new MP($access_token);
         $payment_response = $mp->create_payment($payment_json);
         error_log("pagamento processado: ");
         error_log($payment_json);
         error_log("------------------------------------------------------------- payment status: " . $payment_response['response']['status']);
         error_log("payment send:");
         error_log(json_encode($payment_data));
         error_log("payment received");
         error_log(json_encode($payment_response));
         $this->model_checkout_order->addOrderHistory($order_info['order_id'], $this->config->get('mp_transparente_order_status_id_pending'), date('d/m/Y h:i') . ' - ' . $payment_data['payment_method_id']);
         $this->updateOrder($payment_response['response']['id']);
         $json_response = array('status' => null, 'message' => null);
         if (in_array($payment_response['response']['status'], $accepted_status)) {
             $json_response['status'] = $payment_response['response']['status'];
         } else {
             $json_response['status'] = $payment_response['response']['status_detail'];
         }
         echo json_encode($json_response);
     } catch (Exception $e) {
         echo json_encode(array("status" => $e->getCode(), "message" => $e->getMessage()));
     }
 }