-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAdCredits.php
More file actions
278 lines (235 loc) · 8.12 KB
/
AdCredits.php
File metadata and controls
278 lines (235 loc) · 8.12 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* Pinterest for WooCommerce Ads Credits.
*
* @package Pinterest_For_WooCommerce/Classes/
* @version 1.0.10
*/
namespace Automattic\WooCommerce\Pinterest;
use Automattic\WooCommerce\Pinterest\API\APIV5;
use Automattic\WooCommerce\Pinterest\PinterestApiException;
use Exception;
use Pinterest_For_Woocommerce;
use Pinterest_For_Woocommerce_Ads_Supported_Countries;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Handling ad credits.
*/
class AdCredits {
const ADS_CREDIT_CAMPAIGN_TRANSIENT = PINTEREST_FOR_WOOCOMMERCE_PREFIX . '-ads-credit-campaign-transient';
const ADS_CREDIT_CAMPAIGN_OPTION = 'ads_campaign_is_active';
private const FUTURE_CREDIT = 'FUTURE_CREDIT';
private const MARKETING_OFFER_CREDIT = 'MARKETING_OFFER_CREDIT';
/**
* Initialize Ad Credits actions and Action Scheduler hooks.
*
* @since 1.2.5
*/
public static function schedule_event() {
if ( ! Pinterest_For_Woocommerce::is_connected() ) {
return;
}
add_action( Heartbeat::HOURLY, array( static::class, 'handle_redeem_credit' ), 20 );
}
/**
* Check if the advertiser has set the billing data.
*
* @since 1.2.5
*
* @return mixed
*/
public static function handle_redeem_credit() {
if ( ! Pinterest_For_Woocommerce::is_connected() ) {
// Advertiser not connected redeem operation makes no sense.
return true;
}
Pinterest_For_Woocommerce()::add_available_credits_info_to_account_data();
if ( ! Pinterest_For_Woocommerce()::get_billing_setup_info_from_account_data() ) {
// Do not redeem credits if the billing is not setup.
return true;
}
if ( self::check_if_coupon_was_redeemed() ) {
// Redeem credits only once.
return true;
}
if ( ! self::check_if_ads_campaign_is_active() ) {
return true;
}
Pinterest_For_Woocommerce()::add_redeem_credits_info_to_account_data();
return true;
}
/**
* Check if coupon was redeemed. We can redeem only once.
*
* @since 1.2.5
* @since 1.4.1 Update for API v5. and moved to AdCredits class.
*
* @return bool
*/
public static function check_if_coupon_was_redeemed() {
$account_data = Pinterest_For_Woocommerce()::get_setting( 'account_data' );
$redeem_status = $account_data['coupon_redeem_info']['redeem_status'] ?? false;
$error_id = $account_data['coupon_redeem_info']['error_id'] ?? false;
if ( in_array( $error_id, PinterestApiException::TERMINAL_REDEEM_ERRORS, true ) ) {
// A prior attempt hit a terminal error from Pinterest (e.g., the
// offer has already been redeemed). Do not retry.
return true;
}
if ( false === $redeem_status ) {
return false;
}
return true;
}
/**
* Redeem Ad Credits.
*
* @since 1.2.5
*
* @param string $offer_code Coupon string.
* @param integer $error_code Reference parameter for error number.
* @param string $error_message Reference parameter for error message.
*
* @return bool Weather the coupon was successfully redeemed or not.
*/
public static function redeem_credits( $offer_code, &$error_code = null, &$error_message = null ) {
if ( ! Pinterest_For_Woocommerce::is_connected() ) {
// Advertiser not connected, we can't check if credits were redeemed.
return false;
}
$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
try {
$offer_code_credits_data = APIV5::redeem_ads_offer_code( $ad_account_id, $offer_code );
if ( ! $offer_code_credits_data['success'] ) {
$error_code = $offer_code_credits_data['errorCode'];
$error_message = $offer_code_credits_data['errorMessage'];
Logger::log( "{$error_code}: {$error_message}", 'error' );
return false;
}
return true;
} catch ( PinterestApiException $e ) {
$error_code = $e->get_pinterest_code();
$error_message = $e->getMessage();
if ( PinterestApiException::NO_VALID_BILLING_SETUP === $error_code ) {
// Invalidate billing setup.
Billing::add_billing_setup_status_to_account_data( false );
}
} catch ( Throwable $th ) {
Logger::log( $th->getMessage(), 'error' );
}
return false;
}
/**
* Check if the ads campaign is active. In order for that to happen the
* following conditions need to be met:
* 1. Merchant needs to be in ads supported country.
* 2. Merchant needs to have coupon available for his chosen currency.
* 3. Ads Campaign needs to be globally active.
*
* @since 1.2.5
*
* @return bool Wether campaign is active or not.
*/
public static function check_if_ads_campaign_is_active() {
$is_campaign_active = get_transient( self::ADS_CREDIT_CAMPAIGN_TRANSIENT );
// If transient is available then it means that we have already checked.
if ( false !== $is_campaign_active ) {
return wc_string_to_bool( $is_campaign_active );
}
$request_error = false;
try {
// Check if all conditions are met.
if (
Pinterest_For_Woocommerce_Ads_Supported_Countries::is_ads_supported_country() &&
AdCreditsCoupons::has_valid_coupon_for_merchant() &&
self::get_is_campaign_active_from_recommendations()
) {
$is_campaign_active = true;
}
} catch ( Exception $ex ) {
$request_error = true;
}
Pinterest_For_Woocommerce()->save_setting( self::ADS_CREDIT_CAMPAIGN_OPTION, $is_campaign_active );
/*
* Try again in fifteen minutes in case we had problems fetching
* the campaign status from the server, wait full day otherwise.
*/
set_transient(
self::ADS_CREDIT_CAMPAIGN_TRANSIENT,
wc_bool_to_string( $is_campaign_active ),
$request_error ? 15 * MINUTE_IN_SECONDS : DAY_IN_SECONDS
);
return $is_campaign_active;
}
/**
* Check if campaign is enabled in the recommendations API from WooCommerce.com.
*
* @since 1.2.5
*
* @throws Exception API fetch error.
*
* @return bool Wether the campaign is active or not.
*/
private static function get_is_campaign_active_from_recommendations() {
$request = wp_remote_get( 'https://woocommerce.com/wp-json/wccom/marketing-tab/1.2/recommendations.json' );
$recommendations = array();
if ( is_wp_error( $request ) ) {
throw new Exception(
sprintf(
/* translators: API error message */
__( 'Could not fetch ads campaign status due to: %s', 'pinterest-for-woocommerce' ),
$request->get_error_message()
)
);
}
if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) {
$recommendations = json_decode( $request['body'], true );
}
// Find Pinterest plugin entry and check for promotions key.
foreach ( $recommendations as $recommendation ) {
if ( 'pinterest-for-woocommerce' === $recommendation['product'] ) {
return array_key_exists( 'show_extension_promotions', $recommendation ) ? $recommendation['show_extension_promotions'] : false;
}
}
return false;
}
/**
* Fetch data from the discount endpoint and get the necessary fields.
*
* @since 1.2.5
* @since 1.4.1
*
* @throws PinterestApiException Originating from get_coupon_for_merchant.
* @return mixed False when no info is available, discounts object when discounts are available.
*/
public static function process_available_discounts() {
if ( ! Pinterest_For_Woocommerce::is_connected() ) {
// Advertiser not connected, we can't check if credits were redeemed.
return false;
}
$advertiser_id = Pinterest_For_Woocommerce()::get_setting( 'tracking_advertiser' );
$discounts = APIV5::get_ads_credit_discounts( $advertiser_id )['items'] ?? array();
$coupon = AdCreditsCoupons::get_coupon_for_merchant();
$remaining_discount = 0;
$found_discounts = array();
foreach ( $discounts as $discount ) {
if ( ! $discount['active'] ) {
continue;
}
if ( static::FUTURE_CREDIT === $discount['discount_type'] ) {
$offer_code = $discount['discount_restrictions']['marketing_offer_code_hash'] ?? '';
if ( $offer_code === $coupon ) {
$found_discounts['future_discount'] = true;
}
} elseif ( static::MARKETING_OFFER_CREDIT === $discount['discount_type'] ) {
$remaining_discount += (float) $discount['remaining_discount_in_micro_currency'] / 1000000;
}
}
$found_discounts['marketing_offer'] = array(
'remaining_discount' => wc_price( $remaining_discount ),
);
return $found_discounts;
}
}