vindi/vindi-woocommerce

View on GitHub
src/services/Webhooks.php

Summary

Maintainability
C
1 day
Test Coverage
File `Webhooks.php` has 367 lines of code (exceeds 250 allowed). Consider refactoring.
<?php
 
namespace VindiPaymentGateways;
 
use WC_Subscriptions_Manager;
use Exception;
use WP_Query;
use WC_Order;
 
The class VindiWebhooks has an overall complexity of 75 which is very high. The configured complexity threshold is 50.
`VindiWebhooks` has 26 functions (exceeds 20 allowed). Consider refactoring.
The property $vindi_settings is not named in camelCase.
class VindiWebhooks
{
/**
* @var VindiSettings
*/
Line indented incorrectly; expected 4 spaces, found 2
private $vindi_settings;
 
/**
* @var VindiRoutes
*/
Line indented incorrectly; expected 4 spaces, found 2
private $routes;
 
/**
* @var WebhooksHelper
*/
private $webhooksHelpers;
 
/**
* @param VindiSettings $vindi_settings
*/
public function __construct(VindiSettings $vindi_settings)
{
Line indented incorrectly; expected at least 8 spaces, found 6
$this->vindi_settings = $vindi_settings;
Line indented incorrectly; expected at least 8 spaces, found 6
$this->routes = $vindi_settings->routes;
$this->webhooksHelpers = new WebhooksHelpers($this);
}
 
/**
* Handle incoming webhook.
*/
public function handle()
{
$token = filter_input(INPUT_GET, 'token', FILTER_SANITIZE_STRING);
$raw_body = file_get_contents('php://input');
$body = json_decode($raw_body);
 
if (!$this->validate_access_token($token)) {
http_response_code(403);
The method handle() contains an exit expression.
die('invalid access token');
}
 
$this->vindi_settings->logger->log(sprintf(__('Novo Webhook chamado: %s', VINDI), $raw_body));
 
try {
$this->process_event($body);
} catch (Exception $e) {
$this->vindi_settings->logger->log($e->getMessage());
 
if (2 === $e->getCode()) {
header("HTTP/1.0 422 Unprocessable Entity");
The method handle() contains an exit expression.
die($e->getMessage());
}
}
}
 
/**
* @param string $token
*/
Line indented incorrectly; expected 4 spaces, found 2
private function validate_access_token($token)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
return $token === $this->vindi_settings->get_token();
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Read json entity received and proccess the right event
* @param string $body
*/
Line indented incorrectly; expected 4 spaces, found 2
private function process_event($body)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if (null == $body || empty($body->event))
Line indented incorrectly; expected at least 8 spaces, found 6
throw new Exception(__('Falha ao interpretar JSON do webhook: Evento do Webhook não encontrado!', VINDI));
 
Line indented incorrectly; expected at least 8 spaces, found 4
$type = $body->event->type;
Line indented incorrectly; expected at least 8 spaces, found 4
$data = $body->event->data;
 
Line indented incorrectly; expected 8 spaces, found 4
if (method_exists($this, $type)) {
Line indented incorrectly; expected at least 12 spaces, found 6
$this->vindi_settings->logger->log(sprintf(__('Novo Evento processado: %s', VINDI), $type));
Line indented incorrectly; expected at least 12 spaces, found 6
return $this->{$type}($data);
Line indented incorrectly; expected 8 spaces, found 4
}
 
Line indented incorrectly; expected at least 8 spaces, found 4
$this->vindi_settings->logger->log(sprintf(__('Evento do webhook ignorado pelo plugin: ', VINDI), $type));
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Process test event from webhook
* @param $data array
*/
Avoid unused parameters such as '$data'.
Line indented incorrectly; expected 4 spaces, found 2
private function test($data)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
$this->vindi_settings->logger->log(__('Evento de teste do webhook.', VINDI));
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Process bill_created event from webhook
* @param $data array
*/
private function bill_created($data)
{
$response = ['message' => 'Não foi possível emitir a fatura', 'status' => 422];
try {
if (empty($data->bill->subscription)) {
return;
}
$renewInfos = $this->webhooksHelpers->renew_infos_array($data);
if ($this->webhooksHelpers->handle_subscription_renewal($renewInfos, $data)) {
$response = ['message' => 'Fatura emitida corretamente', 'status' => 200];
} elseif ($this->webhooksHelpers->handle_trial_period($renewInfos['wc_subscription_id'])) {
$response = ['message' => 'O estado da assinatura passou para "Em espera"', 'status' => 200];
}
} catch (\Exception $e) {
$this->handle_exception('bill_created', $e->getMessage(), $data->bill->id);
$response = ['message' => 'Erro durante o processamento da fatura.', 'status' => 500];
}
 
return wp_send_json(['message' => $response['message']], $response['status']);
}
/**
* Process subscription_renew event from webhook
* @param $renew_infos array
*/
public function subscription_renew($renew_infos)
Opening brace indented incorrectly; expected 4 spaces, found 2
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
$subscription = $this->find_subscription_by_id($renew_infos['wc_subscription_id']);
 
Line indented incorrectly; expected 8 spaces, found 4
if ($this->subscription_has_order_in_cycle($renew_infos['vindi_subscription_id'], $renew_infos['cycle'])) {
Line indented incorrectly; expected at least 12 spaces, found 6
throw new Exception(sprintf(
Line indented incorrectly; expected at least 12 spaces, found 8
Multi-line function call not indented correctly; expected 10 spaces but found 8
__('Já existe o ciclo %s para a assinatura #%s pedido #%s!', VINDI),
Multi-line function call not indented correctly; expected 10 spaces but found 8
Line indented incorrectly; expected at least 12 spaces, found 8
$renew_infos['cicle'],
Line indented incorrectly; expected at least 12 spaces, found 8
Multi-line function call not indented correctly; expected 10 spaces but found 8
$renew_infos['vindi_subscription_id'],
Line indented incorrectly; expected at least 12 spaces, found 8
Multi-line function call not indented correctly; expected 10 spaces but found 8
$subscription->get_last_order()
Line indented incorrectly; expected at least 12 spaces, found 6
));
Line indented incorrectly; expected 8 spaces, found 4
}
 
Avoid using static access to class '\WC_Subscriptions_Manager' in method 'subscription_renew'.
WC_Subscriptions_Manager::prepare_renewal($subscription->id);
$order_id = $subscription->get_last_order();
$order = $this->find_order_by_id($order_id);
$subscription_id = $renew_infos['vindi_subscription_id'];
$order_post_meta = array($order->get_meta('vindi_order', true));
$order_post_meta[$subscription_id]['cycle'] = $renew_infos['cycle'];
$order_post_meta[$subscription_id]['product'] = $renew_infos['plan_name'];
$order_post_meta[$subscription_id]['bill'] = $this->webhooksHelpers->make_array_bill($renew_infos);
if ($renew_infos['bill_status'] === 'paid') {
$order->payment_complete();
}
$order->update_meta_data('vindi_order', $order_post_meta);
$order->save();
$this->vindi_settings->logger->log('Novo Período criado: Pedido #'.$order->id);
 
// We've already processed the renewal
remove_action('woocommerce_scheduled_subscription_payment', 'WC_Subscriptions_Manager::prepare_renewal');
}
 
/**
* Process bill_paid event from webhook
* @param $data array
*/
Method `bill_paid` has 30 lines of code (exceeds 25 allowed). Consider refactoring.
Function `bill_paid` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
private function bill_paid($data)
{
try {
if (empty($data->bill->subscription)) {
Line indented incorrectly; expected at least 16 spaces, found 14
$order = $this->find_order_by_id($data->bill->code);
The method bill_paid uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
} else {
$vindi_subscription_id = $data->bill->subscription->id;
$cycle = $data->bill->period->cycle;
$order = $this->find_order_by_subscription_and_cycle($vindi_subscription_id, $cycle);
}
 
$vindi_order = get_post_meta($order->id, 'vindi_order', true);
if (!is_array($vindi_order)) {
return wp_send_json(['message' => 'Pedido Vindi não encontrado.'], 422);
}
 
if (empty($data->bill->subscription)) {
$vindi_order['single_payment']['bill']['status'] = $data->bill->status;
}
$vindi_order[$vindi_subscription_id]['bill']['status'] = $data->bill->status;
$order->update_meta_data('vindi_order', $vindi_order);
$order->save();
$vindi_order_info = end($vindi_order);
 
if ($vindi_order_info['bill']['status'] == 'paid') {
$new_status = $this->vindi_settings->get_return_status();
$order->update_status($new_status, __('O pagamento foi processado com sucesso pela Vindi.', VINDI));
$this->update_next_payment($data);
return wp_send_json(['message' => 'Processamento de pagamento de fatura concluído.'], 200);
}
return wp_send_json(['message' => 'Não foi possível processar o pagamento da fatura'], 422);
} catch (\Exception $e) {
$this->handle_exception('bill_paid', $e->getMessage(), $data->bill->code);
return wp_send_json(['message' => 'Erro durante o processamento do pagamento da fatura.'], 500);
}
}
 
/**
* Process bill_canceled event from webhook
* @param $data array
*/
Line indented incorrectly; expected 4 spaces, found 2
private function bill_canceled($data)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected 8 spaces, found 4
try {
Line indented incorrectly; expected 12 spaces, found 6
if (empty($data->bill->subscription)) {
Line indented incorrectly; expected at least 16 spaces, found 8
$order = $this->find_order_by_id($data->bill->code);
The method bill_canceled uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Line indented incorrectly; expected 12 spaces, found 6
} else {
Line indented incorrectly; expected at least 16 spaces, found 8
$vindi_subscription_id = $data->bill->subscription->id;
Line indented incorrectly; expected at least 16 spaces, found 8
$cycle = $data->bill->period->cycle;
Line indented incorrectly; expected at least 16 spaces, found 8
$order = $this->find_order_by_subscription_and_cycle($vindi_subscription_id, $cycle);
Line indented incorrectly; expected 12 spaces, found 6
}
 
Line indented incorrectly; expected at least 12 spaces, found 6
$order->update_status('cancelled', __('Pagamento cancelado dentro da Vindi!', VINDI));
return wp_send_json(['message' => 'Pagamento cancelado dentro da Vindi!'], 200);
Line indented incorrectly; expected 8 spaces, found 4
} catch (Exception $e) {
$this->handle_exception('bill_canceled', $e->getMessage(), $data->bill->code);
return wp_send_json(['message' => 'Erro durante o processamento de cancelamento da fatura.'], 500);
Line indented incorrectly; expected 8 spaces, found 4
}
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Process issue_created event from webhook
* @param $data array
*/
Line indented incorrectly; expected 4 spaces, found 2
private function issue_created($data)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
$issue_type = $data->issue->issue_type;
Line indented incorrectly; expected at least 8 spaces, found 4
$issue_status = $data->issue->status;
Line indented incorrectly; expected at least 8 spaces, found 4
$item_type = strtolower($data->issue->item_type);
 
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if ('charge_underpay' !== $issue_type)
Line indented incorrectly; expected at least 8 spaces, found 6
throw new Exception(sprintf(__('Pendência criada com o tipo "%s" não processada!', VINDI), $issue_type));
 
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if ('open' !== $issue_status)
Line indented incorrectly; expected at least 8 spaces, found 6
throw new Exception(sprintf(__('Pendência criada com o status "%s" não processada!', VINDI), $issue_status));
 
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if ('charge' !== $item_type)
Line indented incorrectly; expected at least 8 spaces, found 6
throw new Exception(sprintf(__('Pendência criada com o item do tipo "%s" não processada!', VINDI), $item_type));
 
Line indented incorrectly; expected at least 8 spaces, found 4
$item_id = (int) $data->issue->item_id;
Line indented incorrectly; expected at least 8 spaces, found 4
$issue_data = $data->issue->data;
Line indented incorrectly; expected at least 8 spaces, found 4
$bill = $this->find_bill_by_charge_id($item_id);
Line indented incorrectly; expected at least 8 spaces, found 4
$order = $this->find_order_by_bill_id($bill->id);
 
Line indented incorrectly; expected at least 8 spaces, found 4
$order->add_order_note(sprintf(
Multi-line function call not indented correctly; expected 8 spaces but found 6
Line indented incorrectly; expected at least 8 spaces, found 6
__('Divergencia de valores do Pedido #%s: Valor Esperado R$ %s, Valor Pago R$ %s.', VINDI),
Line indented incorrectly; expected at least 8 spaces, found 6
Multi-line function call not indented correctly; expected 8 spaces but found 6
$order->id,
Multi-line function call not indented correctly; expected 8 spaces but found 6
Line indented incorrectly; expected at least 8 spaces, found 6
$issue_data->expected_amount,
Multi-line function call not indented correctly; expected 8 spaces but found 6
Line indented incorrectly; expected at least 8 spaces, found 6
$issue_data->transaction_amount
Line indented incorrectly; expected at least 8 spaces, found 4
));
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Process charge_rejected event from webhook
* @param $data array
*/
private function charge_rejected($data)
{
Line indented incorrectly; expected 8 spaces, found 6
try {
$order = $this->find_order_by_bill_id($data->charge->bill->id);
if (!empty($order) && $order->get_status() == 'pending') {
$order->update_status('failed', __('Pagamento rejeitado!', VINDI));
return wp_send_json(['mensagem' => 'O pagamento foi rejeitado com sucesso.'], 200);
}
return wp_send_json(['mensagem' => 'Erro ao trocar status da fatura para "failed"
pois a fatura #%s não está mais pendente!', $data->charge->bill->id], 404);
Line indented incorrectly; expected 8 spaces, found 6
} catch (Exception $e) {
$this->handle_exception_rejected($e, $data);
return wp_send_json(['mensagem' => 'Ocorreu erro na alteração da assinatura'], 500);
Line indented incorrectly; expected 8 spaces, found 6
}
}
 
/**
* Process handle_exception_rejected
* @param $e array, $data array
*/
private function handle_exception_rejected($event, $data)
{
if ($event->getCode() == 2) {
$bill = $this->routes->findBillById($data->charge->bill->id);
$vindi_subscription_id = isset($bill['subscription']) ? $bill['subscription']['id'] : null;
$cycle = isset($bill['period']) ? $bill['period']['cycle'] : null;
$this->find_order_by_subscription_and_cycle($vindi_subscription_id, $cycle);
}
$this->handle_exception('charge_rejected', $event->getMessage(), $data->charge->bill->id);
}
 
/**
* Process subscription_canceled event from webhook
* @param $data array
*/
private function subscription_canceled($data)
{
try {
$subscription = $this->find_subscription_by_id($data->subscription->code);
if ($this->subscription_cancellation_not_possible($subscription, $data)) {
return wp_send_json(['mensagem' => 'Não foi possível cancelar a assinatura
devido ao seu status atual.'], 422);
}
if ($this->vindi_settings->dependencies->is_wc_memberships_active()) {
return $this->handle_pending_cancel($subscription);
}
$synchronized_subscription = $this->routes->getSubscription($data->subscription->id);
if ($synchronized_subscription['status'] === 'canceled') {
$subscription->update_status('cancelled');
$this->vindi_settings->logger->log(sprintf(__('Assinatura cancelado.', VINDI)));
return wp_send_json(['mensagem' => 'Assinatura cancelado.'], 200);
}
$this->vindi_settings->logger->log(sprintf(__('Ocorreu um erro no cancelamento da assinatura', VINDI)));
wp_send_json(['mensagem' => 'Ocorreu erro na assinatura'], 422);
} catch (\Exception $e) {
$this->handle_exception('subscription_canceled', $e->getMessage(), $data->subscription->id);
$response = ['mensagem' => 'Ocorreu erro no cancelamento da assinatura'];
wp_send_json($response, 500);
return;
}
}
 
private function subscription_cancellation_not_possible($subscription, $data)
{
return $this->vindi_settings->get_synchronism_status()
&& (
$subscription->has_status('cancelled')
|| $subscription->has_status('pending-cancel')
|| $subscription->has_status('on-hold')
)
|| $this->routes->hasPendingSubscriptionBills($data->subscription->id);
}
 
private function handle_pending_cancel($subscription)
{
$subscription->update_status('pending-cancel');
$this->vindi_settings->logger->log(sprintf(__('Assinatura atualizada para cancelamento pendente.', VINDI)));
return wp_send_json(['message' => 'Assinatura atualizada para cancelamento pendente.'], 200);
}
 
/**
* Process subscription_reactivated event from webhook
* @param $data array
*/
private function subscription_reactivated($data)
{
try {
if ($this->vindi_settings->get_synchronism_status()
&& !$this->routes->hasPendingSubscriptionBills($data->subscription->id)
) {
$subscription_id = $data->subscription->code;
$subscription = $this->find_subscription_by_id($subscription_id);
$order_id = $subscription->get_last_order();
$order = $this->find_order_by_id($order_id);
$status_available = array('processing', 'completed', 'on-hold');
if (in_array($order->get_status(), $status_available)) {
$subscription->update_status(
'active',
sprintf(__('Subscription %s reactivated by Vindi.', VINDI), $subscription_id)
);
return wp_send_json(['message' => 'A assinatura foi reativada com sucesso.'], 200);
}
}
return wp_send_json(['message' => 'A assinatura não pôde ser reativada.'], 422);
} catch (\Exception $e) {
$this->handle_exception('subscription_reactivated', $e->getMessage(), $data->subscription->id);
return wp_send_json(['message' => 'Erro durante o processamento da reativação da assinatura.'], 500);
}
}
 
private function handle_exception($event, $error, $data)
{
$message = print_r([
'event' => $event,
'mensagem' => $error,
"id" => $data,
], true);
$this->vindi_settings->logger->log(sprintf(__('WEBHOOK ERROR: %s', VINDI), $message));
}
 
/**
* find a subscription by id
* @param int id
* @return WC_Subscription
*/
public function find_subscription_by_id($id_item)
Opening brace indented incorrectly; expected 4 spaces, found 2
Line indented incorrectly; expected at least 4 spaces, found 2
{
// Webhooks Ids has "WC-" prefix
$sanitized_id = explode('WC-', $id_item);
Line indented incorrectly; expected at least 8 spaces, found 4
$subscription = wcs_get_subscription(end($sanitized_id));
 
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if (empty($subscription))
throw new Exception(sprintf(__('Assinatura #%s não encontrada!', VINDI), $id_item), 2);
 
Line indented incorrectly; expected at least 8 spaces, found 4
return $subscription;
}
 
/**
* @param int id
*
* @return WC_Subscription
*/
Avoid variables with short names like $id. Configured minimum length is 3.
Line indented incorrectly; expected 4 spaces, found 2
private function find_bill_by_charge_id($id)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
$charge = $this->routes->getCharge($id);
 
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if (empty($charge))
Line indented incorrectly; expected at least 8 spaces, found 6
throw new Exception(sprintf(__('Cobrança #%s não encontrada!', VINDI), $id), 2);
 
Line indented incorrectly; expected at least 8 spaces, found 4
return (object) $charge['bill'];
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* find a order by id
* @param int id
*
* @return WC_Order
*/
Avoid variables with short names like $id. Configured minimum length is 3.
Line indented incorrectly; expected 4 spaces, found 2
private function find_order_by_id($id)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
$order = wc_get_order($id);
 
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if (empty($order))
Line indented incorrectly; expected at least 8 spaces, found 6
throw new Exception(sprintf(__('Pedido #%s não encontrado!', VINDI), $id), 2);
 
Line indented incorrectly; expected at least 8 spaces, found 4
return $order;
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* find orders by bill_id meta
*
* @param int $bill_id
*
* @return WC_Order
*/
private function find_order_by_bill_id($bill_id)
Opening brace indented incorrectly; expected 4 spaces, found 2
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
$args = array(
Line indented incorrectly; expected at least 8 spaces, found 6
'post_type' => 'shop_order',
Line indented incorrectly; expected at least 8 spaces, found 6
'meta_key' => 'vindi_bill_id',
Line indented incorrectly; expected at least 8 spaces, found 6
'meta_value' => $bill_id,
Line indented incorrectly; expected at least 8 spaces, found 6
'post_status' => 'any',
Line indented incorrectly; expected at least 8 spaces, found 4
);
 
Line indented incorrectly; expected at least 8 spaces, found 4
$query = new WP_Query($args);
 
Inline control structures are not allowed
Line indented incorrectly; expected at least 8 spaces, found 4
if (false === $query->have_posts())
Line indented incorrectly; expected at least 8 spaces, found 6
throw new Exception(sprintf(__('Pedido com bill_id #%s não encontrado!', VINDI), $bill_id), 2);
 
Line indented incorrectly; expected at least 8 spaces, found 4
return wc_get_order($query->post->ID);
}
 
/**
* Query orders containing cycle meta
*
* @param int $subscription_id
* @param int $cycle
*
* @return WC_Order
*/
private function find_order_by_subscription_and_cycle($subscription_id, $cycle)
{
Line indented incorrectly; expected at least 8 spaces, found 4
$query = $this->query_order_by_metas(array(
Line indented incorrectly; expected at least 8 spaces, found 6
array(
'key' => 'vindi_order',
'value' => 'i:' . $subscription_id . ';a:3:{s:5:"cycle";i:' . $cycle . ';',
'compare' => 'LIKE'
Line indented incorrectly; expected at least 8 spaces, found 6
),
Line indented incorrectly; expected at least 8 spaces, found 4
));
 
if (false === $query->have_posts()) {
$msg = sprintf(__('Pedido #%s ciclo #%s não encontrado!', VINDI), $subscription_id, $cycle);
throw new Exception($msg, 2);
}
Line indented incorrectly; expected at least 8 spaces, found 4
return wc_get_order($query->post->ID);
}
 
/**
* @param int $subscription_id
* @param int $cycle
*
* @return boolean
*/
public function subscription_has_order_in_cycle($subscription_id, $cycle)
{
Line indented incorrectly; expected at least 8 spaces, found 4
$query = $this->query_order_by_metas(array(
Line indented incorrectly; expected at least 8 spaces, found 6
array(
'key' => 'vindi_order',
'value' => 'i:' . $subscription_id . ';a:3:{s:5:"cycle";i:' . $cycle . ';',
'compare' => 'LIKE'
Line indented incorrectly; expected at least 8 spaces, found 6
),
Line indented incorrectly; expected at least 8 spaces, found 4
));
 
Line indented incorrectly; expected at least 8 spaces, found 4
return $query->have_posts();
}
 
/**
* @param array $metas
*
* @return WP_Query
*/
private function query_order_by_metas(array $metas)
{
Line indented incorrectly; expected at least 8 spaces, found 4
$args = array(
Line indented incorrectly; expected at least 8 spaces, found 6
'post_type' => 'shop_order',
Line indented incorrectly; expected at least 8 spaces, found 6
'meta_query' => $metas,
Line indented incorrectly; expected at least 8 spaces, found 6
'post_status' => 'any',
Line indented incorrectly; expected at least 8 spaces, found 4
);
 
Line indented incorrectly; expected at least 8 spaces, found 4
return new WP_Query($args);
}
 
/**
* Update next payment schedule of subscription
*
* @param $data object
*/
public function update_next_payment($data)
Opening brace indented incorrectly; expected 4 spaces, found 2
Line indented incorrectly; expected at least 4 spaces, found 2
{
// let's find the subscription in the API
// we need this step because the actual next billing date does not come from the /bill webhook
Line indented incorrectly; expected at least 8 spaces, found 4
$vindi_subscription = $this->routes->getSubscription($data->bill->subscription->id);
 
Blank line found at start of control structure
Line indented incorrectly; expected 8 spaces, found 4
if ($vindi_subscription && isset($vindi_subscription['next_billing_at'])) {
 
Line indented incorrectly; expected at least 12 spaces, found 6
$next_billing_at = $vindi_subscription['next_billing_at'];
 
Line indented incorrectly; expected at least 12 spaces, found 6
$end_at = $vindi_subscription['end_at'];
 
// na api, quando o plano é de cobrança única,
// o next_billing_at é 1 segundo maior que o end_at
// quando isso acontecer, o next_payment do wc deve ser null
// (a issue #134 tem mais informações do problema)
 
Line indented incorrectly; expected 12 spaces, found 6
if ($end_at != null && $next_billing_at > $end_at) {
Line indented incorrectly; expected at least 16 spaces, found 8
return false;
Line indented incorrectly; expected 12 spaces, found 6
}
 
// format next payment date
Line indented incorrectly; expected at least 12 spaces, found 6
$next_payment = $this->format_date($next_billing_at);
 
// format end date
Line indented incorrectly; expected at least 12 spaces, found 6
$end_date = $this->format_date($end_at);
 
// find our wc_subscription
Line indented incorrectly; expected at least 12 spaces, found 6
$subscription = $this->find_subscription_by_id($data->bill->subscription->code);
 
// update the subscription dates
Line indented incorrectly; expected at least 12 spaces, found 6
$subscription->update_dates(array('next_payment' => $next_payment));
Line indented incorrectly; expected at least 12 spaces, found 6
$subscription->update_dates(array('end_date' => $end_date));
Line indented incorrectly; expected 8 spaces, found 4
}
}
 
Line indented incorrectly; expected 4 spaces, found 2
private function format_date($date)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
return date('Y-m-d H:i:s', strtotime($date));
Line indented incorrectly; expected 4 spaces, found 2
}
}