-
Notifications
You must be signed in to change notification settings - Fork 3
/
edd-external-purchase-api.php
1584 lines (1256 loc) · 42.6 KB
/
edd-external-purchase-api.php
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Easy Digital Downloads - External Purchase API
Plugin URI: http://easydigitaldownloads.com/extension/external-purchase-api/
Description: Provides an API endpoint for creating sales on third party sites
Version: 0.0.2
Author: Reaktiv Studios
Author URI: http://andrewnorcross.com
Contributors: norcross
*/
if( ! defined( 'EDD_EXAPI_DIR' ) ) {
define( 'EDD_EXAPI_DIR', plugin_dir_path( __FILE__ ) );
}
/**
* EDD_External_API Class
*
* Renders API returns as a JSON/XML array
*
* @since 1.5
*/
class EDD_External_Purchase_API {
/**
* API Version
*/
const VERSION = '0.0.2';
/**
* Pretty Print?
*
* @var bool
* @access private
* @since 1.5
*/
private $pretty_print = false;
/**
* Log API requests?
*
* @var bool
* @access private
* @since 1.5
*/
private $log_requests = true;
/**
* Is this a valid request?
*
* @var bool
* @access private
* @since 1.5
*/
private $is_valid_request = false;
/**
* User ID Performing the API Request
*
* @var int
* @access private
* @since 1.5.1
*/
private $user_id = 0;
/**
* Instance of EDD Stats class
*
* @var object
* @access private
* @since 1.7
*/
private $stats;
/**
* Response data to return
*
* @var array
* @access private
* @since 1.5.2
*/
private $data = array();
/**
*
* @var bool
* @access private
* @since 1.7
*/
private $override = true;
/**
* Setup the EDD API
*
* @author Daniel J Griffiths
* @since 1.5
*/
public function __construct() {
if ( ! function_exists( 'edd_price' ) ) {
return; // EDD not present
}
// include our logging functions
require_once( EDD_EXAPI_DIR . 'lib/edd-external-api-log.php' );
add_action( 'init', array( __CLASS__, 'add_endpoint' ) );
add_action( 'init', array( __CLASS__, 'verify_table' ) );
add_action( 'template_redirect', array( $this, 'process_query' ), 1 );
add_filter( 'query_vars', array( $this, 'query_vars' ) );
add_filter( 'edd_external_whitelist', array( $this, 'whitelist' ) );
}
/**
* our activation sequence
* @return [type] [description]
*/
public static function activate() {
self::add_endpoint();
self::verify_table();
flush_rewrite_rules();
}
/**
* Registers a new rewrite endpoint for accessing the API
*
* @access public
* @author Andrew Norcross
* @since 1.5
*/
public static function add_endpoint() {
add_rewrite_endpoint( 'edd-external-api', EP_ALL );
}
/**
* Looks to see if the logging table exists
* and if not, calls the creation
*
* @access public
* @author Andrew Norcross
* @since 1.5
*/
public static function verify_table() {
// bail if disabled
if ( false === apply_filters( 'edd_external_logging', true ) ) {
return;
}
// call the global
global $wpdb;
// set the name
$name = $wpdb->prefix . 'edd_external_log';
// check for existance of table
if( $wpdb->get_var( "SHOW TABLES LIKE '$name'" ) != $name ) {
// no table, so make it
EDD_External_Purchase_API_Log::create_table();
}
}
/**
* Registers query vars for API access
*
* @access public
* @since 1.5
* @author Daniel J Griffiths
* @param array $vars Query vars
* @return array $vars New query vars
*/
public function query_vars( $vars ) {
// add our new vars
$vars[] = 'token';
$vars[] = 'key';
$vars[] = 'trans_type';
$vars[] = 'payment_id';
$vars[] = 'product_id';
$vars[] = 'price';
$vars[] = 'first_name';
$vars[] = 'last_name';
$vars[] = 'email';
$vars[] = 'date';
$vars[] = 'source_name';
$vars[] = 'source_url';
$vars[] = 'receipt';
// return the vars
return $vars;
}
/**
* Retrieve the user ID based on the public key provided
*
* @access public
* @since 1.5.1
* @global object $wpdb Used to query the database using the WordPress
* Database API
*
* @param string $key Public Key
*
* @return bool if user ID is found, false otherwise
*/
public function get_user( $key = '' ) {
// call the global
global $wpdb;
// check for a key being passed
if ( empty( $key ) ) {
// call the query global
global $wp_query;
// get the key
$key = ! empty( $wp_query->query_vars['key'] ) ? urldecode( $wp_query->query_vars['key'] ) : '';
}
// bail with no key
if ( empty( $key ) ) {
return false;
}
// do the lookup
$user = $wpdb->get_var( $wpdb->prepare( "
SELECT user_id
FROM $wpdb->usermeta
WHERE meta_key = '%s'
LIMIT 1",
$key ) );
// we have something. return it
if ( $user != NULL ) {
$this->user_id = $user;
return $user;
}
// bail if not there
return false;
}
/**
* fetch a meta value for an item with an optional default
*
* @param integer $item_id [description]
* @param string $key [description]
* @param string $default [description]
* @return [type] [description]
*/
public function get_content_meta( $item_id = 0, $key = '', $default = '' ) {
// fetch it
$meta = get_post_meta( $item_id, $key, true );
// return the item
return ! empty ( $meta ) ? $meta : $default;
}
/**
* fetch the product downloads (standard or bundle)
* @param int $product_id product ID in the database
* @return string
*/
public function get_product_files( $product_id ) {
// check item type
$itemtype = $this->get_content_meta( $product_id, '_edd_product_type', 'default' );
// fetch download files for single items
if ( $itemtype == 'default' ) {
// set the data
$data[] = array(
'id' => absint( $product_id ),
'file' => edd_get_download_files( $product_id ),
'name' => $this->get_product_name( $product_id ),
);
// return the data
return $data;
}
// get the bundles
$bundles = $this->get_content_meta( $product_id, '_edd_bundled_products' );
// nothing? bail
if ( empty( $bundles ) ) {
return false;
}
// loop it
foreach ( $bundles as $bundle_id ) {
$data[] = array(
'id' => absint( $bundle_id ),
'file' => edd_get_download_files( $bundle_id ),
'name' => $this->get_product_name( $bundle_id ),
);
}
// return the data
return $data;
}
/**
* fetch the custom product name with standard fallback
* @param int $product_id product ID in the database
* @return string
*/
public function get_product_name( $product_id ) {
// fetch the custom
$custom = $this->get_content_meta( $product_id, '_edd_external_title' );
// if we have a custom, return that
if ( ! empty( $custom ) ) {
return esc_html( $custom );
}
// fetch the base one
$title = get_the_title( $product_id );
// check for and return
return ! empty( $title ) ? esc_html( $title ) : 'EDD Product';
}
/**
* fetch the product license key (if one exists)
* @param int $payment_id payment ID in the database
* @return string
*/
public function get_product_license( $payment_id ) {
// fetch payment data based on ID with license
$args = array(
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_edd_sl_payment_id',
'meta_value' => $payment_id,
'post_type' => 'edd_license',
'post_status' => 'any'
);
// fetch the licenses
$licenses = get_posts( $args );
// bail with none
if ( empty( $licenses ) ) {
return false;
}
// set an empty default
$license_keys = array();
// fetch license keys and make sure they are in an array to match the download items
foreach ( $licenses as $license_id ) {
$license_keys[] = get_post_meta( $license_id, '_edd_sl_key', true );
}
// send back key(s)
return ! empty( $license_keys ) ? $license_keys : false;
}
/**
* fetch the download URL
* @param string $url
* @return bool
*/
public function get_product_download_url( $payment_id, $product_id ) {
// fetch my key and email
$payment_key = $this->get_content_meta( $payment_id, '_edd_payment_purchase_key' );
$payment_email = $this->get_content_meta( $payment_id, '_edd_payment_user_email' );
$params = array(
'download_key' => $payment_key,
'email' => rawurlencode( $payment_email ),
'file' => 0,
'price_id' => 0,
'download_id' => $product_id,
'expire' => rawurlencode( base64_encode( 2147472000 ) )
);
// add my args
$download_url = add_query_arg( $params, home_url( 'index.php' ) );
// return the download URL
return $download_url;
}
/**
* [fetch_download_urls description]
* @return [type] [description]
*/
public function fetch_download_data( $payment_id, $product_id ) {
$downloads = $this->get_product_files( $product_id );
$licenses = $this->get_product_license( $payment_id );
// set the data return to an array
$download_data = array();
// loop them into a key / value
foreach ( $downloads as $filekey => $file ) {
$download_url = $this->get_product_download_url( $payment_id, $file['id'] );
// match up licenses to the applicable download item
$download_license = isset( $licenses[ $filekey ] ) ? $licenses[ $filekey ] : '';
$download_data[] = array(
'product_id' => absint( $file['id'] ),
'product_name' => esc_attr( $file['name'] ),
'download_link' => $download_url,
'license_key' => $download_license
);
}
// return the data
return $download_data;
}
/**
* [fetch_purchase_data description]
* @param [type] $payment_id [description]
* @return [type] [description]
*/
public function fetch_purchase_data( $payment_id ) {
// fetch my purchase key and total
$purchase_key = $this->get_content_meta( $payment_id, '_edd_payment_purchase_key' );
$purchase_total = $this->get_content_meta( $payment_id, '_edd_payment_total' );
// get and convert the purchase date
$purchase_date = get_post_field( 'post_date', $payment_id, 'raw' );
$purchase_stamp = strtotime( $purchase_date );
// fetch any external meta
$external_meta = $this->get_content_meta( $payment_id, '_edd_external_purchase_meta' );
// get my source name and URL
$source_name = ! empty( $external_meta['source_name'] ) ? esc_html( $external_meta['source_name'] ) : '';
$source_url = ! empty( $external_meta['source_url'] ) ? esc_url( $external_meta['source_url'] ) : '';
// return the data array
return array(
'external_source' => $source_name,
'external_url' => $source_url,
'purchase_key' => $purchase_key,
'purchase_total' => $purchase_total,
'purchase_date' => $purchase_date,
'purchase_stamp' => $purchase_stamp
);
}
/**
* confirm the ID being passed is actually a live item and not something else
* @param int $item_id content ID in the database
* @return bool
*/
public function confirm_id_exists( $item_id = 0, $type = 'download' ) {
// fetch the item
$item = get_post( $item_id );
// bail if nothing
if ( ! $item || ! is_object( $item ) ) {
return false;
}
// bail if not a download
if ( $item->post_type != $type ) {
return false;
}
// bail if not published
if ( $item->post_status != 'publish' ) {
return false;
}
// return true
return true;
}
/**
* confirm the source URL is whitelisted
* @param string $url
* @return bool
*/
public function confirm_source_url( $source_url ) {
// first run bypass filter for those LIVIN ON THE EDGE
if ( false === $disable = apply_filters( 'edd_external_whitelist_enabled', true ) ) {
return true;
}
// grab the source
$source = $this->strip_url( $source_url );
// check the whitelist
$whitelist = apply_filters( 'edd_external_whitelist', array() );
$whitelist = ! is_array( $whitelist ) ? array( $whitelist ) : $whitelist;
// check for a whitelist
if ( ! $whitelist || empty( $whitelist ) ) {
return false;
}
// loop through the URLs and strip them
foreach ( $whitelist as $url ) {
$list[] = $this->strip_url( $url );
}
// check said whitelist and return if you're on the list
return ! in_array( $source, $list ) ? false : true;
}
/**
* strip the URL down to the host
*
* @param string $url
* @return string
*/
public function strip_url( $url ) {
// check for the http or https and add it if it's missing
if ( ! preg_match( '~^(?:f|ht)tps?://~i', $url ) ) {
$url = 'http://' . $url;
}
// clean up the damn link
$parsed = parse_url( $url );
$host = $parsed['host'];
$parts = explode( '.', $host );
// Give us only the last two parts of the host (domain and TLD)
$domain = join( '.', array_slice( $parts, -2 ) );
// return the domain
return $domain;
}
/**
* run various checks on the purchase request
* @param array $wp_query API query being passed
* @return bool
*/
public function validate_request( $wp_query, $log_id = 0 ) {
// check for missing transaction type
if ( ! isset( $wp_query->query_vars['trans_type'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'TRANS_TYPE_MISSING',
'message' => 'No transaction type has been supplied.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, 'unknown', 0, 0, $response['error_code'] );
// send the API response
return false;
}
// set our transaction type to use in logging going forward
$type = esc_attr( $wp_query->query_vars['trans_type'] );
// Bail if we're not serving over SSL
if ( apply_filters( 'edd_external_require_ssl', true ) && ! is_ssl() ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NO_SSL',
'message' => 'The API is only available over HTTPS.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for both missing key AND token
if ( ! isset( $wp_query->query_vars['key'] ) && ! isset( $wp_query->query_vars['token'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'KEY_TOKEN_MISSING',
'message' => 'The required API key and token were not provided.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for missing key
if ( ! isset( $wp_query->query_vars['key'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'KEY_MISSING',
'message' => 'The required API key was not provided.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for missing token
if ( ! isset( $wp_query->query_vars['token'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'TOKEN_MISSING',
'message' => 'The required token was not provided.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for missing source URL
if ( ! isset( $wp_query->query_vars['source_url'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'SOURCE_URL_MISSING',
'message' => 'A source URL is required.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for source URL on whitelist
if ( false === $whitelist = $this->confirm_source_url( $wp_query->query_vars['source_url'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'SOURCE_URL_WHITELIST',
'message' => 'Your site has not been approved for external purchases. Please contact the store owner.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// fetch the user ID from the API key passed
$apiuser = $this->get_user( $wp_query->query_vars['key'] );
// check if user being passed has purchase access
if ( empty( $apiuser ) || ! user_can( $apiuser, 'edit_shop_payments' ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NO_PAYMENT_ACCESS',
'message' => 'The API user does not have permission to create payments.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// checks that are tied to purchases
if ( $type == 'purchase' ) {
// check for missing product ID
if ( ! isset( $wp_query->query_vars['product_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NO_PRODUCT_ID',
'message' => 'No product ID was provided.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for invalid product ID
if ( ! is_numeric( $wp_query->query_vars['product_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'INVALID_PRODUCT_ID',
'message' => 'The provided product ID must be numeric.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check if the product ID is an actual product
if ( false === $product_check = $this->confirm_id_exists( $wp_query->query_vars['product_id'], 'download' ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NOT_VALID_PRODUCT',
'message' => 'The provided ID was not a valid product.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
} // end purchase checks
// run checks related to refunds
if ( $type == 'refund' ) {
// check for missing payment ID
if ( ! isset( $wp_query->query_vars['payment_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NO_PAYMENT_ID',
'message' => 'No payment ID was not provided.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for invalid payment ID
if ( ! is_numeric( $wp_query->query_vars['payment_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'INVALID_PAYMENT_ID',
'message' => 'The provided payment ID must be numeric.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check if the payment ID is an actual payment
if ( false === $payment_check = $this->confirm_id_exists( $wp_query->query_vars['payment_id'], 'edd_payment' ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NOT_VALID_PAYMENT',
'message' => 'The provided ID was not a valid payment ID.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
} // end refund checks
// run checks related to details
if ( $wp_query->query_vars['trans_type'] == 'details' ) {
// check for missing product ID
if ( ! isset( $wp_query->query_vars['product_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NO_PRODUCT_ID',
'message' => 'No product ID was provided.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for missing payment ID
if ( ! isset( $wp_query->query_vars['payment_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NO_PAYMENT_ID',
'message' => 'No payment ID was not provided.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for invalid product ID
if ( ! is_numeric( $wp_query->query_vars['product_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'INVALID_PRODUCT_ID',
'message' => 'The provided product ID must be numeric.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check if the product ID is an actual product
if ( false === $product_check = $this->confirm_id_exists( $wp_query->query_vars['product_id'], 'download' ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NOT_VALID_PRODUCT',
'message' => 'The provided ID was not a valid product.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check for invalid payment ID
if ( ! is_numeric( $wp_query->query_vars['payment_id'] ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'INVALID_PAYMENT_ID',
'message' => 'The provided payment ID must be numeric.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
// check if the payment ID is an actual payment
if ( false === $payment_check = $this->confirm_id_exists( $wp_query->query_vars['payment_id'], 'edd_payment' ) ) {
// set the response array
$response = array(
'success' => false,
'error_code' => 'NOT_VALID_PAYMENT',
'message' => 'The provided ID was not a valid payment ID.'
);
// update the log entry
EDD_External_Purchase_API_Log::update_log_entry( $log_id, $type, 0, 0, $response['error_code'] );
// send the API response
$this->output( $response );
// and bail
return false;
}
} // end details check
// all checks passed
return true;
}
/**
* Listens for the API and then processes the API requests
*
* @access public
* @author Daniel J Griffiths
* @global $wp_query
* @since 1.5
* @return void
*/
public function process_query() {
// call the global
global $wp_query;
// Check for edd-external-purchase var. Get out if not present
if ( ! isset( $wp_query->query_vars['edd-external-api'] ) ) {
return;
}
// run our initial log
$log_id = EDD_External_Purchase_API_Log::create_log_entry( 'request', $wp_query->query );
// run my validation checks and if validation failed, just return
if ( false === $validate = $this->validate_request( $wp_query, $log_id ) ) {
return;
}
// set process to false
$process = false;