<?php require __DIR__ . '/../../vendor/autoload.php'; use Gerencianet\Exception\GerencianetException; use Gerencianet\Gerencianet; $file = file_get_contents(__DIR__ . '/../config.json'); $options = json_decode($file, true); $params = ['id' => 0]; $customer = ['name' => 'Gorbadoc Oldbuck', 'cpf' => '04267484171', 'phone_number' => '5144916523']; $body = ['payment' => ['banking_billet' => ['expire_at' => '2018-12-12', 'customer' => $customer]]]; try { $api = new Gerencianet($options); $charge = $api->payCharge($params, $body); print_r($charge); } catch (GerencianetException $e) { print_r($e->code); print_r($e->error); print_r($e->errorDescription); } catch (Exception $e) { print_r($e->getMessage()); }
public function pay_card() { $this->load->language('payment/gerencianet'); $this->load->model('checkout/order'); $this->load->model('extension/extension'); $options = $this->gerencianet_config_payment_api(); $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); if ($order_info) { $order_data['totals'] = array(); $total = 0; $taxes = $this->cart->getTaxes(); $sort_order = array(); $results = $this->model_extension_extension->getExtensions('total'); foreach ($results as $key => $value) { $sort_order[$key] = $this->config->get($value['code'] . '_sort_order'); } array_multisort($sort_order, SORT_ASC, $results); foreach ($results as $result) { if ($this->config->get($result['code'] . '_status')) { $this->load->model('total/' . $result['code']); } } $sort_order = array(); foreach ($order_data['totals'] as $key => $value) { $sort_order[$key] = $value['sort_order']; } array_multisort($sort_order, SORT_ASC, $order_data['totals']); $data['totals'] = array(); $data['taxes'] = array(); $total_discount = 0; foreach ($order_data['totals'] as $total) { if (floatval($total['value']) < 0) { $total_discount += $this->formatMoney(intval($total['value'] * 100) / 100, true); } } if (isset($this->session->data['shipping_method']['cost'])) { $shipp = $this->session->data['shipping_method']['cost']; } else { $shipp = 0; } $total_discount = intval(ceil(($this->cart->getSubTotal() + $shipp - $order_info['total']) * 100)); if (isset($this->request->post['first_name'])) { $data['first_name'] = $this->request->post['first_name']; } else { if (isset($order_info['payment_firstname'])) { $data['first_name'] = html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8'); } else { $data['first_name'] = ''; } } if (isset($this->request->post['last_name'])) { $data['last_name'] = $this->request->post['last_name']; } else { if (isset($order_info['payment_lastname'])) { $data['last_name'] = html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8'); } else { $data['last_name'] = ''; } } if (isset($this->request->post['cpf'])) { $data['cpf'] = $this->request->post['cpf']; } else { if (isset($order_info['cpf'])) { $data['cpf'] = $order_info['cpf']; } else { $data['cpf'] = ''; } } if (isset($this->request->post['phone_number'])) { $data['phone_number'] = $this->request->post['phone_number']; } else { if (isset($data['phone_number'])) { $data['phone_number'] = $order_info['phone_number']; } else { $data['phone_number'] = ''; } } if (isset($this->request->post['email'])) { $data['email'] = $this->request->post['email']; } else { if (isset($data['email'])) { $data['email'] = $order_info['email']; } else { $data['email'] = ''; } } if (isset($this->request->post['birth'])) { $data['birth'] = $this->request->post['birth']; } else { if (isset($data['birth'])) { $data['birth'] = $order_info['birth']; } else { $data['birth'] = ''; } } if (isset($this->request->post['street'])) { $data['street'] = $this->request->post['street']; } else { if (isset($data['street'])) { $data['street'] = $order_info['street']; } else { $data['street'] = ''; } } if (isset($this->request->post['number'])) { $data['number'] = $this->request->post['number']; } else { if (isset($data['number'])) { $data['number'] = $order_info['number']; } else { $data['number'] = ''; } } if (isset($this->request->post['birth'])) { $data['birth'] = $this->request->post['birth']; } else { if (isset($data['birth'])) { $data['birth'] = $order_info['birth']; } else { $data['birth'] = ''; } } if (isset($this->request->post['neighborhood'])) { $data['neighborhood'] = $this->request->post['neighborhood']; } else { if (isset($data['neighborhood'])) { $data['neighborhood'] = $order_info['neighborhood']; } else { $data['neighborhood'] = ''; } } if (isset($this->request->post['complement'])) { $data['complement'] = $this->request->post['complement']; } else { if (isset($data['complement'])) { $data['complement'] = $order_info['complement']; } else { $data['complement'] = ''; } } if (isset($this->request->post['zipcode'])) { $data['zipcode'] = $this->request->post['zipcode']; } else { if (isset($data['zipcode'])) { $data['zipcode'] = $order_info['zipcode']; } else { $data['zipcode'] = ''; } } if (isset($this->request->post['city'])) { $data['city'] = $this->request->post['city']; } else { if (isset($data['city'])) { $data['city'] = $order_info['city']; } else { $data['city'] = ''; } } if (isset($this->request->post['state'])) { $data['state'] = $this->request->post['state']; } else { if (isset($data['state'])) { $data['state'] = $order_info['state']; } else { $data['state'] = ''; } } if (isset($this->request->post['installments'])) { $data['installments'] = $this->request->post['installments']; } else { if (isset($data['installments'])) { $data['installments'] = $order_info['installments']; } else { $data['installments'] = ''; } } if (isset($this->request->post['id_charge'])) { $data['id_charge'] = $this->request->post['id_charge']; } else { $data['id_charge'] = ''; } if (isset($this->request->post['payment_token'])) { $data['payment_token'] = $this->request->post['payment_token']; } else { $data['payment_token'] = ''; } $this->session->data['buyer_email'] = $data['email']; $params = array('id' => $data['id_charge']); $paymentToken = $data['payment_token']; if (isset($this->request->post['pay_card_with_cnpj'])) { $data['pay_card_with_cnpj'] = $this->request->post['pay_card_with_cnpj']; } else { $data['pay_card_with_cnpj'] = ''; } if (isset($this->request->post['cnpj'])) { $data['cnpj'] = $this->request->post['cnpj']; } else { $data['cnpj'] = ''; } if (isset($this->request->post['corporate_name'])) { $data['corporate_name'] = $this->request->post['corporate_name']; } else { $data['corporate_name'] = ''; } if ($data['pay_card_with_cnpj'] && $data['corporate_name'] && $data['cnpj']) { $juridical_data = array('corporate_name' => $data['corporate_name'], 'cnpj' => $data['cnpj']); $customer = array('phone_number' => $data['phone_number'], 'email' => $data['email'], 'birth' => $data['birth'], 'juridical_person' => $juridical_data); } else { $customer = array('name' => $data['first_name'] . " " . $data['last_name'], 'cpf' => $data['cpf'], 'phone_number' => $data['phone_number'], 'email' => $data['email'], 'birth' => $data['birth']); } $billingAddress = array('street' => $data['street'], 'number' => $data['number'], 'neighborhood' => $data['neighborhood'], 'zipcode' => preg_replace('/[^0-9]/', '', $data['zipcode']), 'city' => $data['city'], 'state' => $data['state'], 'complement' => $data['complement']); $discount = array('type' => 'currency', 'value' => intval($total_discount)); if ($total_discount > 0) { $body = array('payment' => array('credit_card' => array('installments' => intval($data['installments']), 'billing_address' => $billingAddress, 'payment_token' => $paymentToken, 'customer' => $customer, 'discount' => $discount))); } else { $body = array('payment' => array('credit_card' => array('installments' => intval($data['installments']), 'billing_address' => $billingAddress, 'payment_token' => $paymentToken, 'customer' => $customer))); } try { $api = new Gerencianet($options); $charge = $api->payCharge($params, $body); $this->db->query("UPDATE `" . DB_PREFIX . "order` SET payment_method = 'Cartão de Crédito - Gerencianet' WHERE order_id = '" . (int) $this->session->data['order_id'] . "'"); $this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('gerencianet_waiting_status_id'), $this->language->get('gn_card_oc_order_comment'), true); $this->session->data['order_id'] = ''; $this->cart->clear(); $this->result_api($charge, true); } catch (GerencianetException $e) { $errorResponse = array("code" => $e->code, "error" => $e->error, "message" => $e->errorDescription); $this->result_api($errorResponse, false); } catch (Exception $e) { $errorResponse = array("message" => $e->getMessage()); $this->result_api($errorResponse, false); } } }