|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: Affiliates Api |
| 4 | + * Description: Affiliates Api |
| 5 | + * Version: 0.0.1 |
| 6 | + * Author: Dmitrii Poddubnyi |
| 7 | + * Author URI: https://karser.dev |
| 8 | + */ |
| 9 | + |
| 10 | +// If this file is called directly, abort. |
| 11 | +if ( ! defined( 'WPINC' ) ) { |
| 12 | + die; |
| 13 | +} |
| 14 | + |
| 15 | +if ( ! defined( 'AFFILIATE_WP_API_KEY' ) ) { |
| 16 | + die; |
| 17 | +} |
| 18 | + |
| 19 | +add_action('rest_api_init', function () { |
| 20 | + register_rest_route('affiliates-api', '/referrals', [ |
| 21 | + 'methods' => 'POST', |
| 22 | + 'callback' => 'affiliates_api_add_referrals', |
| 23 | + 'permission_callback' => '__return_true', |
| 24 | + 'args' => [ |
| 25 | + 'email' => [ |
| 26 | + 'required' => true, |
| 27 | + 'type' => 'string', |
| 28 | + 'format' => 'email', |
| 29 | + ], |
| 30 | + 'date' => [ |
| 31 | + 'required' => true, |
| 32 | + 'type' => 'string', |
| 33 | + 'format' => 'date-time', |
| 34 | + ], |
| 35 | + 'baseAmount' => [ |
| 36 | + 'required' => true, |
| 37 | + 'type' => 'string', |
| 38 | + ], |
| 39 | + 'currency' => [ |
| 40 | + 'required' => true, |
| 41 | + 'type' => 'string', |
| 42 | + ], |
| 43 | + 'refId' => [ |
| 44 | + 'required' => true, |
| 45 | + 'type' => 'string', |
| 46 | + ], |
| 47 | + 'hit' => [ |
| 48 | + 'required' => true, |
| 49 | + 'type' => 'string', |
| 50 | + ], |
| 51 | + 'reference' => [ |
| 52 | + 'required' => true, |
| 53 | + 'type' => 'string', |
| 54 | + ], |
| 55 | + 'ip' => [ |
| 56 | + 'required' => true, |
| 57 | + 'type' => 'string', |
| 58 | + 'format' => 'ip', |
| 59 | + ], |
| 60 | + ] |
| 61 | + ]); |
| 62 | +}); |
| 63 | + |
| 64 | +function affiliates_api_add_referrals(WP_REST_Request $request) |
| 65 | +{ |
| 66 | + $data = $request->get_json_params(); |
| 67 | + |
| 68 | + if (null === ($apiKey = $request->get_header('X-Api-Key')) |
| 69 | + || $apiKey !== AFFILIATE_WP_API_KEY |
| 70 | + ) { |
| 71 | + return new WP_Error( 'access_denied', 'Api key is wrong', ['status' => 400]); |
| 72 | + } |
| 73 | + |
| 74 | + $hitId = affiliates_api_get_hit_id($data['hit'], $data['refId']) ; |
| 75 | + |
| 76 | + if (null === $hitId) { |
| 77 | + return new WP_Error( 'wrong_hash', 'Hash or referrer is wrong', ['status' => 400]); |
| 78 | + } |
| 79 | + |
| 80 | + $r = new Affiliates_Referral_WordPress(); |
| 81 | + $affiliate_ids = [$data['refId']]; |
| 82 | + $orderId = 0; // related post ID, order ID |
| 83 | + $description = $data['email']; |
| 84 | + |
| 85 | + $payload = [ |
| 86 | + 'date' => [ |
| 87 | + 'title' => 'Date', |
| 88 | + 'domain' => 'affiliates', |
| 89 | + 'value' => esc_sql( $data['date'] ) |
| 90 | + ], |
| 91 | + 'email' => [ |
| 92 | + 'title' => 'Email', |
| 93 | + 'domain' => 'affiliates', |
| 94 | + 'value' => esc_sql( $data['email'] ) |
| 95 | + ], |
| 96 | + 'ip' => [ |
| 97 | + 'title' => 'IP', |
| 98 | + 'domain' => 'affiliates', |
| 99 | + 'value' => esc_sql( $data['ip'] ) |
| 100 | + ], |
| 101 | + 'baseAmount' => [ |
| 102 | + 'title' => 'Base Amount', |
| 103 | + 'domain' => 'affiliates', |
| 104 | + 'value' => esc_sql( $data['baseAmount'] ) |
| 105 | + ], |
| 106 | + ]; |
| 107 | + |
| 108 | + $base_amount = $data['baseAmount']; |
| 109 | + $amount = null; |
| 110 | + $currency_id = $data['currency']; |
| 111 | + $status = null; |
| 112 | + $type = null; |
| 113 | + $reference = $data['reference']; |
| 114 | + $test = false; |
| 115 | + |
| 116 | + $r->add_referrals($affiliate_ids, $orderId, $description, $payload, $base_amount, $amount, $currency_id, null, null, $reference, $test, $hitId); |
| 117 | +} |
| 118 | + |
| 119 | +function affiliates_api_get_hit_id($hash, $affiliate_id) { |
| 120 | + global $wpdb; |
| 121 | + $hits_table = _affiliates_get_tablename( 'hits' ); |
| 122 | + $query = "SELECT hit_id, hash, affiliate_id FROM {$hits_table} WHERE affiliate_id = %d AND hash = %s"; |
| 123 | + $row = $wpdb->get_row($wpdb->prepare($query, (int) $affiliate_id, $hash)); |
| 124 | + if ($row && !empty($row->hit_id)) { |
| 125 | + return $row->hit_id; |
| 126 | + } |
| 127 | + return null; |
| 128 | +} |
0 commit comments