Skip to content

Commit

Permalink
feat: implement period campaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcuzz committed Aug 14, 2023
1 parent 4611a96 commit 6081710
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Alternatively you could look into using WooCommerce "Early renewals": [https://d

= 1.16.0 =
* Added: a `wc_vipps_recurring_after_payment_complete` action
* Added: Implementation of period campaigns. It will now properly inform your customers of a campaigns length in the app.

= 1.15.2 =
* Fix: No longer attempt to cancel agreements that are already cancelled.
Expand Down
4 changes: 2 additions & 2 deletions includes/models/WC_Vipps_Agreement_Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class WC_Vipps_Agreement_Campaign extends WC_Vipps_Model {
*/
public $event_date;
public ?string $event_text = null;
public ?WC_Vipps_Agreement_Interval $period = null;
public ?WC_Vipps_Agreement_Campaign_Period $period = null;
public ?WC_Vipps_Agreement_Interval $interval = null;

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ public function set_event_text( string $event_text ): self {
return $this;
}

public function set_period( WC_Vipps_Agreement_Interval $period ): self {
public function set_period( WC_Vipps_Agreement_Campaign_Period $period ): self {
$this->period = $period;

return $this;
Expand Down
56 changes: 56 additions & 0 deletions includes/models/WC_Vipps_Agreement_Campaign_Period.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

defined( 'ABSPATH' ) || exit;

class WC_Vipps_Agreement_Campaign_Period extends WC_Vipps_Model {
public const UNIT_YEAR = "YEAR";
public const UNIT_MONTH = "MONTH";
public const UNIT_WEEK = "WEEK";
public const UNIT_DAY = "DAY";

protected array $valid_units = [
self::UNIT_YEAR,
self::UNIT_MONTH,
self::UNIT_WEEK,
self::UNIT_DAY
];

protected array $required_fields = [ "unit", "count" ];

public ?string $unit = null;
public ?int $count = null;

/**
* @throws WC_Vipps_Recurring_Invalid_Value_Exception
*/
public function set_unit( string $unit ): self {
if ( ! in_array( $unit, $this->valid_units ) ) {
$class = get_class( $this );
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$unit is not a valid value for `unit` in $class." );
}

$this->unit = $unit;

return $this;
}

public function set_count( int $count ): self {
$this->count = $count;

return $this;
}

/**
* @throws WC_Vipps_Recurring_Missing_Value_Exception
*/
public function to_array( bool $check_required = false ): array {
if ( $check_required ) {
$this->check_required();
}

return [
"unit" => $this->unit,
"count" => $this->count
];
}
}
10 changes: 1 addition & 9 deletions includes/models/WC_Vipps_Agreement_Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class WC_Vipps_Agreement_Interval extends WC_Vipps_Model {

public ?string $unit = null;
public ?int $count = null;
public ?string $text = null;

/**
* @throws WC_Vipps_Recurring_Invalid_Value_Exception
Expand All @@ -41,12 +40,6 @@ public function set_count( int $count ): self {
return $this;
}

public function set_text( string $text ): self {
$this->text = $text;

return $this;
}

/**
* @throws WC_Vipps_Recurring_Missing_Value_Exception
*/
Expand All @@ -57,8 +50,7 @@ public function to_array( bool $check_required = false ): array {

return [
"unit" => $this->unit,
"count" => $this->count,
"text" => $this->text,
"count" => $this->count
];
}
}
15 changes: 13 additions & 2 deletions includes/wc-gateway-vipps-recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -1559,12 +1559,23 @@ public function process_payment( $order_id, bool $retry = true, bool $previous_e
$campaign_price = $has_free_campaign ? 0 : $order->get_total();
$campaign_end_date = $subscription->get_time( 'end' ) === 0 ? $next_payment : $end_date;

$campaign_type = WC_Vipps_Agreement_Campaign::TYPE_PRICE_CAMPAIGN;
$campaign_period = null;

if ( $has_trial ) {
$campaign_type = WC_Vipps_Agreement_Campaign::TYPE_PERIOD_CAMPAIGN;
$campaign_end_date = null;
$campaign_period = ( new WC_Vipps_Agreement_Campaign_Period() )
->set_count( WC_Subscriptions_Product::get_trial_length( $product ) )
->set_unit( strtoupper( WC_Subscriptions_Product::get_trial_period( $product ) ) );
}

$agreement = $agreement->set_campaign(
( new WC_Vipps_Agreement_Campaign() )
->set_type( WC_Vipps_Agreement_Campaign::TYPE_PRICE_CAMPAIGN )
->set_type( $campaign_type )
->set_price( WC_Vipps_Recurring_Helper::get_vipps_amount( $campaign_price ) )
// ->set_event_date( $start_date )
->set_end( $campaign_end_date )
->set_period( $campaign_period )
);
}

Expand Down
1 change: 1 addition & 0 deletions includes/wc-vipps-models.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once( __DIR__ . '/models/WC_Vipps_Model.php' );
require_once( __DIR__ . '/models/WC_Vipps_Agreement.php' );
require_once( __DIR__ . '/models/WC_Vipps_Agreement_Campaign.php' );
require_once( __DIR__ . '/models/WC_Vipps_Agreement_Campaign_Period.php' );
require_once( __DIR__ . '/models/WC_Vipps_Agreement_Initial_Charge.php' );
require_once( __DIR__ . '/models/WC_Vipps_Agreement_Interval.php' );
require_once( __DIR__ . '/models/WC_Vipps_Agreement_Pricing.php' );
Expand Down

0 comments on commit 6081710

Please sign in to comment.