-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJvzooRestApi.php
129 lines (113 loc) · 4.07 KB
/
JvzooRestApi.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
<?php
/**
* JVZoo Rest Api for PHP
*
* You can use this class to interface with the JVZoo Rest API.
*
* JVZoo API Signup: https://www.jvzoo.com/myaccount/api
* API Documentation: http://api.jvzoo.com
*
* @author Nate Sanden <[email protected]>
*
* @link http://www.natesanden.com
* @copyright Copyright (c) 2015 Nate Sanden
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
namespace nsanden\jvzoo;
class JvzooRestApi {
public $api_key = 'your-api-key-here';
public $account_password = 'your-account-password-here';
const API_URL = 'https://api.jvzoo.com/v2.0';
public function __construct($api_key, $account_password)
{
$this->api_key = $api_key;
$this->account_password = $account_password;
}
public function beginCurl($end_point)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::API_URL . $end_point);
curl_setopt($ch, CURLOPT_USERPWD, $this->api_key . ':' . $this->account_password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return $ch;
}
protected function endCurl($ch)
{
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function getAffiliateStatus($product_id, $affiliate_id)
{
if(trim($product_id) == '')
{
throw new \Exception('$product_id must not be empty.');
}
if(trim($affiliate_id) == '')
{
throw new \Exception('$affiliate_id must not be empty.');
}
$ch = $this->beginCurl('/products/' . $product_id . '/affiliates/' . $affiliate_id);
$response = $this->endCurl($ch);
return $response;
}
public function getTransactionSummary($pay_key)
{
if(trim($pay_key) == '')
{
throw new \Exception('$pay_key must not be empty.');
}
$ch = $this->beginCurl('/transactions/summaries/' . $pay_key);
$response = $this->endCurl($ch);
return $response;
}
public function getRecurringPayment($pre_key)
{
if(trim($pre_key) == '')
{
throw new \Exception('$pre_key must not be empty.');
}
$ch = $this->beginCurl('/recurring_payment/' . $pre_key);
$response = $this->endCurl($ch);
return $response;
}
public function cancelRecurringPayment($pre_key)
{
$response = false;
if(trim($pre_key) != '') {
$ch = $this->beginCurl('/recurring_payment/PA-' . $pre_key);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$post_fields = http_build_query(['status' => 'CANCEL']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$response = $this->strip_tags_content($this->endCurl($ch));
$re = json_decode($response);
if ($re->results->canceled == 'false') {
//try again
$ch = $this->beginCurl('/recurring_payment/SR-' . $pre_key);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$post_fields = http_build_query(['status' => 'CANCEL']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$response = $this->strip_tags_content($this->endCurl($ch));
}
}
return $response;
}
public function strip_tags_content($text, $tags = '', $invert = FALSE) {
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if(is_array($tags) AND count($tags) > 0) {
if($invert == FALSE) {
return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
}
else {
return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
}
}
elseif($invert == FALSE) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
return $text;
}
}