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