aboutsummaryrefslogtreecommitdiffstats
path: root/public/catalog/controller/extension/recurring
diff options
context:
space:
mode:
authorJesús <heckyel@hyperbola.info>2019-08-18 21:14:58 -0500
committerJesús <heckyel@hyperbola.info>2019-08-18 21:14:58 -0500
commit2eed7b082f83630301e51f57ca8394de228a8605 (patch)
tree1d19962d22d30f99317d9276e4bae7744fc93fc2 /public/catalog/controller/extension/recurring
downloadlibrecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.lz
librecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.xz
librecart-2eed7b082f83630301e51f57ca8394de228a8605.zip
first commit
Diffstat (limited to 'public/catalog/controller/extension/recurring')
-rw-r--r--public/catalog/controller/extension/recurring/pp_express.php106
-rw-r--r--public/catalog/controller/extension/recurring/squareup.php170
2 files changed, 276 insertions, 0 deletions
diff --git a/public/catalog/controller/extension/recurring/pp_express.php b/public/catalog/controller/extension/recurring/pp_express.php
new file mode 100644
index 0000000..0124770
--- /dev/null
+++ b/public/catalog/controller/extension/recurring/pp_express.php
@@ -0,0 +1,106 @@
+<?php
+class ControllerExtensionRecurringPPExpress extends Controller {
+ public function index() {
+ $this->load->language('extension/recurring/pp_express');
+
+ if (isset($this->request->get['order_recurring_id'])) {
+ $order_recurring_id = $this->request->get['order_recurring_id'];
+ } else {
+ $order_recurring_id = 0;
+ }
+
+ $this->load->model('account/recurring');
+
+ $recurring_info = $this->model_account_recurring->getOrderRecurring($order_recurring_id);
+
+ if ($recurring_info) {
+ $data['continue'] = $this->url->link('account/recurring', '', true);
+
+ if ($recurring_info['status'] == 2 || $recurring_info['status'] == 3) {
+ $data['order_recurring_id'] = $order_recurring_id;
+ } else {
+ $data['order_recurring_id'] = '';
+ }
+
+ return $this->load->view('extension/recurring/pp_express', $data);
+ }
+ }
+
+ public function cancel() {
+ $json = array();
+
+ $this->load->language('extension/recurring/pp_express');
+
+ //cancel an active recurring
+ $this->load->model('account/recurring');
+
+ if (isset($this->request->get['order_recurring_id'])) {
+ $order_recurring_id = $this->request->get['order_recurring_id'];
+ } else {
+ $order_recurring_id = 0;
+ }
+
+ $recurring_info = $this->model_account_recurring->getOrderRecurring($order_recurring_id);
+
+ if ($recurring_info && $recurring_info['reference']) {
+ if ($this->config->get('payment_pp_express_test')) {
+ $api_url = 'https://api-3t.sandbox.paypal.com/nvp';
+ $api_username = $this->config->get('payment_pp_express_sandbox_username');
+ $api_password = $this->config->get('payment_pp_express_sandbox_password');
+ $api_signature = $this->config->get('payment_pp_express_sandbox_signature');
+ } else {
+ $api_url = 'https://api-3t.paypal.com/nvp';
+ $api_username = $this->config->get('payment_pp_express_username');
+ $api_password = $this->config->get('payment_pp_express_password');
+ $api_signature = $this->config->get('payment_pp_express_signature');
+ }
+
+ $request = array(
+ 'USER' => $api_username,
+ 'PWD' => $api_password,
+ 'SIGNATURE' => $api_signature,
+ 'VERSION' => '109.0',
+ 'BUTTONSOURCE' => 'OpenCart_2.0_EC',
+ 'METHOD' => 'SetExpressCheckout',
+ 'METHOD' => 'ManageRecurringPaymentsProfileStatus',
+ 'PROFILEID' => $recurring_info['reference'],
+ 'ACTION' => 'Cancel'
+ );
+
+ $curl = curl_init($api_url);
+
+ curl_setopt($curl, CURLOPT_POST, true);
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($curl, CURLOPT_HEADER, false);
+ curl_setopt($curl, CURLOPT_TIMEOUT, 30);
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
+
+ $response = curl_exec($curl);
+
+ if (!$response) {
+ $this->log(sprintf($this->language->get('error_curl'), curl_errno($curl), curl_error($curl)));
+ }
+
+ curl_close($curl);
+
+ $response_info = array();
+
+ parse_str($response, $response_info);
+
+ if (isset($response_info['PROFILEID'])) {
+ $this->model_account_recurring->editOrderRecurringStatus($order_recurring_id, 4);
+ $this->model_account_recurring->addOrderRecurringTransaction($order_recurring_id, 5);
+
+ $json['success'] = $this->language->get('text_cancelled');
+ } else {
+ $json['error'] = sprintf($this->language->get('error_not_cancelled'), $response_info['L_LONGMESSAGE0']);
+ }
+ } else {
+ $json['error'] = $this->language->get('error_not_found');
+ }
+
+ $this->response->addHeader('Content-Type: application/json');
+ $this->response->setOutput(json_encode($json));
+ }
+} \ No newline at end of file
diff --git a/public/catalog/controller/extension/recurring/squareup.php b/public/catalog/controller/extension/recurring/squareup.php
new file mode 100644
index 0000000..eabc932
--- /dev/null
+++ b/public/catalog/controller/extension/recurring/squareup.php
@@ -0,0 +1,170 @@
+<?php
+class ControllerExtensionRecurringSquareup extends Controller {
+ public function index() {
+ $this->load->language('extension/recurring/squareup');
+
+ $this->load->model('account/recurring');
+ $this->load->model('extension/payment/squareup');
+
+ if (isset($this->request->get['order_recurring_id'])) {
+ $order_recurring_id = $this->request->get['order_recurring_id'];
+ } else {
+ $order_recurring_id = 0;
+ }
+
+ $recurring_info = $this->model_account_recurring->getOrderRecurring($order_recurring_id);
+
+ if ($recurring_info) {
+ $data['cancel_url'] = html_entity_decode($this->url->link('extension/recurring/squareup/cancel', 'order_recurring_id=' . $order_recurring_id, 'SSL'));
+
+ $data['continue'] = $this->url->link('account/recurring', '', true);
+
+ if ($recurring_info['status'] == ModelExtensionPaymentSquareup::RECURRING_ACTIVE) {
+ $data['order_recurring_id'] = $order_recurring_id;
+ } else {
+ $data['order_recurring_id'] = '';
+ }
+
+ return $this->load->view('extension/recurring/squareup', $data);
+ }
+ }
+
+ public function cancel() {
+ $this->load->language('extension/recurring/squareup');
+
+ $this->load->model('account/recurring');
+ $this->load->model('extension/payment/squareup');
+
+ if (isset($this->request->get['order_recurring_id'])) {
+ $order_recurring_id = $this->request->get['order_recurring_id'];
+ } else {
+ $order_recurring_id = 0;
+ }
+
+ $json = array();
+
+ $recurring_info = $this->model_account_recurring->getOrderRecurring($order_recurring_id);
+
+ if ($recurring_info) {
+ $this->model_account_recurring->editOrderRecurringStatus($order_recurring_id, ModelExtensionPaymentSquareup::RECURRING_CANCELLED);
+
+ $this->load->model('checkout/order');
+
+ $order_info = $this->model_checkout_order->getOrder($recurring_info['order_id']);
+
+ $this->model_checkout_order->addOrderHistory($recurring_info['order_id'], $order_info['order_status_id'], $this->language->get('text_order_history_cancel'), true);
+
+ $json['success'] = $this->language->get('text_canceled');
+ } else {
+ $json['error'] = $this->language->get('error_not_found');
+ }
+
+ $this->response->addHeader('Content-Type: application/json');
+ $this->response->setOutput(json_encode($json));
+ }
+
+ public function recurring() {
+ $this->load->language('extension/payment/squareup');
+
+ $this->load->model('extension/payment/squareup');
+
+ if (!$this->model_extension_payment_squareup->validateCRON()) {
+ return;
+ }
+
+ $this->load->library('squareup');
+
+ $result = array(
+ 'transaction_success' => array(),
+ 'transaction_error' => array(),
+ 'transaction_fail' => array(),
+ 'token_update_error' => ''
+ );
+
+ $result['token_update_error'] = $this->model_extension_payment_squareup->updateToken();
+
+ $this->load->model('checkout/order');
+
+ foreach ($this->model_extension_payment_squareup->nextRecurringPayments() as $payment) {
+ try {
+ if (!$payment['is_free']) {
+ $transaction = $this->squareup->addTransaction($payment['transaction']);
+
+ $transaction_status = !empty($transaction['tenders'][0]['card_details']['status']) ?
+ strtolower($transaction['tenders'][0]['card_details']['status']) : '';
+
+ $target_currency = $transaction['tenders'][0]['amount_money']['currency'];
+
+ $amount = $this->squareup->standardDenomination($transaction['tenders'][0]['amount_money']['amount'], $target_currency);
+
+ $this->model_extension_payment_squareup->addTransaction($transaction, $this->config->get('payment_squareup_merchant_id'), $payment['billing_address'], $payment['order_id'], "CRON JOB", "127.0.0.1");
+
+ $reference = $transaction['id'];
+ } else {
+ $amount = 0;
+ $target_currency = $this->config->get('config_currency');
+ $reference = '';
+ $transaction_status = 'captured';
+ }
+
+ $success = $transaction_status == 'captured';
+
+ $this->model_extension_payment_squareup->addRecurringTransaction($payment['order_recurring_id'], $reference, $amount, $success);
+
+ $trial_expired = false;
+ $recurring_expired = false;
+ $profile_suspended = false;
+
+ if ($success) {
+ $trial_expired = $this->model_extension_payment_squareup->updateRecurringTrial($payment['order_recurring_id']);
+
+ $recurring_expired = $this->model_extension_payment_squareup->updateRecurringExpired($payment['order_recurring_id']);
+
+ $result['transaction_success'][$payment['order_recurring_id']] = $this->currency->format($amount, $target_currency);
+ } else {
+ // Transaction was not successful. Suspend the recurring profile.
+ $profile_suspended = $this->model_extension_payment_squareup->suspendRecurringProfile($payment['order_recurring_id']);
+
+ $result['transaction_fail'][$payment['order_recurring_id']] = $this->currency->format($amount, $target_currency);
+ }
+
+
+ $order_status_id = $this->config->get('payment_squareup_status_' . $transaction_status);
+
+ if ($order_status_id) {
+ if (!$payment['is_free']) {
+ $order_status_comment = $this->language->get('squareup_status_comment_' . $transaction_status);
+ } else {
+ $order_status_comment = '';
+ }
+
+ if ($profile_suspended) {
+ $order_status_comment .= $this->language->get('text_squareup_profile_suspended');
+ }
+
+ if ($trial_expired) {
+ $order_status_comment .= $this->language->get('text_squareup_trial_expired');
+ }
+
+ if ($recurring_expired) {
+ $order_status_comment .= $this->language->get('text_squareup_recurring_expired');
+ }
+
+ if ($success) {
+ $notify = (bool)$this->config->get('payment_squareup_notify_recurring_success');
+ } else {
+ $notify = (bool)$this->config->get('payment_squareup_notify_recurring_fail');
+ }
+
+ $this->model_checkout_order->addOrderHistory($payment['order_id'], $order_status_id, trim($order_status_comment), $notify);
+ }
+ } catch (\Squareup\Exception $e) {
+ $result['transaction_error'][] = '[ID: ' . $payment['order_recurring_id'] . '] - ' . $e->getMessage();
+ }
+ };
+
+ if ($this->config->get('payment_squareup_cron_email_status')) {
+ $this->model_extension_payment_squareup->cronEmail($result);
+ }
+ }
+} \ No newline at end of file