1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Traits\PayPalAPI; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
trait Subscriptions |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Create a new subscription. |
11
|
|
|
* |
12
|
|
|
* @param array $data |
13
|
|
|
* |
14
|
|
|
* @throws \Throwable |
15
|
|
|
* |
16
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
17
|
|
|
* |
18
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create |
19
|
|
|
*/ |
20
|
|
|
public function createSubscription(array $data) |
21
|
|
|
{ |
22
|
|
|
$this->apiEndPoint = 'v1/billing/subscriptions'; |
|
|
|
|
23
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$this->options['json'] = $data; |
|
|
|
|
26
|
|
|
|
27
|
|
|
$this->verb = 'post'; |
|
|
|
|
28
|
|
|
|
29
|
|
|
return $this->doPayPalRequest(); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Update an existing billing plan. |
34
|
|
|
* |
35
|
|
|
* @param string $subscription_id |
36
|
|
|
* @param array $data |
37
|
|
|
* |
38
|
|
|
* @throws \Throwable |
39
|
|
|
* |
40
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
41
|
|
|
* |
42
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_patch |
43
|
|
|
*/ |
44
|
|
|
public function updateSubscription($subscription_id, array $data) |
45
|
|
|
{ |
46
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}"; |
|
|
|
|
47
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->options['json'] = $data; |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->verb = 'patch'; |
|
|
|
|
52
|
|
|
|
53
|
|
|
return $this->doPayPalRequest(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Show details for an existing subscription. |
58
|
|
|
* |
59
|
|
|
* @param string $subscription_id |
60
|
|
|
* |
61
|
|
|
* @throws \Throwable |
62
|
|
|
* |
63
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
64
|
|
|
* |
65
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get |
66
|
|
|
*/ |
67
|
|
|
public function showSubscriptionDetails($subscription_id) |
68
|
|
|
{ |
69
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}"; |
|
|
|
|
70
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->verb = 'get'; |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $this->doPayPalRequest(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Activate an existing subscription. |
79
|
|
|
* |
80
|
|
|
* @param string $subscription_id |
81
|
|
|
* |
82
|
|
|
* @throws \Throwable |
83
|
|
|
* |
84
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
85
|
|
|
* |
86
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_activate |
87
|
|
|
*/ |
88
|
|
|
public function activateSubscription($subscription_id) |
89
|
|
|
{ |
90
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}/activate"; |
|
|
|
|
91
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
92
|
|
|
|
93
|
|
|
$this->verb = 'post'; |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $this->doPayPalRequest(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Cancel an existing subscription. |
100
|
|
|
* |
101
|
|
|
* @param string $subscription_id |
102
|
|
|
* |
103
|
|
|
* @throws \Throwable |
104
|
|
|
* |
105
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
106
|
|
|
* |
107
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel |
108
|
|
|
*/ |
109
|
|
|
public function cancelSubscription($subscription_id) |
110
|
|
|
{ |
111
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}/cancel"; |
|
|
|
|
112
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
113
|
|
|
|
114
|
|
|
$this->verb = 'post'; |
|
|
|
|
115
|
|
|
|
116
|
|
|
return $this->doPayPalRequest(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Suspend an existing subscription. |
121
|
|
|
* |
122
|
|
|
* @param string $subscription_id |
123
|
|
|
* |
124
|
|
|
* @throws \Throwable |
125
|
|
|
* |
126
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
127
|
|
|
* |
128
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_suspend |
129
|
|
|
*/ |
130
|
|
|
public function suspendSubscription($subscription_id) |
131
|
|
|
{ |
132
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}/suspend"; |
|
|
|
|
133
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
134
|
|
|
|
135
|
|
|
$this->verb = 'post'; |
|
|
|
|
136
|
|
|
|
137
|
|
|
return $this->doPayPalRequest(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Capture payment for an existing subscription. |
142
|
|
|
* |
143
|
|
|
* @param string $subscription_id |
144
|
|
|
* @param string $note |
145
|
|
|
* @param float $amount |
146
|
|
|
* |
147
|
|
|
* @throws \Throwable |
148
|
|
|
* |
149
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
150
|
|
|
* |
151
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_capture |
152
|
|
|
*/ |
153
|
|
|
public function captureSubscriptionPayment($subscription_id, $note, $amount) |
154
|
|
|
{ |
155
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}/capture"; |
|
|
|
|
156
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
157
|
|
|
|
158
|
|
|
$this->options['json'] = [ |
|
|
|
|
159
|
|
|
"note" => $note, |
160
|
|
|
"capture_type" => "OUTSTANDING_BALANCE", |
161
|
|
|
"amount" => [ |
162
|
|
|
"currency" => $this->currency, |
163
|
|
|
"value" => "{$amount}", |
164
|
|
|
], |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
$this->verb = 'post'; |
|
|
|
|
168
|
|
|
|
169
|
|
|
return $this->doPayPalRequest(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Revise quantity, product or service for an existing subscription. |
174
|
|
|
* |
175
|
|
|
* @param string $subscription_id |
176
|
|
|
* @param array $items |
177
|
|
|
* |
178
|
|
|
* @throws \Throwable |
179
|
|
|
* |
180
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
181
|
|
|
* |
182
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_revise |
183
|
|
|
*/ |
184
|
|
|
public function reviseSubscription($subscription_id, array $items) |
185
|
|
|
{ |
186
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}/revise"; |
|
|
|
|
187
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
188
|
|
|
|
189
|
|
|
$this->options['json'] = $items; |
|
|
|
|
190
|
|
|
|
191
|
|
|
$this->verb = 'post'; |
|
|
|
|
192
|
|
|
|
193
|
|
|
return $this->doPayPalRequest(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* List transactions for an existing subscription. |
198
|
|
|
* |
199
|
|
|
* @param string $subscription_id |
200
|
|
|
* @param string $start_date |
201
|
|
|
* @param string $end_date |
202
|
|
|
* |
203
|
|
|
* @throws \Throwable |
204
|
|
|
* |
205
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
206
|
|
|
* |
207
|
|
|
* @see https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions |
208
|
|
|
*/ |
209
|
|
|
public function listSubscriptionTransactions($subscription_id, $start_date = "", $end_date = "") |
210
|
|
|
{ |
211
|
|
|
$start_date = empty($start_date) ? |
212
|
|
|
Carbon::now()->subDay()->toIso8601String() : |
213
|
|
|
Carbon::parse($start_date)->toIso8601String(); |
214
|
|
|
|
215
|
|
|
$end_date = empty($end_date) ? |
216
|
|
|
Carbon::now()->toIso8601String() : |
217
|
|
|
Carbon::parse($end_date)->toIso8601String(); |
218
|
|
|
|
219
|
|
|
$this->apiEndPoint = "v1/billing/subscriptions/{$subscription_id}/transactions?start_time={$start_date}&end_time={$end_date}"; |
|
|
|
|
220
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
221
|
|
|
|
222
|
|
|
$this->verb = 'get'; |
|
|
|
|
223
|
|
|
|
224
|
|
|
return $this->doPayPalRequest(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|