Passed
Pull Request — master (#126)
by Kiran
04:13
created

wpinv-payment-functions.php ➔ wpinv_get_pretty_subscription_period()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 27
Code Lines 23

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 9
nop 1
dl 27
loc 27
rs 4.909
c 0
b 0
f 0
1
<?php
2
function wpinv_is_subscription_payment( $invoice = '' ) {
3
    if ( empty( $invoice ) ) {
4
        return false;
5
    }
6
    
7
    if ( !is_object( $invoice ) && is_scalar( $invoice ) ) {
8
        $invoice = wpinv_get_invoice( $invoice );
9
    }
10
    
11
    if ( empty( $invoice ) ) {
12
        return false;
13
    }
14
        
15
    if ( $invoice->is_renewal() ) {
16
        return true;
17
    }
18
19
    return false;
20
}
21
22
function wpinv_payment_link_transaction_id( $invoice = '' ) {
23
    if ( empty( $invoice ) ) {
24
        return false;
25
    }
26
    
27
    if ( !is_object( $invoice ) && is_scalar( $invoice ) ) {
28
        $invoice = wpinv_get_invoice( $invoice );
29
    }
30
    
31
    if ( empty( $invoice ) ) {
32
        return false;
33
    }
34
35
    return apply_filters( 'wpinv_payment_details_transaction_id-' . $invoice->gateway, $invoice->get_transaction_id(), $invoice->ID, $invoice );
36
}
37
38
function wpinv_subscription_initial_payment_desc( $amount, $period, $interval, $trial_period = '', $trial_interval = 0 ) {
39
    $interval   = (int)$interval > 0 ? (int)$interval : 1;
40
    
41
    if ( $trial_interval > 0 && !empty( $trial_period ) ) {
42
        $amount = __( 'Free', 'invoicing' );
43
        $interval = $trial_interval;
44
        $period = $trial_period;
45
    }
46
    
47
    $description = '';
48
    switch ( $period ) {
49
        case 'D' :
50
        case 'day' :
51
            $description = wp_sprintf( _n( '%s for the first day.', '%s for the first %d days.', $interval, 'invoicing' ), $amount, $interval );
52
            break;
53
        case 'W' :
54
        case 'week' :
55
            $description = wp_sprintf( _n( '%s for the first week.', '%s for the first %d weeks.', $interval, 'invoicing' ), $amount, $interval );
56
            break;
57
        case 'M' :
58
        case 'month' :
59
            $description = wp_sprintf( _n( '%s for the first month.', '%s for the first %d months.', $interval, 'invoicing' ), $amount, $interval );
60
            break;
61
        case 'Y' :
62
        case 'year' :
63
            $description = wp_sprintf( _n( '%s for the first year.', '%s for the first %d years.', $interval, 'invoicing' ), $amount, $interval );
64
            break;
65
    }
66
67
    return apply_filters( 'wpinv_subscription_initial_payment_desc', $description, $amount, $period, $interval, $trial_period, $trial_interval  );
68
}
69
70
function wpinv_subscription_recurring_payment_desc( $amount, $period, $interval, $bill_times = 0, $trial_period = '', $trial_interval = 0 ) {
71
    $interval   = (int)$interval > 0 ? (int)$interval : 1;
72
    $bill_times = (int)$bill_times > 0 ? (int)$bill_times : 0;
73
    
74
    $description = '';
75
    switch ( $period ) {
76
        case 'D' :
77 View Code Duplication
        case 'day' :            
78
            if ( (int)$bill_times > 0 ) {
79
                if ( $interval > 1 ) {
80
                    if ( $bill_times > 1 ) {
81
                        $description = wp_sprintf( __( '%s for each %d days, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
82
                    } else {
83
                        $description = wp_sprintf( __( '%s for %d days.', 'invoicing' ), $amount, $interval );
84
                    }
85
                } else {
86
                    $description = wp_sprintf( _n( '%s for one day.', '%s for each day, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
87
                }
88
            } else {
89
                $description = wp_sprintf( _n( '%s for each day.', '%s for each %d days.', $interval, 'invoicing'), $amount, $interval );
90
            }
91
            break;
92
        case 'W' :
93 View Code Duplication
        case 'week' :            
94
            if ( (int)$bill_times > 0 ) {
95
                if ( $interval > 1 ) {
96
                    if ( $bill_times > 1 ) {
97
                        $description = wp_sprintf( __( '%s for each %d weeks, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
98
                    } else {
99
                        $description = wp_sprintf( __( '%s for %d weeks.', 'invoicing' ), $amount, $interval );
100
                    }
101
                } else {
102
                    $description = wp_sprintf( _n( '%s for one week.', '%s for each week, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
103
                }
104
            } else {
105
                $description = wp_sprintf( _n( '%s for each week.', '%s for each %d weeks.', $interval, 'invoicing' ), $amount, $interval );
106
            }
107
            break;
108
        case 'M' :
109 View Code Duplication
        case 'month' :            
110
            if ( (int)$bill_times > 0 ) {
111
                if ( $interval > 1 ) {
112
                    if ( $bill_times > 1 ) {
113
                        $description = wp_sprintf( __( '%s for each %d months, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
114
                    } else {
115
                        $description = wp_sprintf( __( '%s for %d months.', 'invoicing' ), $amount, $interval );
116
                    }
117
                } else {
118
                    $description = wp_sprintf( _n( '%s for one month.', '%s for each month, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
119
                }
120
            } else {
121
                $description = wp_sprintf( _n( '%s for each month.', '%s for each %d months.', $interval, 'invoicing' ), $amount, $interval );
122
            }
123
            break;
124
        case 'Y' :
125 View Code Duplication
        case 'year' :            
126
            if ( (int)$bill_times > 0 ) {
127
                if ( $interval > 1 ) {
128
                    if ( $bill_times > 1 ) {
129
                        $description = wp_sprintf( __( '%s for each %d years, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
130
                    } else {
131
                        $description = wp_sprintf( __( '%s for %d years.', 'invoicing'), $amount, $interval );
132
                    }
133
                } else {
134
                    $description = wp_sprintf( _n( '%s for one year.', '%s for each year, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
135
                }
136
            } else {
137
                $description = wp_sprintf( _n( '%s for each year.', '%s for each %d years.', $interval, 'invoicing' ), $amount, $interval );
138
            }
139
            break;
140
    }
141
142
    return apply_filters( 'wpinv_subscription_recurring_payment_desc', $description, $amount, $period, $interval, $bill_times, $trial_period, $trial_interval );
143
}
144
145
function wpinv_subscription_payment_desc( $invoice ) {
146
    if ( empty( $invoice ) ) {
147
        return NULL;
148
    }
149
    
150
    $description = '';
151
    if ( $invoice->is_parent() && $item = $invoice->get_recurring( true ) ) {
152
        if ( $item->has_free_trial() ) {
153
            $trial_period = $item->get_trial_period();
154
            $trial_interval = $item->get_trial_interval();
155
        } else {
156
            $trial_period = '';
157
            $trial_interval = 0;
158
        }
159
        
160
        $description = wpinv_get_billing_cycle( $invoice->get_total(), $invoice->get_recurring_details( 'total' ), $item->get_recurring_period(), $item->get_recurring_interval(), $item->get_recurring_limit(), $trial_period, $trial_interval, $invoice->get_currency() );
161
    }
162
    
163
    return apply_filters( 'wpinv_subscription_payment_desc', $description, $invoice );
164
}
165
166
function wpinv_get_billing_cycle( $initial, $recurring, $period, $interval, $bill_times, $trial_period = '', $trial_interval = 0, $currency = '' ) {
167
    $initial_total      = wpinv_round_amount( $initial );
168
    $recurring_total    = wpinv_round_amount( $recurring );
169
    
170
    if ( $trial_interval > 0 && !empty( $trial_period ) ) {
171
        // Free trial
172
    } else {
173
        if ( $bill_times == 1 ) {
174
            $recurring_total = $initial_total;
175
        } else if ( $bill_times > 1 && $initial_total != $recurring_total ) {
176
            $bill_times--;
177
        }
178
    }
179
    
180
    $initial_amount     = wpinv_price( wpinv_format_amount( $initial_total ), $currency );
181
    $recurring_amount   = wpinv_price( wpinv_format_amount( $recurring_total ), $currency );
182
    
183
    $recurring          = wpinv_subscription_recurring_payment_desc( $recurring_amount, $period, $interval, $bill_times, $trial_period, $trial_interval );
184
        
185
    if ( $initial_total != $recurring_total ) {
186
        $initial        = wpinv_subscription_initial_payment_desc( $initial_amount, $period, $interval, $trial_period, $trial_interval );
187
        
188
        $description    = wp_sprintf( __( '%s Then %s', 'invoicing' ), $initial, $recurring );
189
    } else {
190
        $description    = $recurring;
191
    }
192
    
193
    return apply_filters( 'wpinv_get_billing_cycle', $description, $initial, $recurring, $period, $interval, $bill_times, $trial_period, $trial_interval, $currency );
194
}
195
196
function wpinv_recurring_send_payment_failed( $invoice ) {
197
    if ( !empty( $invoice->ID ) ) {
198
        wpinv_failed_invoice_notification( $invoice->ID );
199
    }
200
}
201
add_action( 'wpinv_recurring_payment_failed', 'wpinv_recurring_send_payment_failed', 10, 1 );