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

Add support for free trials for subscription orders. #111

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [3.7.2]
- Add support for free trials for subscription orders.

## [3.7.1]
- Fix: Resolved memory issues encountered during checkout for logged-in users.

Expand Down
6 changes: 3 additions & 3 deletions altapay.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* Author URI: https://altapay.com
* Text Domain: altapay
* Domain Path: /languages
* Version: 3.7.1
* Version: 3.7.2
* Name: SDM_Altapay
* WC requires at least: 3.9.0
* WC tested up to: 9.2.3
* WC tested up to: 9.3.3
*
* @package Altapay
*/
Expand Down Expand Up @@ -41,7 +41,7 @@
}

if ( ! defined( 'ALTAPAY_PLUGIN_VERSION' ) ) {
define( 'ALTAPAY_PLUGIN_VERSION', '3.7.1' );
define( 'ALTAPAY_PLUGIN_VERSION', '3.7.2' );
}

// Include the autoloader, so we can dynamically include the rest of the classes.
Expand Down
83 changes: 56 additions & 27 deletions classes/core/AltapaySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use AltaPay\vendor\GuzzleHttp\Exception\ClientException;
use WC_Order;
use Altapay\Classes\Core;
use Altapay\Api\Subscription\ChargeSubscription;
use Altapay\Exceptions\ResponseHeaderException;

class AltapaySettings {

Expand Down Expand Up @@ -52,13 +54,26 @@ public function altapayOrderStatusCompleted( $orderID ) {
$order = new WC_Order( $orderID );
$txnID = $order->get_transaction_id();

$subscription = false;
$authType = 'payment';
if ( class_exists( 'WC_Subscriptions_Order' ) && ( wcs_order_contains_subscription( $orderID, 'parent' ) || wcs_order_contains_subscription( $orderID, 'renewal' ) ) ) {

if ( $order->get_total() == 0 ) {
return;
}

$txnID = $order->get_meta( '_agreement_id' );
$subscription = true;
$authType = 'subscription_payment';
}

if ( ! $txnID ) {
return;
}

$login = $this->altapayApiLogin();
if ( ! $login || is_wp_error( $login ) ) {
echo '<p><b>' . __( 'Could not connect to AltaPay!', 'altapay' ) . '</b></p>';
error_log( 'Could not connect to AltaPay!' );

return;
}
Expand Down Expand Up @@ -88,37 +103,51 @@ public function altapayOrderStatusCompleted( $orderID ) {
} else { // Order wasn't captured and must be captured now.
$amount = $pay->ReservedAmount; // Amount to capture.

$api = new CaptureReservation( $this->getAuth() );
$api->setAmount( round( $amount, 2 ) );
$api->setTransaction( $txnID );
try {
if ( $subscription === true ) {
$api = new ChargeSubscription( $this->getAuth() );
} else {
$api = new CaptureReservation( $this->getAuth() );
}

$response = $api->call();
if ( $response->Result !== 'Success' ) {
$order->add_order_note(
__(
'Capture failed: ' . $response->MerchantErrorMessage,
'Altapay'
)
); // log to history.
$this->saveCaptureFailedMessage(
'Capture failed for order ' . $orderID . ': ' . $response->MerchantErrorMessage
);
$api->setAmount( round( $amount, 2 ) );
$api->setTransaction( $txnID );

$response = $api->call();
if ( $response->Result !== 'Success' ) {
$order->add_order_note(
__(
'Capture failed: ' . $response->MerchantErrorMessage,
'Altapay'
)
); // log to history.
$this->saveCaptureFailedMessage(
'Capture failed for order ' . $orderID . ': ' . $response->MerchantErrorMessage
);

return;
}
return;
}

$order->update_meta_data( '_captured', true );
$order->add_order_note( __( 'Order captured: amount: ' . $amount, 'Altapay' ) );
$order->save();

$order->update_meta_data( '_captured', true );
$order->add_order_note( __( 'Order captured: amount: ' . $amount, 'Altapay' ) );
$order->save();
$transactions = json_decode( wp_json_encode( $response->Transactions ), true );
$latest_transaction = $this->getLatestTransaction( $transactions, $authType );
$transaction = $transactions[ $latest_transaction ];
$txn_id = $transaction['TransactionId'];

$transactions = json_decode( wp_json_encode( $response->Transactions ), true );
$latest_transaction = $this->getLatestTransaction( $transactions, 'payment' );
$transaction = $transactions[ $latest_transaction ];
$txn_id = $transaction['TransactionId'];
if ( $subscription === true ) {
$order->set_transaction_id( $txn_id );
$order->save();
}

$reconciliation = new Core\AltapayReconciliation();
foreach ( $transaction['ReconciliationIdentifiers'] as $val ) {
$reconciliation->saveReconciliationIdentifier( $orderID, $txn_id, $val['Id'], $val['Type'] );
$reconciliation = new Core\AltapayReconciliation();
foreach ( $transaction['ReconciliationIdentifiers'] as $val ) {
$reconciliation->saveReconciliationIdentifier( $orderID, $txn_id, $val['Id'], $val['Type'] );
}
} catch ( ResponseHeaderException | Exception $e ) {
error_log( 'Exception ' . $e->getMessage() );
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions classes/util/UtilMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function createOrderLines( $order, $products = array(), $wcRefund = false
$subtotal = $item->get_subtotal();
$discount = 0;

if ( $total == 0 && $isSubscription ) {
$total = (float) $item->get_product()->get_regular_price();
}

if ( $total == 0 ) {
continue;
}
Expand Down
9 changes: 6 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Contributors: altapay_integrations
Tags: AltaPay, Gateway, Payments, WooCommerce, Payment Card Industry
Requires PHP: 7.4
Requires at least: 5.0
Tested up to: 6.6.1
Stable tag: 3.7.1
Tested up to: 6.6.2
Stable tag: 3.7.2
License: MIT
WC requires at least: 3.9.0
WC tested up to: 9.2.3
WC tested up to: 9.3.3
License URI: http://www.gnu.org/licenses/gpl-2.0.html

A plugin that integrates your WooCommerce web shop to the AltaPay payments gateway.
Expand Down Expand Up @@ -39,6 +39,9 @@ AltaPay's Payment Gateway for WooCommerce provides merchants with access to a fu

== Changelog ==

= 3.7.2 =
* Add support for free trials for subscription orders.

= 3.7.1 =
* Fix: Memory issues encountered during checkout for logged-in users.

Expand Down
21 changes: 20 additions & 1 deletion views/paymentClass.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class WC_Gateway_{key} extends WC_Payment_Gateway {

// TODO Get terminal form instance
$terminal = $this->terminal;
$amount = $order->get_total();
$amount = $this->getOrderAmount( $order );
$currency = $order->get_currency();
$customerInfo = $this->setCustomer( $order );
$cookie = isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : '';
Expand Down Expand Up @@ -655,6 +655,25 @@ class WC_Gateway_{key} extends WC_Payment_Gateway {
return true;
}

/**
* @param $order
*
* @return float
*/
private function getOrderAmount( $order ) {

$amount = $order->get_total();

if ( $amount == 0 && class_exists( 'WC_Subscriptions_Order' ) && wcs_order_contains_subscription( $order ) ) {
$subscriptions = wcs_get_subscriptions_for_order( $order );
foreach ( $subscriptions as $subscription ) {
$amount += $subscription->get_total();
}
}

return $amount;
}

/**
* @param array $addressInfo
* @param Address $address
Expand Down
6 changes: 3 additions & 3 deletions wiki.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ In order to reconcile payments please follow the steps below:
## Supported versions

Minimum system requirements are:
- WordPress min. 5.0 – max. 6.6.1
- WooCommerce min. 3.9.0 – max. 9.2.3
- WordPress min. 5.0 – max. 6.6.2
- WooCommerce min. 3.9.0 – max. 9.3.3
- PHP 7.4 and above
- PHP-bcmath library installed.
- PHP-curl MUST be enabled.

The latest tested version is:
- WordPress 6.6.1, WooCommerce 9.2.3 and PHP 8.1
- WordPress 6.6.2, WooCommerce 9.3.3 and PHP 8.1


## Troubleshooting
Expand Down
Loading