Passed
Push — master ( 249f07...65bd19 )
by Brian
04:54
created

getpaid_get_recaptcha_secret_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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.', '%1$s for the first %2$d days.', $interval, 'invoicing' ), $amount, $interval );
52
            break;
53
        case 'W':
54
        case 'week':
55
            $description = wp_sprintf( _n( '%s for the first week.', '%1$s for the first %2$d weeks.', $interval, 'invoicing' ), $amount, $interval );
56
            break;
57
        case 'M':
58
        case 'month':
59
            $description = wp_sprintf( _n( '%s for the first month.', '%1$s for the first %2$d months.', $interval, 'invoicing' ), $amount, $interval );
60
            break;
61
        case 'Y':
62
        case 'year':
63
            $description = wp_sprintf( _n( '%s for the first year.', '%1$s for the first %2$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
        case 'day':
78
            if ( (int)$bill_times > 0 ) {
79
                if ( $interval > 1 ) {
80
                    if ( $bill_times > 1 ) {
81
                        $description = wp_sprintf( __( '%1$s for each %2$d days, for %3$d installments.', 'invoicing' ), $amount, $interval, $bill_times );
82
                    } else {
83
                        $description = wp_sprintf( __( '%1$s for %2$d days.', 'invoicing' ), $amount, $interval );
84
                    }
85
                } else {
86
                    $description = wp_sprintf( _n( '%s for one day.', '%1$s for each day, for %2$d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
87
                }
88
            } else {
89
                $description = wp_sprintf( _n( '%s for each day.', '%1$s for each %2$d days.', $interval, 'invoicing' ), $amount, $interval );
90
            }
91
            break;
92
        case 'W':
93
        case 'week':
94
            if ( (int)$bill_times > 0 ) {
95
                if ( $interval > 1 ) {
96
                    if ( $bill_times > 1 ) {
97
                        $description = wp_sprintf( __( '%1$s for each %2$d weeks, for %3$d installments.', 'invoicing' ), $amount, $interval, $bill_times );
98
                    } else {
99
                        $description = wp_sprintf( __( '%1$s for %2$d weeks.', 'invoicing' ), $amount, $interval );
100
                    }
101
                } else {
102
                    $description = wp_sprintf( _n( '%s for one week.', '%1$s for each week, for %2$d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
103
                }
104
            } else {
105
                $description = wp_sprintf( _n( '%s for each week.', '%1$s for each %2$d weeks.', $interval, 'invoicing' ), $amount, $interval );
106
            }
107
            break;
108
        case 'M':
109
        case 'month':
110
            if ( (int)$bill_times > 0 ) {
111
                if ( $interval > 1 ) {
112
                    if ( $bill_times > 1 ) {
113
                        $description = wp_sprintf( __( '%1$s for each %2$d months, for %3$d installments.', 'invoicing' ), $amount, $interval, $bill_times );
114
                    } else {
115
                        $description = wp_sprintf( __( '%1$s for %2$d months.', 'invoicing' ), $amount, $interval );
116
                    }
117
                } else {
118
                    $description = wp_sprintf( _n( '%s for one month.', '%1$s for each month, for %2$d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
119
                }
120
            } else {
121
                $description = wp_sprintf( _n( '%s for each month.', '%1$s for each %2$d months.', $interval, 'invoicing' ), $amount, $interval );
122
            }
123
            break;
124
        case 'Y':
125
        case 'year':
126
            if ( (int)$bill_times > 0 ) {
127
                if ( $interval > 1 ) {
128
                    if ( $bill_times > 1 ) {
129
                        $description = wp_sprintf( __( '%1$s for each %2$d years, for %3$d installments.', 'invoicing' ), $amount, $interval, $bill_times );
130
                    } else {
131
                        $description = wp_sprintf( __( '%1$s for %2$d years.', 'invoicing' ), $amount, $interval );
132
                    }
133
                } else {
134
                    $description = wp_sprintf( _n( '%s for one year.', '%1$s for each year, for %2$d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
135
                }
136
            } else {
137
                $description = wp_sprintf( _n( '%s for each year.', '%1$s for each %2$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
        } elseif ( $bill_times > 1 && $initial_total != $recurring_total ) {
176
            $bill_times--;
177
        }
178
    }
179
180
    $initial_amount     = wpinv_price( $initial_total, $currency );
181
    $recurring_amount   = wpinv_price( $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( __( '%1$s Then %2$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
/**
197
 * Calculates the card name form a card number.
198
 *
199
 *
200
 * @param string $card_number Card number.
201
 * @return string
202
 */
203
function getpaid_get_card_name( $card_number ) {
204
205
    // Known regexes.
206
    $regexes = array(
207
        '/^4/'                     => __( 'Visa', 'invoicing' ),
208
        '/^5[1-5]/'                => __( 'Mastercard', 'invoicing' ),
209
        '/^3[47]/'                 => __( 'Amex', 'invoicing' ),
210
        '/^3(?:0[0-5]|[68])/'      => __( 'Diners Club', 'invoicing' ),
211
        '/^6(?:011|5)/'            => __( 'Discover', 'invoicing' ),
212
        '/^(?:2131|1800|35\d{3})/' => __( 'JCB', 'invoicing' ),
213
    );
214
215
    // Confirm if one matches.
216
    foreach ( $regexes as $regex => $card ) {
217
        if ( preg_match( $regex, $card_number ) >= 1 ) {
218
            return $card;
219
        }
220
    }
221
222
    // None matched.
223
    return __( 'Card', 'invoicing' );
224
225
}
226
227
/**
228
 * Sends an error response during checkout.
229
 *
230
 * @param WPInv_Invoice|int|null $invoice
231
 */
232
function wpinv_send_back_to_checkout( $invoice = null ) {
233
    $response = array( 'success' => false );
234
    $invoice  = wpinv_get_invoice( $invoice );
235
236
    // Was an invoice created?
237
    if ( ! empty( $invoice ) ) {
238
        $invoice             = is_scalar( $invoice ) ? new WPInv_Invoice( $invoice ) : $invoice;
0 ignored issues
show
introduced by
The condition is_scalar($invoice) is always false.
Loading history...
239
        $response['invoice'] = $invoice->get_id();
240
        do_action( 'getpaid_checkout_invoice_exception', $invoice );
241
    }
242
243
	// Do we have any errors?
244
    if ( wpinv_get_errors() ) {
245
        $response['data'] = getpaid_get_errors_html( true, false );
246
    } else {
247
        $response['data'] = __( 'An error occured while processing your payment. Please try again.', 'invoicing' );
248
    }
249
250
    wp_send_json( $response );
251
}
252
253
/**
254
 * Returns the reCAPTCHA site key.
255
 *
256
 * @return string
257
 */
258
function getpaid_get_recaptcha_site_key() {
259
    return apply_filters( 'getpaid_recaptcha_site_key', wpinv_get_option( 'recaptcha_site_key', '' ) );
260
}
261
262
/**
263
 * Returns the reCAPTCHA secret key.
264
 *
265
 * @return string
266
 */
267
function getpaid_get_recaptcha_secret_key() {
268
    return apply_filters( 'getpaid_recaptcha_secret_key', wpinv_get_option( 'recaptcha_secret_key', '' ) );
269
}
270
271
/**
272
 * Checks if reCAPTCHA is enabled.
273
 *
274
 * @return bool
275
 */
276
function getpaid_is_recaptcha_enabled() {
277
    return wpinv_get_option( 'enable_recaptcha', false ) && getpaid_get_recaptcha_site_key() && getpaid_get_recaptcha_secret_key();
278
}
279
280
/**
281
 * Returns the reCAPTCHA version.
282
 *
283
 * @return string
284
 */
285
function getpaid_get_recaptcha_version() {
286
    return apply_filters( 'getpaid_recaptcha_version', wpinv_get_option( 'recaptcha_version', 'v2' ) );
287
}
288