Skip to content

Commit

Permalink
Fix - ProRate with PayPal
Browse files Browse the repository at this point in the history
  • Loading branch information
panoslyrakis committed Aug 7, 2019
1 parent 69a644c commit 3a107ff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,13 @@ private function prepare_fields() {
'value' => $period_type,
);

$once_duration = !empty( $invoice->discount ) && !empty( $invoice->duration ) && MS_Addon_Coupon_Model::DURATION_ONCE === $invoice->duration;
$once_duration = (
( !empty( $invoice->discount ) && !empty( $invoice->duration ) && MS_Addon_Coupon_Model::DURATION_ONCE === $invoice->duration ) ||
( MS_Addon_Prorate::is_active() && ! empty( $invoice->pro_rate ) && $invoice->pro_rate > 0 )
);

if ( $once_duration ) {

$n = empty( $fields['p1'] ) ? 1 : 2;

$fields['a'.$n] = array(
Expand All @@ -283,6 +288,7 @@ private function prepare_fields() {
'value' => $period_type,
);
}


$custom_period_type = isset( $period_type ) ? $period_type : '';
$custom_period_value = isset( $period_value ) ? $period_value : '';
Expand Down
73 changes: 68 additions & 5 deletions premium/addon/prorate/class-ms-addon-prorate.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,22 @@ public function get_id() {
*/
public function init() {
if ( self::is_active() ) {

//$this->add_filter(
// 'ms_model_invoice_create_before_save',
// 'add_discount'
//);

$this->add_filter(
'ms_signup_payment_details',
'add_discount',
10, 2
);

$this->add_filter(
'ms_model_invoice_create_before_save',
'add_discount'
'ms_model_relationship_get_payment_description/recurring',
'payment_description_recurring',
10, 6
);
}
}
Expand Down Expand Up @@ -86,9 +99,8 @@ public function register( $list ) {
* @param MS_Model_Invoice $invoice
* @return MS_Model_Invoice Modified Invoice.
*/
public function add_discount( $invoice ) {
$subscription = $invoice->get_subscription();

public function add_discount( $invoice, $subscription ) {

// If memberships were already cancelled don't pro-rate again!
if ( $subscription->cancelled_memberships ) { return $invoice; }

Expand Down Expand Up @@ -185,4 +197,55 @@ protected function get_discount( $subscription ) {
$subscription
);
}


/**
* Sets the payment description on checkout page if a valid coupon is applied
*
* @since 1.1.6
* @param String $desc
* @param Boolean $short
* @param String $currency
* @param String $total_price Price where discount has already been applied
* @param MS_Model_Membership $membership
* @param MS_Model_Invoice $invoice
* @return String Payment description
*/
public function payment_description_recurring( $desc, $short, $currency, $total_price, $membership, $invoice ){

if ( 1 == $membership->pay_cycle_repetitions ) return $desc;

if ( ! empty( $invoice->pro_rate ) && $invoice->pro_rate > 0 && ! empty( $_REQUEST['membership_id'] ) ) {

$lbl = '';
if ( $membership->pay_cycle_repetitions > 1 ) {
// Fixed number of payments (more than 1)
if ( $short ) {
$lbl = __( '<span class="price">%1$s %2$s</span> first time and then <span class="price">%1$s %3$s</span> (each %4$s)', 'membership2' );
} else {
$lbl = __( 'First payment <span class="price">%1$s %2$s</span> and then you will make %5$s payments of <span class="price">%1$s %3$s</span>, one each %4$s.', 'membership2' );
}
} else {
// Indefinite number of payments
if ( $short ) {
$lbl = __( '<span class="price">%1$s %2$s</span> first time and then <span class="price">%1$s %3$s</span> (each %4$s)', 'membership2' );
} else {
$lbl = __( 'You will pay <span class="price">%1$s %2$s</span> first time and then <span class="price">%1$s %3$s</span> each %4$s.', 'membership2' );
}
}

$desc = sprintf(
$lbl,
$currency,
$total_price,
$membership->price,
MS_Helper_Period::get_period_desc( $membership->pay_cycle_period ),
$membership->pay_cycle_repetitions - 1
);

}

return $desc;
}

}

0 comments on commit 3a107ff

Please sign in to comment.