-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-freemius-coupon.php
More file actions
144 lines (127 loc) · 3.34 KB
/
Copy pathclass-freemius-coupon.php
File metadata and controls
144 lines (127 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Freemius Coupon helpers
*
* @package Freemius
* @category WordPress_Plugin
* @author Freemius <support@freemius.com>
* @license MIT
* @link https://freemius.com/
*/
namespace Freemius;
/**
* Class Coupon
*
* @package Freemius
* @since 0.5.0
*/
class Coupon {
/**
* Fetch coupon metadata by code.
*
* @since 0.5.0
*
* @param int $product_id Product ID.
* @param string $code Coupon code.
* @return array<string, mixed>|null Coupon data or null when not found.
*/
public static function get_by_code( int $product_id, string $code ): ?array {
if ( '' === $code ) {
return null;
}
$api = Api::get_instance();
$result = $api->get_request(
'products/' . $product_id . '/coupons.json',
array( 'code' => $code )
);
if ( \is_wp_error( $result ) ) {
return null;
}
$data = $result->get_data();
if ( empty( $data['coupons'][0] ) || ! \is_array( $data['coupons'][0] ) ) {
return null;
}
return $data['coupons'][0];
}
/**
* Whether a coupon applies to the given plan.
*
* @since 0.5.0
*
* @param array<string, mixed> $coupon Coupon metadata.
* @param int|string|null $plan_id Plan ID.
* @return bool
*/
public static function applies_to_plan( array $coupon, $plan_id ): bool {
$plan_ids = self::normalize_plan_ids( $coupon['plans'] ?? null );
if ( empty( $plan_ids ) ) {
return true;
}
return \in_array( (string) $plan_id, $plan_ids, true );
}
/**
* Normalize coupon plan restrictions from API payloads.
*
* @since 0.5.0
*
* @param mixed $plans Plan restriction from API.
* @return array<int, string>
*/
private static function normalize_plan_ids( $plans ): array {
if ( null === $plans || '' === $plans ) {
return array();
}
if ( \is_array( $plans ) ) {
return \array_values(
\array_filter(
\array_map(
static function ( $plan_id ) {
return \trim( (string) $plan_id );
},
$plans
)
)
);
}
return \array_values(
\array_filter( \array_map( 'trim', \explode( ',', (string) $plans ) ) )
);
}
/**
* Apply coupon discount to a base price.
*
* @since 0.5.0
*
* @param float $price List price.
* @param array<string, mixed> $coupon Coupon metadata.
* @param string $currency Currency code.
* @return float Discounted price, floored at zero.
*/
public static function apply_discount( float $price, array $coupon, string $currency ): float {
$discounted = $price;
if ( isset( $coupon['discount_type'] ) && 'percentage' === $coupon['discount_type'] ) {
$discounted = $price * ( 1 - ( (float) $coupon['discount'] / 100 ) );
} else {
$currency_key = strtolower( $currency );
$amount = $coupon['discounts'][ $currency_key ] ?? (float) $coupon['discount'];
$discounted = $price - (float) $amount;
}
return max( 0.0, $discounted );
}
/**
* Coupon fields to embed on the frontend.
*
* @since 0.5.0
*
* @param array<string, mixed> $coupon Full coupon payload.
* @return array<string, mixed>
*/
public static function to_embed_data( array $coupon ): array {
return array(
'discount' => $coupon['discount'] ?? 0,
'discount_type' => $coupon['discount_type'] ?? 'percentage',
'plans' => $coupon['plans'] ?? null,
'discounts' => $coupon['discounts'] ?? array(),
);
}
}