Skip to content

Commit 2a96a2b

Browse files
committed
initial commit
0 parents  commit 2a96a2b

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
.DS_Store
3+
.idea

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Rest API for Itthinx Affiliate WordPress Plugin
2+
3+
## How it works:
4+
5+
### 1. Define the api key in wp-config.php
6+
7+
```
8+
if ( !defined('AFFILIATE_WP_API_KEY') ) {
9+
define('AFFILIATE_WP_API_KEY', 'define-your-api-key-here');
10+
}
11+
```
12+
13+
### 2. Save the hit hash and referrer id from cookie during registration
14+
15+
This may happen when the user submits the registration form so this code checks for the affiliate cookies and sends them back to the server along with the registration data
16+
17+
```
18+
function getRefTrackingId(): string {
19+
const affId = CookieStorage.getItem('wp_affiliates');
20+
const hit = CookieStorage.getItem('_h_affiliates');
21+
if (isDefined(affId) && isDefined(hit)) {
22+
return `${affId}|${hit}`;
23+
}
24+
return null;
25+
}
26+
```
27+
28+
### 3. Submit a purchase back to the plugin
29+
30+
```
31+
POST /wp-json/affiliates-api/referrals HTTP/1.1
32+
Host: your-host.com
33+
Content-Type: application/json
34+
X-Api-Key: your-key
35+
{
36+
"email":"[email protected]",
37+
"baseAmount":"100",
38+
"currency":"USD",
39+
"refId":"5",
40+
"date":"2012-01-01T10:01:01Z",
41+
"hit":"c096876aa6517e2c6d5198efa6a4037532dc1f97959963689c8b6bf3bba17c2f",
42+
"reference":"txn_16A3XQSE2ez0wdde",
43+
"ip":"158.217.93.151"
44+
}
45+
```
46+
47+
## Troubleshooting
48+
49+
### Share cookie between subdomain and domain
50+
if the wordpress and your app are on different subdomains
51+
(e.g your-domain.com and app.your-domain.com)
52+
try adding a leading dot to the cookie settings:
53+
```
54+
define( 'COOKIE_DOMAIN', '.your-domain.com' );
55+
```

affiliates-api.php

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)