From 0f7af50096726d219166de771c20be5b490a92c7 Mon Sep 17 00:00:00 2001 From: Cristian Rossi Date: Thu, 20 Sep 2018 17:14:36 -0300 Subject: [PATCH 1/8] Desabilita o handling de renewal orders pelo WC_Subscriptions para assinaturas do Vindi --- ...vindi-wc-subscriptions-disable-renewal.php | 40 +++++++++++++++++++ vindi-woocommerce-subscriptions.php | 1 + 2 files changed, 41 insertions(+) create mode 100644 includes/class-vindi-wc-subscriptions-disable-renewal.php diff --git a/includes/class-vindi-wc-subscriptions-disable-renewal.php b/includes/class-vindi-wc-subscriptions-disable-renewal.php new file mode 100644 index 0000000..a413eed --- /dev/null +++ b/includes/class-vindi-wc-subscriptions-disable-renewal.php @@ -0,0 +1,40 @@ +get_meta( 'vindi_wc_subscription_id' ) ) ) { + //if it is we disable Woocommerce Subscriptions Renewal order and let Vindi handle it via webhooks + remove_action( 'woocommerce_scheduled_subscription_payment', 'WC_Subscriptions_Manager::prepare_renewal', 1 ); + } + } + + +} diff --git a/vindi-woocommerce-subscriptions.php b/vindi-woocommerce-subscriptions.php index 082c324..99ea6c2 100755 --- a/vindi-woocommerce-subscriptions.php +++ b/vindi-woocommerce-subscriptions.php @@ -88,6 +88,7 @@ public function __construct() 'class-vindi-payment.php', 'class-vindi-webhook-handler.php', 'class-vindi-subscription-status-handler.php', + 'class-vindi-wc-subscriptions-disable-renewal.php', )); $this->settings = new Vindi_Settings(); From 2b5b731d81f0da9f624ac997e24532a5d2d0c3b4 Mon Sep 17 00:00:00 2001 From: Cristian Rossi Date: Thu, 20 Sep 2018 17:57:48 -0300 Subject: [PATCH 2/8] =?UTF-8?q?-=20Criada=20fun=C3=A7=C3=A3o=20na=20API=20?= =?UTF-8?q?para=20buscar=20assinatura=20-=20Corrigida=20fun=C3=A7=C3=A3o?= =?UTF-8?q?=20de=20atualizar=20pr=C3=B3ximo=20pagamento=20para=20buscar=20?= =?UTF-8?q?a=20subscription=20do=20Vindi=20e=20usar=20a=20data=20correta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-vindi-api.php | 14 ++++++++ includes/class-vindi-webhook-handler.php | 41 +++++++++++++++--------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/includes/class-vindi-api.php b/includes/class-vindi-api.php index 4e3a95d..1283557 100755 --- a/includes/class-vindi-api.php +++ b/includes/class-vindi-api.php @@ -240,6 +240,20 @@ public function activate_subscription($subscription_id) return false; } + + /** + * @param int $subscription_id + * + * @return array|bool|mixed + */ + public function get_subscription($subscription_id) + { + if ($response = $this->request('subscriptions/' . $subscription_id, 'GET')) + return $response; + + return false; + } + /** * @param int $bill_id * diff --git a/includes/class-vindi-webhook-handler.php b/includes/class-vindi-webhook-handler.php index 71e10ff..a5ff477 100644 --- a/includes/class-vindi-webhook-handler.php +++ b/includes/class-vindi-webhook-handler.php @@ -114,7 +114,7 @@ private function bill_created($data) if(empty($data->bill->subscription)) { return; } - + $renew_infos = [ 'wc_subscription_id' => $data->bill->subscription->code, 'vindi_subscription_id' => $data->bill->subscription->id, @@ -124,7 +124,6 @@ private function bill_created($data) if(!$this->subscription_has_order_in_cycle($renew_infos['vindi_subscription_id'], $renew_infos['cycle'])) { $this->subscription_renew($renew_infos); - $this->update_next_payment($data); } } @@ -139,12 +138,13 @@ private function bill_paid($data) } 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); } $new_status = $this->container->get_return_status(); $order->update_status($new_status, __('O Pagamento foi realizado com sucesso pela Vindi.', 'woocommerce-vindi')); + $this->update_next_payment($data); } /** @@ -201,7 +201,7 @@ private function charge_rejected($data) private function subscription_canceled($data) { $subscription = $this->find_subscription_by_id($data->subscription->code); - + if ($this->container->get_synchronism_status() && ($subscription->has_status('cancelled') || $subscription->has_status('pending-cancel') @@ -364,16 +364,27 @@ private function query_order_by_metas(array $metas) return new WP_Query($args); } - /** - * Update next payment schedule of subscription - * @param $next_payment string - * @param $subscription_id int wc subscription id - **/ - private function update_next_payment($data) - { - $subscription = $this->find_subscription_by_id($data->bill->subscription->code); - $next_payment = date('Y-m-d H:i:s', strtotime($data->bill->period->end_at . ' + 3 days')); + /** + * Update next payment schedule of subscription + * + * @param $data object + **/ + private function update_next_payment( $data ) { - $subscription->update_dates(array('next_payment' => $next_payment)); - } + // 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 + $vindi_subscription = $this->container->api->get_subscription( $data->bill->subscription->id ); + + if ( $vindi_subscription and isset( $vindi_subscription['subscription'] ) ) { + + // format next payment date + $next_payment = date( 'Y-m-d H:i:s', strtotime( $vindi_subscription['subscription']['next_billing_at'] ) ); + + // find our wc_subscription + $subscription = $this->find_subscription_by_id( $data->bill->subscription->code ); + + // update the date to show the user when will be his next payment + $subscription->update_dates( array( 'next_payment' => $next_payment ) ); + } + } } From 46760b12a8cd5a93fe51800734145b3f96759c67 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Sep 2018 16:22:41 -0300 Subject: [PATCH 3/8] =?UTF-8?q?Refatora=20m=C3=A9todos=20isCanceledSubscri?= =?UTF-8?q?ption=20e=20getSubscription?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-vindi-api.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/class-vindi-api.php b/includes/class-vindi-api.php index 1283557..40bbfb4 100755 --- a/includes/class-vindi-api.php +++ b/includes/class-vindi-api.php @@ -248,7 +248,7 @@ public function activate_subscription($subscription_id) */ public function get_subscription($subscription_id) { - if ($response = $this->request('subscriptions/' . $subscription_id, 'GET')) + if ($response = $this->request(sprintf('subscriptions/%s', $subscription_id),'GET')['subscription']) return $response; return false; @@ -274,16 +274,18 @@ public function delete_bill($bill_id) */ public function is_subscription_canceled($subscription_id) { - if (isset($this->recentRequest)) { - unset($this->recentRequest); + if (isset($this->recentRequest) + && $this->recentRequest['id'] == $subscription_id) { + if ($this->recentRequest['status'] == 'canceled') + return true; return false; } - $response = $this->request(sprintf('subscriptions/%s', $subscription_id),'GET')['subscription']; + $response = $this->get_subscription($subscription_id); if (array_key_exists('status', $response)) { - if ($response['status'] != 'canceled') { - $this->recentRequest = $subscription_id; + if ($response['status'] == 'canceled') { + $this->recentRequest = $response; return true; } } From 6111d001a360c2eae00b48aeb0ad6d5e47f373cc Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Sep 2018 16:23:37 -0300 Subject: [PATCH 4/8] =?UTF-8?q?Altera=20valida=C3=A7=C3=A3o=20da=20consult?= =?UTF-8?q?a=20de=20status=20de=20assinatura=20(pr=C3=A9-cancelamento)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-vindi-subscription-status-handler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-vindi-subscription-status-handler.php b/includes/class-vindi-subscription-status-handler.php index f669b19..6ba19d6 100644 --- a/includes/class-vindi-subscription-status-handler.php +++ b/includes/class-vindi-subscription-status-handler.php @@ -55,7 +55,7 @@ public function cancelled_status($wc_subscription,$new_status = 'cancelled') if (!$this->container->dependency->wc_memberships_are_activated() && 'pending-cancel' === $new_status) { return $wc_subscription->update_status('cancelled'); } - if ($this->container->api->is_subscription_canceled($this->vindi_subscription_id)) { + if (!$this->container->api->is_subscription_canceled($this->vindi_subscription_id)) { $this->container->api->suspend_subscription($this->vindi_subscription_id, true); } } From 2eb23e69f512d6b9339f8622f60f83902a5b0803 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Sep 2018 16:25:07 -0300 Subject: [PATCH 5/8] Aplica CS --- ...vindi-wc-subscriptions-disable-renewal.php | 40 ------------------- includes/class-vindi-wcs-disable-renewal.php | 37 +++++++++++++++++ includes/class-vindi-webhook-handler.php | 15 +++---- vindi-woocommerce-subscriptions.php | 2 +- 4 files changed, 46 insertions(+), 48 deletions(-) delete mode 100644 includes/class-vindi-wc-subscriptions-disable-renewal.php create mode 100644 includes/class-vindi-wcs-disable-renewal.php diff --git a/includes/class-vindi-wc-subscriptions-disable-renewal.php b/includes/class-vindi-wc-subscriptions-disable-renewal.php deleted file mode 100644 index a413eed..0000000 --- a/includes/class-vindi-wc-subscriptions-disable-renewal.php +++ /dev/null @@ -1,40 +0,0 @@ -get_meta( 'vindi_wc_subscription_id' ) ) ) { - //if it is we disable Woocommerce Subscriptions Renewal order and let Vindi handle it via webhooks - remove_action( 'woocommerce_scheduled_subscription_payment', 'WC_Subscriptions_Manager::prepare_renewal', 1 ); - } - } - - -} diff --git a/includes/class-vindi-wcs-disable-renewal.php b/includes/class-vindi-wcs-disable-renewal.php new file mode 100644 index 0000000..610baf0 --- /dev/null +++ b/includes/class-vindi-wcs-disable-renewal.php @@ -0,0 +1,37 @@ +get_meta('vindi_wc_subscription_id'))) { + //if it is we disable Woocommerce Subscriptions Renewal order and let Vindi handle it via webhooks + remove_action('woocommerce_scheduled_subscription_payment', + 'WC_Subscriptions_Manager::prepare_renewal', 1); + } + } +} diff --git a/includes/class-vindi-webhook-handler.php b/includes/class-vindi-webhook-handler.php index a5ff477..c908f28 100644 --- a/includes/class-vindi-webhook-handler.php +++ b/includes/class-vindi-webhook-handler.php @@ -122,7 +122,8 @@ private function bill_created($data) 'bill_id' => $data->bill->id ]; - if(!$this->subscription_has_order_in_cycle($renew_infos['vindi_subscription_id'], $renew_infos['cycle'])) { + if(!$this->subscription_has_order_in_cycle($renew_infos['vindi_subscription_id'] + , $renew_infos['cycle'])) { $this->subscription_renew($renew_infos); } } @@ -369,22 +370,22 @@ private function query_order_by_metas(array $metas) * * @param $data object **/ - private function update_next_payment( $data ) { + private function update_next_payment($data) { // 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 - $vindi_subscription = $this->container->api->get_subscription( $data->bill->subscription->id ); + $vindi_subscription = $this->container->api->get_subscription($data->bill->subscription->id); - if ( $vindi_subscription and isset( $vindi_subscription['subscription'] ) ) { + if ($vindi_subscription && isset($vindi_subscription['subscription'])) { // format next payment date - $next_payment = date( 'Y-m-d H:i:s', strtotime( $vindi_subscription['subscription']['next_billing_at'] ) ); + $next_payment = date('Y-m-d H:i:s', strtotime($vindi_subscription['subscription']['next_billing_at'])); // find our wc_subscription - $subscription = $this->find_subscription_by_id( $data->bill->subscription->code ); + $subscription = $this->find_subscription_by_id($data->bill->subscription->code); // update the date to show the user when will be his next payment - $subscription->update_dates( array( 'next_payment' => $next_payment ) ); + $subscription->update_dates(array('next_payment' => $next_payment)); } } } diff --git a/vindi-woocommerce-subscriptions.php b/vindi-woocommerce-subscriptions.php index 99ea6c2..00e5bfa 100755 --- a/vindi-woocommerce-subscriptions.php +++ b/vindi-woocommerce-subscriptions.php @@ -88,7 +88,7 @@ public function __construct() 'class-vindi-payment.php', 'class-vindi-webhook-handler.php', 'class-vindi-subscription-status-handler.php', - 'class-vindi-wc-subscriptions-disable-renewal.php', + 'class-vindi-wcs-disable-renewal.php', )); $this->settings = new Vindi_Settings(); From 8b17f7c809e127eaedb38ad069f0090d53b2b2fa Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 2 Oct 2018 19:34:59 -0300 Subject: [PATCH 6/8] =?UTF-8?q?Aplica=20ajuste=20realizado=20na=20=C3=BAlt?= =?UTF-8?q?ima=20feature?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-vindi-api.php | 7 ++++--- includes/class-vindi-webhook-handler.php | 11 ++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/includes/class-vindi-api.php b/includes/class-vindi-api.php index c7735de..38cd308 100755 --- a/includes/class-vindi-api.php +++ b/includes/class-vindi-api.php @@ -280,19 +280,20 @@ public function is_subscription_active($subscription_id) { if (isset($this->recentRequest) && $this->recentRequest['id'] == $subscription_id) { - if ($this->recentRequest['status'] == 'canceled') + if ($this->recentRequest['status'] != 'canceled') return true; return false; } $response = $this->get_subscription($subscription_id); - if (array_key_exists('status', $response)) { - if ($response['status'] == 'canceled') { + if ($response && array_key_exists('status', $response)) { + if ($response['status'] != 'canceled') { $this->recentRequest = $response; return true; } } + return false; } public function find_customer_by_code($code) diff --git a/includes/class-vindi-webhook-handler.php b/includes/class-vindi-webhook-handler.php index c908f28..78eb098 100644 --- a/includes/class-vindi-webhook-handler.php +++ b/includes/class-vindi-webhook-handler.php @@ -111,7 +111,7 @@ private function subscription_renew($renew_infos) **/ private function bill_created($data) { - if(empty($data->bill->subscription)) { + if (empty($data->bill->subscription)) { return; } @@ -122,7 +122,7 @@ private function bill_created($data) 'bill_id' => $data->bill->id ]; - if(!$this->subscription_has_order_in_cycle($renew_infos['vindi_subscription_id'] + if (!$this->subscription_has_order_in_cycle($renew_infos['vindi_subscription_id'] , $renew_infos['cycle'])) { $this->subscription_renew($renew_infos); } @@ -139,12 +139,12 @@ private function bill_paid($data) } 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); } $new_status = $this->container->get_return_status(); - $order->update_status($new_status, __('O Pagamento foi realizado com sucesso pela Vindi.', 'woocommerce-vindi')); + $order->update_status($new_status, __('O Pagamento foi realizado com sucesso pela Vindi.', + 'woocommerce-vindi')); $this->update_next_payment($data); } @@ -191,7 +191,8 @@ private function charge_rejected($data) if($order->get_status() == 'pending'){ $order->update_status('failed', 'Pagamento rejeitado!'); }else{ - throw new Exception('Erro ao trocar status da fatura para "failed" pois a fatura #' . $data->charge->bill->id . ' não está mais pendente!'); + throw new Exception('Erro ao trocar status da fatura para "failed" pois a fatura #' . + $data->charge->bill->id . ' não está mais pendente!'); } } From 92733d7f7853102b55b30325214d4eb2a0e0935c Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 4 Oct 2018 19:26:04 -0300 Subject: [PATCH 7/8] =?UTF-8?q?Ajusta=20par=C3=A2metro=20'data=20do=20pr?= =?UTF-8?q?=C3=B3ximo=20pagamento'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-vindi-webhook-handler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-vindi-webhook-handler.php b/includes/class-vindi-webhook-handler.php index 78eb098..dfba25c 100644 --- a/includes/class-vindi-webhook-handler.php +++ b/includes/class-vindi-webhook-handler.php @@ -377,10 +377,10 @@ private function update_next_payment($data) { // we need this step because the actual next billing date does not come from the /bill webhook $vindi_subscription = $this->container->api->get_subscription($data->bill->subscription->id); - if ($vindi_subscription && isset($vindi_subscription['subscription'])) { + if ($vindi_subscription && isset($vindi_subscription['next_billing_at'])) { // format next payment date - $next_payment = date('Y-m-d H:i:s', strtotime($vindi_subscription['subscription']['next_billing_at'])); + $next_payment = date('Y-m-d H:i:s', strtotime($vindi_subscription['next_billing_at'])); // find our wc_subscription $subscription = $this->find_subscription_by_id($data->bill->subscription->code); From 1e54f4c32bfc43eeb9a4c9af001dce0c2aeb5907 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 8 Oct 2018 16:54:58 -0300 Subject: [PATCH 8/8] =?UTF-8?q?Ajusta=20valida=C3=A7=C3=A3o=20de=20certifi?= =?UTF-8?q?cado=20para=20m=C3=A9todos=20de=20pagamento?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-vindi-bank-slip-gateway.php | 2 +- includes/class-vindi-creditcard-gateway.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-vindi-bank-slip-gateway.php b/includes/class-vindi-bank-slip-gateway.php index 3d0b784..b0db52a 100644 --- a/includes/class-vindi-bank-slip-gateway.php +++ b/includes/class-vindi-bank-slip-gateway.php @@ -58,7 +58,7 @@ public function payment_fields() $is_single_order = $this->is_single_order(); - if (!($is_trial = $this->container->get_is_active_sandbox())) + if ($is_trial = $this->container->get_is_active_sandbox()) $is_trial = $this->container->api->is_merchant_status_trial_or_sandbox(); $this->container->get_template('bankslip-checkout.html.php', compact('is_trial', 'is_single_order')); diff --git a/includes/class-vindi-creditcard-gateway.php b/includes/class-vindi-creditcard-gateway.php index d256424..063dc3e 100755 --- a/includes/class-vindi-creditcard-gateway.php +++ b/includes/class-vindi-creditcard-gateway.php @@ -175,7 +175,7 @@ public function payment_fields() for ($i = date('Y') ; $i <= date('Y') + 15 ; $i++) $years[] = $i; - if (!($is_trial = $this->container->get_is_active_sandbox())) + if ($is_trial = $this->container->get_is_active_sandbox()) $is_trial = $this->container->api->is_merchant_status_trial_or_sandbox(); $this->container->get_template('creditcard-checkout.html.php', compact(