1: <?php
2:
3: /**
4: * Rebill a customer
5: * @see https://app.syspay.com/bundles/emiuser/doc/merchant_api.html#issue-a-rebill-on-a-billing-agreement
6: */
7: class Syspay_Merchant_RebillRequest extends Syspay_Merchant_Request
8: {
9: const METHOD = 'POST';
10: const PATH = '/api/v1/merchant/billing-agreement/%d/rebill';
11:
12: /**
13: * @var string
14: */
15: private $threatMetrixSessionId;
16:
17: /**
18: * @var string
19: */
20: private $emsUrl;
21:
22: /**
23: * @var integer
24: */
25: private $billingAgreementId;
26:
27: /**
28: * @var string
29: */
30: private $reference;
31:
32: /**
33: * @var integer
34: */
35: private $amount;
36:
37: /**
38: * @var string
39: */
40: private $currency;
41:
42: /**
43: * @var string
44: */
45: private $description;
46:
47: /**
48: * @var string
49: */
50: private $extra;
51:
52: /**
53: * @var array
54: */
55: private $recipient_map;
56:
57: public function __construct($billingAgreementId = null)
58: {
59: if (null !== $billingAgreementId) {
60: $this->setBillingAgreementId($billingAgreementId);
61: }
62: }
63:
64: /**
65: * {@inheritDoc}
66: */
67: public function getMethod()
68: {
69: return self::METHOD;
70: }
71:
72: /**
73: * {@inheritDoc}
74: */
75: public function getPath()
76: {
77: return sprintf(self::PATH, $this->billingAgreementId);
78: }
79:
80: /**
81: * {@inheritDoc}
82: */
83: public function buildResponse(stdClass $response)
84: {
85: if (!isset($response->payment)) {
86: throw new Syspay_Merchant_UnexpectedResponseException(
87: 'Unable to retrieve "payment" data from response',
88: $response
89: );
90: }
91:
92: $payment = Syspay_Merchant_Entity_Payment::buildFromResponse($response->payment);
93:
94: return $payment;
95: }
96:
97: /**
98: * {@inheritDoc}
99: */
100: public function getData()
101: {
102: $data = array();
103:
104: if (false == empty($this->threatMetrixSessionId)) {
105: $data['threatmetrix_session_id'] = $this->threatMetrixSessionId;
106: }
107:
108: if (false === empty($this->emsUrl)) {
109: $data['ems_url'] = $this->emsUrl;
110: }
111:
112: $data['payment'] = array();
113:
114: if (false === empty($this->reference)) {
115: $data['payment']['reference'] = $this->reference;
116: }
117:
118: if (false === empty($this->amount)) {
119: $data['payment']['amount'] = $this->amount;
120: }
121:
122: if (false === empty($this->currency)) {
123: $data['payment']['currency'] = $this->currency;
124: }
125:
126: if (false === empty($this->description)) {
127: $data['payment']['description'] = $this->description;
128: }
129:
130: if (false === empty($this->extra)) {
131: $data['payment']['extra'] = $this->extra;
132: }
133:
134: if (false === empty($this->recipient_map)
135: && is_array($this->recipient_map)) {
136: $data['payment']['recipient_map'] = array();
137: foreach ($this->recipient_map as $r) {
138: $data['payment']['recipient_map'][] = $r->toArray();
139: }
140: }
141:
142: return $data;
143: }
144:
145: /**
146: * Gets the value of threatMetrixSessionId.
147: *
148: * @return string
149: */
150: public function getThreatMetrixSessionId()
151: {
152: return $this->threatMetrixSessionId;
153: }
154:
155: /**
156: * Sets the value of threatMetrixSessionId.
157: *
158: * @param string $threatMetrixSessionId the threatMetrixSessionId
159: *
160: * @return self
161: */
162: public function setThreatMetrixSessionId($threatMetrixSessionId)
163: {
164: $this->threatMetrixSessionId = $threatMetrixSessionId;
165:
166: return $this;
167: }
168:
169: /**
170: * Gets the value of emsUrl.
171: *
172: * @return string
173: */
174: public function getEmsUrl()
175: {
176: return $this->emsUrl;
177: }
178:
179: /**
180: * Sets the value of emsUrl.
181: *
182: * @param string $emsUrl the emsUrl
183: *
184: * @return self
185: */
186: public function setEmsUrl($emsUrl)
187: {
188: $this->emsUrl = $emsUrl;
189:
190: return $this;
191: }
192:
193: /**
194: * Gets the value of billingAgreementId.
195: *
196: * @return integer
197: */
198: public function getBillingAgreementId()
199: {
200: return $this->billingAgreementId;
201: }
202:
203: /**
204: * Sets the value of billingAgreementId.
205: *
206: * @param integer $billingAgreementId the billingAgreementId
207: *
208: * @return self
209: */
210: public function setBillingAgreementId($billingAgreementId)
211: {
212: $this->billingAgreementId = $billingAgreementId;
213:
214: return $this;
215: }
216:
217: /**
218: * Gets the value of reference.
219: *
220: * @return string
221: */
222: public function getReference()
223: {
224: return $this->reference;
225: }
226:
227: /**
228: * Sets the value of reference.
229: *
230: * @param string $reference the reference
231: *
232: * @return self
233: */
234: public function setReference($reference)
235: {
236: $this->reference = $reference;
237:
238: return $this;
239: }
240:
241: /**
242: * Gets the value of amount.
243: *
244: * @return integer
245: */
246: public function getAmount()
247: {
248: return $this->amount;
249: }
250:
251: /**
252: * Sets the value of amount.
253: *
254: * @param integer $amount the amount
255: *
256: * @return self
257: */
258: public function setAmount($amount)
259: {
260: $this->amount = $amount;
261:
262: return $this;
263: }
264:
265: /**
266: * Gets the value of currency.
267: *
268: * @return string
269: */
270: public function getCurrency()
271: {
272: return $this->currency;
273: }
274:
275: /**
276: * Sets the value of currency.
277: *
278: * @param string $currency the currency
279: *
280: * @return self
281: */
282: public function setCurrency($currency)
283: {
284: $this->currency = $currency;
285:
286: return $this;
287: }
288:
289: /**
290: * Gets the value of description.
291: *
292: * @return string
293: */
294: public function getDescription()
295: {
296: return $this->description;
297: }
298:
299: /**
300: * Sets the value of description.
301: *
302: * @param string $description the description
303: *
304: * @return self
305: */
306: public function setDescription($description)
307: {
308: $this->description = $description;
309:
310: return $this;
311: }
312:
313: /**
314: * Gets the value of extra.
315: *
316: * @return string
317: */
318: public function getExtra()
319: {
320: return $this->extra;
321: }
322:
323: /**
324: * Sets the value of extra.
325: *
326: * @param string $extra the extra
327: *
328: * @return self
329: */
330: public function setExtra($extra)
331: {
332: $this->extra = $extra;
333:
334: return $this;
335: }
336:
337: /**
338: * Gets the value of recipient_map.
339: *
340: * @return array
341: */
342: public function getRecipientMap()
343: {
344: return $this->recipient_map;
345: }
346:
347: /**
348: * Sets the value of recipient_map.
349: *
350: * @param array $recipientMap An array of Syspay_Merchant_Entity_PaymentRecipient
351: *
352: * @return self
353: */
354: public function setRecipientMap(array $recipientMap)
355: {
356: foreach ($recipientMap as $r) {
357: if (!$r instanceof Syspay_Merchant_Entity_PaymentRecipient) {
358: throw new InvalidArgumentException(
359: 'The given array must only contain Syspay_Merchant_Entity_PaymentRecipient instances'
360: );
361: }
362: }
363: $this->recipient_map = $recipientMap;
364:
365: return $this;
366: }
367:
368: /**
369: * Add a PaymentRecipient to the recipient map list
370: *
371: * @param Syspay_Merchant_Entity_PaymentRecipient $paymentRecipient
372: *
373: * @return self
374: */
375: public function addRecipient(Syspay_Merchant_Entity_PaymentRecipient $paymentRecipient)
376: {
377: if (!isset($this->recipient_map)) {
378: $this->recipient_map = array();
379: }
380: array_push($this->recipient_map, $paymentRecipient);
381: }
382: }
383: