Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolução das pendências #27

Merged
merged 11 commits into from
Oct 4, 2020
31 changes: 23 additions & 8 deletions woocommerce-imobanco/includes/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ class Request {

private function request($method, $path, $params = []) {
$ch = curl_init();
if ('test' == WOO_IMOPAY_ENVIRONMENT) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
}

$url = WOO_IMOPAY_API_URL . '/' . $path;
if (substr($path, -1) != '/') {
$url .= '/';
// if ('test' == WOO_IMOPAY_ENVIRONMENT) {
// curl_setopt($ch, CURLOPT_VERBOSE, true);
// }

$url = $path;
$headers = [];

if (strpos($path, 'http://') === false && strpos($path, 'https://') === false ) {
// testa se a URL absoluta foi enviada
$url = WOO_IMOPAY_API_URL . '/' . $path;

if (substr($path, -1) != '/') {
$url .= '/';
}

$headers = ['Authorization: Api-Key '.WOO_IMOPAY_API_KEY, 'Accept:application/json', 'Content-Type: application/json'];

//error_log(json_encode($headers));
}

$method = strtolower($method);
Expand All @@ -20,7 +32,7 @@ private function request($method, $path, $params = []) {

if ('post' == $method) curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Length: '.strlen($postfields)]);
$headers[] = 'Content-Length: '.strlen($postfields);
} else {
if (strpos($url, '?') !== false) {
$url .= '&';
Expand All @@ -30,7 +42,9 @@ private function request($method, $path, $params = []) {
$url .= http_build_query($params);
}

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Api-Key '.WOO_IMOPAY_API_KEY, 'Accept:application/json', 'Content-Type: application/json']);
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
Expand All @@ -40,6 +54,7 @@ private function request($method, $path, $params = []) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
error_log('Request URL: '.$url. ' - Method: '.$method.' - Response: '. $response);

curl_close($ch);

Expand Down
32 changes: 27 additions & 5 deletions woocommerce-imobanco/includes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@
add_action( 'rest_api_init', function () {

register_rest_route( 'imobanco/v1', '/order/update', array(
'methods' => 'GET',
'methods' => 'POST',
'callback' => 'woo_imobanco_update_order',
));

});

function woo_imobanco_update_order($params) {

$id = $params->get_param('id');
$status = $params->get_param('status');
$request = new \WoocommerceImobanco\Request();
error_log('NOTIFICATION INPUT: '. file_get_contents('php://input'));

try {
$content = json_decode(file_get_contents('php://input'));
} catch (\Exception $e) {

error_log($e->getMessage() . file_get_contents('php://input'));
return;
}

// confirmação de notificação
if ($content->Type == 'SubscriptionConfirmation') {
// request de confimação no aws
error_log($request->get($content->SubscribeURL));
return;

}

if ($content->Type == 'Notification') {
$id = json_decode($content->Message)->imopay_id;
error_log('Notification received '.$id);
} else {
return;
}

$order = wc_get_orders([
'post_type' => 'shop_order',
Expand All @@ -36,9 +59,8 @@ function woo_imobanco_update_order($params) {
'charged_back' => 'wc-refundedd'
];

if (isset($order[0]) && $id && $status)
if (isset($order[0]) && $id)
{
$request = new \WoocommerceImobanco\Request();
$response = $request->get("transactions/{$id}");

if ($response) {
Expand Down
21 changes: 20 additions & 1 deletion woocommerce-imobanco/includes/billet.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,19 @@ public function process_payment( $order_id ) {
'payment_method' => [
'expiration_date' => date('Y-m-d', strtotime(WOO_IMOPAY_EXPIRATION_DATE_INCREMENT)),
'limit_date' => date('Y-m-d', strtotime(WOO_IMOPAY_LIMIT_DATE_INCREMENT)),
]
],
'reference_id' => $order_id,
'metadata' => [
'woocommerce' => [
'order' => [
'customer_id' => $customer,
'order_key' => $order->get_data() ['order_key'],
'customer_user_agent' => $order->get_data() ['customer_user_agent'],
'cart_hash' => $order->get_data() ['cart_hash']
],
'blogname' => get_bloginfo('name')
]
]
];

// request API imopay
Expand Down Expand Up @@ -206,6 +218,13 @@ public function process_payment( $order_id ) {

// pagamento aprovado

error_log('BILLET RESPONSE '. json_encode($response));

if (!isset($response->id)) {
wc_add_notice('Ocorreu um erro ao tentar realizar o pagamento via boletos com o gateway. tente novamente', 'error');
return false;
}

update_post_meta($order_id, '_imopay_order_id', $response->id );
update_post_meta($order_id, '_imopay_billet', json_encode($response->payment_method) );

Expand Down
39 changes: 38 additions & 1 deletion woocommerce-imobanco/includes/creditcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ public function payment_fields(){
echo wpautop( wptexturize( $description ) );
}

$total = WC()->cart->cart_contents_total + WC()->cart->tax_total;
$min_installment_value = 0; // preencher com 0 se não quiser limitar
// todo: preencher no painel

// taxas de parcelamento por parcela em % (sem o caractere %)
$installments = [
0, // 1x, ex: 0
0, // 2x, ex: 3
0, // 3x, ex: 5
0, // ...
0,
0,
0,
0,
0,
0,
0,
0, // 12x
// todo: preencher via painel
];

require WOO_IMOPAY_PLUGIN_DIR . 'includes/forms/creditcard_form.php';
}

Expand Down Expand Up @@ -178,7 +199,23 @@ public function process_payment( $order_id ) {
'card_number' => $_POST['card_number'],
'expiration_month' => $_POST['expiration_month'],
'expiration_year' => $_POST['expiration_year'],
'security_code' => $_POST['security_code']
'security_code' => $_POST['security_code'],
'installment_plan' => [
'number_installments' => $_POST['installment'],
'mode' => 'interest_free'
]
],
'reference_id' => $order_id,
'metadata' => [
'woocommerce' => [
'order' => [
'customer_id' => $customer,
'order_key' => $order->get_data() ['order_key'],
'customer_user_agent' => $order->get_data() ['customer_user_agent'],
'cart_hash' => $order->get_data() ['cart_hash']
],
'blogname' => get_bloginfo('name')
]
]
];

Expand Down
22 changes: 22 additions & 0 deletions woocommerce-imobanco/includes/forms/creditcard_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,27 @@
<label for="security_code" class="security_code">Código de segurança (CVV)</label>
<input type="number" class="security_code" name="security_code">
</div>
<div class="form-row form-row-wide">
<label for="installments">Parcelas</label>

<select name="installment input-text">
<?php foreach($installments as $key => $fee): ?>

<option value="<?= $key + 1 ?>">
<?php
if ($fee > 0)
$installment = ($total + ( $total * ( $fee / 100 ) )) / ($key + 1);
else
$installment = $total / ( $key + 1 );
?>
<?= $key + 1 ?>x (R$ <?= number_format($installment,2,',','.') ?> <?= $fee == 0 ? 'sem juros': 'Juros de '.$fee.'%' ?>)
</option>
<?php
if (0 != $min_installment_value && $installment <= $min_installment_value) break;
?>
<?php endforeach ?>
</select>
</div>
<div style="clear:both"></div>

</div>
17 changes: 16 additions & 1 deletion woocommerce-imobanco/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ function imopay_register_address($imopay_id, $data, $customer = null)
}

$request = new Request();

$meta = get_user_meta($customer);
$response = $request->post('addresses/get_by_document/', [
'cpf_cnpj' => $meta['billing_cpf'] ?? $meta['billing_cnpj']
]);

if (isset($response->id)) {
imopay_save_address_id($response->id, $customer);
return $response;
}

$data['owner'] = $imopay_id;

$response = $request->post('addresses/create_by_name_and_uf', $data);
Expand Down Expand Up @@ -133,14 +144,18 @@ function imopay_get_address_from_formdata($imopay_id)
return [];
}

return [
$data = [
'owner' => $imopay_id,
'uf' => $_POST['billing_state'],
'city' => ucwords(strtolower($_POST['billing_city'])),
'neighborhood' => $_POST['billing_neighborhood'],
'street' => $_POST['billing_address_1'],
'zip_code' => str_replace('-', '', trim($_POST['billing_postcode'])),
'number' => $_POST['billing_number'],
'complement' => $_POST['billing_address_2'] ?? ''
];

return $data;
}

function imopay_save_user_id($id, $customer = null)
Expand Down
8 changes: 7 additions & 1 deletion woocommerce-imobanco/woocommerce-imobanco.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

if (!defined('WOO_IMOPAY_SELLER_ID')) {
define('WOO_IMOPAY_SELLER_ID', getenv('WOO_IMOPAY_SELLER_ID'));

}

if (!defined('WOO_IMOPAY_API_KEY')) {
define('WOO_IMOPAY_API_KEY', getenv('WOO_IMOPAY_API_KEY'));
}
Expand Down Expand Up @@ -73,6 +73,12 @@
]
]);

// // somente para testes, remove os dados do imopay do banco do woocommerce
// add_action('init' , function() {
// delete_user_meta(60, '_imopay_user_id' );
// delete_user_meta(60, '_imopay_address_id' );
// });

require WOO_IMOPAY_PLUGIN_DIR . 'includes/functions.php';
require WOO_IMOPAY_PLUGIN_DIR . 'includes/Request.php';
require WOO_IMOPAY_PLUGIN_DIR . 'includes/hooks.php';
Expand Down