<?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); $items = [['name' => 'Item 1', 'amount' => 1, 'value' => 1000], ['name' => 'Item 2', 'amount' => 2, 'value' => 2000]]; $body = ['items' => $items]; try { $api = new Gerencianet($options); $charge = $api->createCharge([], $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 create_charge() { $options = $this->gerencianet_config_payment_api(); $this->load->model('checkout/order'); $this->load->model('extension/extension'); $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); $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(); $shippingOnArray = true; $subTotalOnArray = true; $totalOnArray = true; if (isset($this->session->data['shipping_method']['cost'])) { $shipping_price = $this->session->data['shipping_method']['cost']; } else { $shipping_price = 0; } foreach ($order_data['totals'] as $total) { if (floatval($total['value']) == floatval($shipping_price) && $shippingOnArray) { $shippingOnArray = false; } else { if ((floatval($total['value']) == floatval($this->cart->getSubTotal()) || $total['title'] == "Sub-Total") && $subTotalOnArray) { $subTotalOnArray = false; } else { if ((floatval($total['value']) == floatval($order_info['total']) || $total['title'] == "Total") && $totalOnArray) { $totalOnArray = false; } else { if (floatval($total['value']) > 0) { $data['taxes'][] = array('title' => $total['title'], 'value' => $total['value']); } } } } } if ($order_info) { $items = array(); foreach ($this->cart->getProducts() as $product) { $item = array('name' => htmlspecialchars($product['name']), 'amount' => intval($product['quantity']), 'value' => (int) number_format(floatval($product['price']) * 100, 0, '', '')); array_push($items, $item); } } foreach ($data['taxes'] as $new_tax) { $item = array('name' => $new_tax['title'], 'amount' => 1, 'value' => intval(floatval($new_tax['value']) * 100)); array_push($items, $item); } if (isset($this->session->data['shipping_method']['cost'])) { $shipping = array(array('name' => $this->session->data['shipping_method']['title'], 'value' => (int) number_format(floatval($this->session->data['shipping_method']['cost']) * 100, 0, '', ''))); } $metadata = array('custom_id' => strval($this->session->data['order_id']), 'notification_url' => $this->url->link('payment/gerencianet/callback', '', 'SSL')); if (isset($this->session->data['shipping_method']['cost'])) { $body = array('items' => $items, 'shippings' => $shipping, 'metadata' => $metadata); } else { $body = array('items' => $items, 'metadata' => $metadata); } try { $api = new Gerencianet($options); $charge = $api->createCharge(array(), $body); $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); } }