Skip to content

Commit

Permalink
Moved add recurring report to catalog paypal checkout controller
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCartpenter committed Jun 2, 2024
1 parent a951dde commit 4fdea08
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 129 deletions.
51 changes: 0 additions & 51 deletions upload/admin/controller/extension/payment/squareup.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,57 +969,6 @@ public function recurringCancel(): void {
$this->response->setOutput(json_encode($json));
}

/**
* Add Recurring Report
*/
public function addRecurringReport(): void {
$this->load->language('extension/payment/squareup');

$json = [];

if (!$this->user->hasPermission('modify', 'extension/payment/squareup') || !$this->config->get('config_order_recurring_report_status')) {
$json['error'] = $this->language->get('error_permission_recurring');
} else {
if (isset($this->request->get['order_recurring_id'])) {
$order_recurring_id = (int)$this->request->get['order_recurring_id'];
} else {
$order_recurring_id = 0;
}

$this->load->model('extension/other/recurring');

$order_recurring_report_info = $this->model_extension_other_recurring->getRecurringReport($order_recurring_id);

if (!$order_recurring_report_info) {
$json['error'] = $this->language->get('error_recurring_report');
} else {
if (oc_get_ip()) {
$ip = oc_get_ip();
} else {
$ip = '';
}

// Countries
$this->load->model('localisation/country');

$country_info = $this->model_localisation_country->getCountry((int)$this->config->get('config_country_id'));

if ($country_info) {
$country = $country_info['name'];
} else {
$country = '';
}

$this->model_extension_other_recurring->addRecurringReport($order_recurring_id, $this->request->post['store_id'], $ip, $country);

$json['success'] = $this->language->get('text_recurring_report_success');
}
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}

/**
* Validate
*
Expand Down
14 changes: 0 additions & 14 deletions upload/admin/model/extension/other/recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@
* @package Admin\Model\Extension\Other
*/
class ModelExtensionOtherRecurring extends Model {
/**
* Add Recurring Report
*
* @param int $order_recurring_id
* @param int $store_id
* @param string $ip
* @param string $country
*
* @return void
*/
public function addRecurringReport(int $order_recurring_id, int $store_id, string $ip, string $country): void {
$this->db->query("INSERT INTO `" . DB_PREFIX . "order_recurring_report` SET `order_recurring_id` = '" . (int)$order_recurring_id . "', `store_id` = '" . (int)$store_id . "', `ip` = '" . $this->db->escape($ip) . "', `country` = '" . $this->db->escape($country) . "', `date_added` = NOW()");
}

/**
* Get Recurring
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script type="text/javascript"><!--
$(document).ready(function() {
var addHistory = function(success_callback) {
var request_1 = $.ajax({
$.ajax({
url: '{{ catalog }}index.php?route=api/order/history&api_token={{ api_token }}&order_id={{ order_id }}',
type: 'post',
dataType: 'json',
Expand All @@ -20,31 +20,6 @@ $(document).ready(function() {
$('#squareup-buttons').before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + thrownError + "<br/>" + xhr.statusText + "<br/>" + xhr.responseText + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
}
});
var request_2 = request_1.then(function() {
$.ajax({
url: 'index.php?route=extension/payment/squareup/addRecurringReport&user_token={{ user_token }}&order_recurring_id={{ order_recurring_id }}',
type: 'post',
dataType: 'json',
data: 'store_id={{ store_id }}',
success: function(json) {
if (json['error']) {
$('#squareup-buttons').before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
}
if (json['success']) {
success_callback();
}
},
error: function(xhr, ajaxOptions, thrownError) {
$('#squareup-buttons').before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + thrownError + "<br/>" + xhr.statusText + "<br/>" + xhr.responseText + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
}
});
});
request_2.done(function() {
});
}
$(document).delegate('#button-cancel', 'click', function() {
Expand Down
118 changes: 80 additions & 38 deletions upload/catalog/controller/extension/payment/paypal.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,29 +801,33 @@ public function createOrder() {
}
}

if (isset($product['recurring_id'])) {
$recurring_id = $product['recurring_id'];
if (isset($product['subscription_plan_id'])) {
$subscription_plan_id = $product['subscription_plan_id'];
} else {
$recurring_id = 0;
$subscription_plan_id = 0;
}

$recurrings = $this->model_catalog_product->getProfiles($product_info['product_id']);
$subscription_plans = $this->model_catalog_subscription_plan->getSubscriptionPlans();

if ($recurrings) {
$recurring_ids = array();
if ($subscription_plans) {
$subscription_plan_ids = array();

foreach ($recurrings as $recurring) {
$recurring_ids[] = $recurring['recurring_id'];
foreach ($subscription_plans as $subscription) {
$subscription_plan = $this->model_catalog_subscription_plan->getSubscriptionPlan($subscription_plan_id);

if ($subscription_plan) {
$subscription_plan_ids[] = $subscription_plan['subscription_plan_id'];
}
}

if (!in_array($recurring_id, $recurring_ids)) {
if (!in_array($subscription_plan_id, $subscription_plan_ids)) {
$errors[] = $this->language->get('error_recurring_required');
}
}

if (!$errors) {
if (!$this->model_extension_payment_paypal->hasProductInCart($product_id, $option, $recurring_id)) {
$this->cart->add($product_id, $quantity, $option, $recurring_id);
if (!$this->model_extension_payment_paypal->hasProductInCart($product_id, $option, $subscription_plan_id)) {
$this->cart->add($product_id, $quantity, $option, $subscription_plan_id);
}

// Unset all shipping and payment methods
Expand Down Expand Up @@ -1047,7 +1051,7 @@ public function createOrder() {

$paypal_order_info['application_context']['shipping_preference'] = $shipping_preference;

if ($setting['general']['vault_status'] && ($this->customer->isLogged() || $this->cart->hasRecurringProducts())) {
if ($setting['general']['vault_status'] && ($this->customer->isLogged() || $this->cart->hasSubscription())) {
if ($payment_method == 'paypal') {
$paypal_customer_token = array();

Expand Down Expand Up @@ -1078,12 +1082,12 @@ public function createOrder() {
$paypal_order_info['payment_source'][$payment_method]['stored_credential']['usage'] = 'SUBSEQUENT';
}
} else {
if (!empty($this->request->post['card_save']) || $this->cart->hasRecurringProducts()) {
if (!empty($this->request->post['card_save']) || $this->cart->hasSubscription()) {
$paypal_order_info['payment_source'][$payment_method]['attributes']['vault']['store_in_vault'] = 'ON_SUCCESS';
$paypal_order_info['payment_source'][$payment_method]['stored_credential']['payment_initiator'] = 'CUSTOMER';
$paypal_order_info['payment_source'][$payment_method]['stored_credential']['usage'] = 'FIRST';

if ($this->cart->hasRecurringProducts()) {
if ($this->cart->hasSubscription()) {
$paypal_order_info['payment_source'][$payment_method]['stored_credential']['payment_type'] = 'UNSCHEDULED';
} else {
$paypal_order_info['payment_source'][$payment_method]['stored_credential']['payment_type'] = 'ONE_TIME';
Expand Down Expand Up @@ -1196,7 +1200,7 @@ public function approveOrder() {
}

// if user not logged in check that the guest checkout is allowed
if (!$this->customer->isLogged() && (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload() || $this->cart->hasRecurringProducts())) {
if (!$this->customer->isLogged() && (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload() || $this->cart->hasSubscription())) {
$data['url'] = $this->url->link('checkout/cart', '', true);

$this->response->addHeader('Content-Type: application/json');
Expand Down Expand Up @@ -1739,7 +1743,7 @@ public function approveOrder() {
}

if (($authorization_status == 'CREATED') || ($authorization_status == 'PENDING')) {
$recurring_products = $this->cart->getRecurringProducts();
$recurring_products = $this->cart->getSubscriptions();

foreach ($recurring_products as $recurring_product) {
$this->model_extension_payment_paypal->recurringPayment($recurring_product, $order_info, $paypal_order_data);
Expand Down Expand Up @@ -1868,7 +1872,7 @@ public function approveOrder() {
}

if (($capture_status == 'COMPLETED') || ($capture_status == 'PENDING')) {
$recurring_products = $this->cart->getRecurringProducts();
$recurring_products = $this->cart->getSubscriptions();

foreach ($recurring_products as $recurring_product) {
$this->model_extension_payment_paypal->recurringPayment($recurring_product, $order_info, $paypal_order_data);
Expand Down Expand Up @@ -2038,25 +2042,31 @@ public function confirmOrder() {
$total = false;
}

$recurring = '';
$description = '';

if ($product['recurring']) {
$frequencies = array(
'day' => $this->language->get('text_day'),
'week' => $this->language->get('text_week'),
'semi_month' => $this->language->get('text_semi_month'),
'month' => $this->language->get('text_month'),
'year' => $this->language->get('text_year'),
);
if ($product['subscription']) {
// Subscriptions
if ($product['subscription']) {
if ($product['subscription']['trial_status']) {
$trial_price = $this->currency->format($this->tax->calculate($product['subscription']['trial_price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
$trial_cycle = $product['subscription']['trial_cycle'];
$trial_frequency = $this->language->get('text_' . $product['subscription']['trial_frequency']);
$trial_duration = $product['subscription']['trial_duration'];

if ($product['recurring']['trial']) {
$recurring = sprintf($this->language->get('text_trial_description'), $this->currency->format($this->tax->calculate($product['recurring']['trial_price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['trial_cycle'], $frequencies[$product['recurring']['trial_frequency']], $product['recurring']['trial_duration']) . ' ';
}
$description .= sprintf($this->language->get('text_subscription_trial'), $price ? $trial_price : '', $trial_cycle, $trial_frequency, $trial_duration);
}

if ($product['recurring']['duration']) {
$recurring .= sprintf($this->language->get('text_payment_description'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']);
} else {
$recurring .= sprintf($this->language->get('text_payment_cancel'), $this->currency->format($this->tax->calculate($product['recurring']['price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), $product['recurring']['cycle'], $frequencies[$product['recurring']['frequency']], $product['recurring']['duration']);
$price = $this->currency->format($this->tax->calculate($product['subscription']['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);

$cycle = $product['subscription']['cycle'];
$frequency = $this->language->get('text_' . $product['subscription']['frequency']);
$duration = $product['subscription']['duration'];

if ($duration) {
$description .= sprintf($this->language->get('text_subscription_duration'), $price ?: '', $cycle, $frequency, $duration);
} else {
$description .= sprintf($this->language->get('text_subscription_cancel'), $price ?: '', $cycle, $frequency);
}
}
}

Expand All @@ -2066,7 +2076,7 @@ public function confirmOrder() {
'name' => $product['name'],
'model' => $product['model'],
'option' => $option_data,
'recurring' => $recurring,
'recurring' => $description,
'quantity' => $product['quantity'],
'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')),
'reward' => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''),
Expand Down Expand Up @@ -3052,7 +3062,7 @@ public function completeOrder() {
}

if (($authorization_status == 'CREATED') || ($authorization_status == 'PENDING')) {
$recurring_products = $this->cart->getRecurringProducts();
$recurring_products = $this->cart->getSubscriptions();

foreach ($recurring_products as $recurring_product) {
$this->model_extension_payment_paypal->recurringPayment($recurring_product, $order_data, $paypal_order_data);
Expand Down Expand Up @@ -3181,7 +3191,7 @@ public function completeOrder() {
}

if (($capture_status == 'COMPLETED') || ($capture_status == 'PENDING')) {
$recurring_products = $this->cart->getRecurringProducts();
$recurring_products = $this->cart->getSubscriptions();

foreach ($recurring_products as $recurring_product) {
$this->model_extension_payment_paypal->recurringPayment($recurring_product, $order_data, $paypal_order_data);
Expand Down Expand Up @@ -3753,7 +3763,7 @@ public function callback() {
}

if (($authorization_status == 'CREATED') || ($authorization_status == 'PENDING')) {
$recurring_products = $this->cart->getRecurringProducts();
$recurring_products = $this->cart->getSubscriptions();

foreach ($recurring_products as $recurring_product) {
$this->model_extension_payment_paypal->recurringPayment($recurring_product, $order_info, $paypal_order_data);
Expand Down Expand Up @@ -3854,7 +3864,7 @@ public function callback() {
}

if (($capture_status == 'COMPLETED') || ($capture_status == 'PENDING')) {
$recurring_products = $this->cart->getRecurringProducts();
$recurring_products = $this->cart->getSubscriptions();

foreach ($recurring_products as $recurring_product) {
$this->model_extension_payment_paypal->recurringPayment($recurring_product, $order_info, $paypal_order_data);
Expand Down Expand Up @@ -4281,6 +4291,38 @@ public function order_delete_order_before($route, $data) {
$this->model_extension_payment_paypal->deletePayPalOrder($order_id);
$this->model_extension_payment_paypal->deletePayPalOrderRecurring($order_id);
}

/**
* Add Report
*
* @param string $route
* @param array<string, mixed> $args
*
* @return void
*
* catalog/model/extension/payment/paypal/addOrderRecurringTransaction/before
*/
public function addReport(&$route, &$args): void {
if (isset($args['order_recurring_id'])) {
$order_recurring_id = (int)$args['order_recurring_id'];
} else {
$order_recurring_id = 0;
}

if ($order_recurring_id) {
$this->load->model('checkout/recurring');

if (isset($this->request->server['HTTP_X_REAL_IP'])) {
$ip = $this->request->server['HTTP_X_REAL_IP'];
} elseif (oc_get_ip()) {
$ip = oc_get_ip();
} else {
$ip = '';
}

$this->model_checkout_recurring->addReport($order_recurring_id, $this->config->get('config_store_id'), $ip, $this->session->data['payment_address']['country']);
}
}

private function validateShipping($code) {
$this->load->language('checkout/cart');
Expand Down
21 changes: 21 additions & 0 deletions upload/catalog/model/checkout/recurring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Class Recurring
*
* @package Catalog\Model\Checkout
*/
class ModelCheckoutRecurring extends Model {
/**
* Add Recurring Report
*
* @param int $order_recurring_id
* @param int $store_id
* @param string $ip
* @param string $country
*
* @return void
*/
public function addReport(int $order_recurring_id, int $store_id, string $ip, string $country): void {
$this->db->query("INSERT INTO `" . DB_PREFIX . "order_recurring_report` SET `order_recurring_id` = '" . (int)$order_recurring_id . "', `store_id` = '" . (int)$store_id . "', `ip` = '" . $this->db->escape($ip) . "', `country` = '" . $this->db->escape($country) . "', `date_added` = NOW()");
}
}
2 changes: 2 additions & 0 deletions upload/install/opencart.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,8 @@ INSERT INTO `oc_event` (`code`, `trigger`, `action`, `status`, `sort_order`) VAL
('mail_subscription_cancel', 'catalog/model/checkout/order/editOrder/after', 'mail/subscription/cancel', 1, 0);
INSERT INTO `oc_event` (`code`, `trigger`, `action`, `status`, `sort_order`) VALUES
('subscription_charge', 'catalog/model/checkout/order/addHistory/before', 'account/subscription/charge', 1, 0);
INSERT INTO `oc_event` (`code`, `trigger`, `action`, `status`, `sort_order`) VALUES
('recurring_report', 'catalog/model/extension/payment/paypal/addOrderRecurringTransaction/before', 'extension/payment/paypal/addReport', 0, 0);

-----------------------------------------------------------

Expand Down

0 comments on commit 4fdea08

Please sign in to comment.