1: <?php
2:
3: /**
4: * A subscription object
5: */
6: class Syspay_Merchant_Entity_Subscription extends Syspay_Merchant_Entity implements
7: Syspay_Merchant_Entity_ReturnedEntityInterface
8: {
9: const TYPE = 'subscription';
10:
11: const STATUS_PENDING = 'PENDING'; // First payment not yet successful
12: const STATUS_ACTIVE = 'ACTIVE'; // Active susbscription
13: const STATUS_CANCELLED = 'CANCELLED'; // The first payment failed, the subscription is cancelled
14: const STATUS_ENDED = 'ENDED'; // Subscription has ended but rebills are still possible
15: const STATUS_TERMINATED = 'TERMINATED'; // Subscription is completely terminated, manual rebills are not possible anymore
16:
17: const PHASE_NEW = 'NEW';
18: const PHASE_TRIAL = 'TRIAL';
19: const PHASE_BILLING = 'BILLING';
20: const PHASE_RETRY = 'RETRY';
21: const PHASE_LAST = 'LAST';
22: const PHASE_CLOSED = 'CLOSED';
23:
24: // Merchant stopped the subscription via his interface or via the API
25: const END_REASON_UNSUBSCRIBED_MERCHANT = 'UNSUBSCRIBED_MERCHANT';
26: // Admin stopped the subscription via the admin interface
27: const END_REASON_UNSUBSCRIBED_ADMIN = 'UNSUBSCRIBED_ADMIN';
28: // Max retry attempts reached for one payment
29: const END_REASON_SUSPENDED_ATTEMPTS = 'SUSPENDED_ATTEMPTS';
30: // Payment method has expired
31: const END_REASON_SUSPENDED_EXPIRED = 'SUSPENDED_EXPIRED';
32: // A chargeback was received on this subscription
33: const END_REASON_SUSPENDED_CHARGEBACK = 'SUSPENDED_CHARGEBACK';
34: // The subscription is complete
35: const END_REASON_COMPLETE = 'COMPLETE';
36:
37: /**
38: * @var integer
39: */
40: private $id;
41:
42: /**
43: * @var DateTime
44: */
45: private $created;
46:
47: /**
48: * @var DateTime
49: */
50: private $start_date;
51:
52: /**
53: * @var DateTime
54: */
55: private $end_date;
56:
57: /**
58: * @var string
59: */
60: private $status;
61:
62: /**
63: * @var string
64: */
65: private $phase;
66:
67: /**
68: * @var string
69: */
70: private $end_reason;
71:
72: /**
73: * @var Syspay_Merchant_Entity_PaymentMethod
74: */
75: private $payment_method;
76:
77: /**
78: * @var integer
79: */
80: protected $website_id;
81:
82: /**
83: * @var string
84: */
85: protected $ems_url;
86:
87: /**
88: * @var string
89: */
90: protected $redirect_url;
91:
92: /**
93: * @var Syspay_Merchant_Entity_Plan
94: */
95: protected $plan;
96:
97: /**
98: * @var Syspay_Merchant_Entity_Customer
99: */
100: private $customer;
101:
102: /**
103: * @var integer
104: */
105: protected $plan_id;
106:
107: /**
108: * @var string
109: */
110: private $plan_type;
111:
112: /**
113: * @var string
114: */
115: protected $extra;
116:
117: /**
118: * @var string
119: */
120: protected $reference;
121:
122: /**
123: * @var string
124: */
125: private $redirect;
126:
127: /**
128: * @var Syspay_Merchant_Entity_SubscriptionEvent
129: */
130: private $next_event;
131:
132: /**
133: * @var integer
134: */
135: protected $capture_delay;
136:
137: /**
138: * Build a subscription entity based on a json-decoded subscription stdClass
139: *
140: * @param stdClass $response The subscription data
141: * @return Syspay_Merchant_Entity_Subscription The subscription object
142: */
143: public static function buildFromResponse(stdClass $response)
144: {
145: $subscription = new self();
146: $subscription->setId(isset($response->id)?$response->id:null);
147: $subscription->setPlanId(isset($response->plan_id)?$response->plan_id:null);
148: $subscription->setPlanType(isset($response->plan_type)?$response->plan_type:null);
149: $subscription->setReference(isset($response->reference)?$response->reference:null);
150: $subscription->setStatus(isset($response->status)?$response->status:null);
151: $subscription->setPhase(isset($response->phase)?$response->phase:null);
152: $subscription->setExtra(isset($response->extra)?$response->extra:null);
153: $subscription->setEmsUrl(isset($response->ems_url)?$response->ems_url:null);
154: $subscription->setWebsiteId(isset($response->website_id)?$response->website_id:null);
155: $subscription->setCaptureDelay(isset($response->capture_delay)?$response->capture_delay:null);
156: $subscription->setCreated(
157: isset($response->created)?Syspay_Merchant_Utils::tsToDateTime($response->created):null
158: );
159: $subscription->setStartDate(
160: isset($response->start_date)?Syspay_Merchant_Utils::tsToDateTime($response->start_date):null
161: );
162: $subscription->setEndDate(
163: isset($response->end_date)?Syspay_Merchant_Utils::tsToDateTime($response->end_date):null
164: );
165: $subscription->setEndReason(
166: isset($response->end_reason)?$response->end_reason:null
167: );
168:
169: if (isset($response->payment_method)
170: && ($response->payment_method instanceof stdClass)) {
171: $paymentMethod = Syspay_Merchant_Entity_PaymentMethod::buildFromResponse($response->payment_method);
172: $subscription->setPaymentMethod($paymentMethod);
173: }
174:
175: if (isset($response->customer)
176: && ($response->customer instanceof stdClass)) {
177: $customer = Syspay_Merchant_Entity_Customer::buildFromResponse($response->customer);
178: $subscription->setCustomer($customer);
179: }
180:
181: if (isset($response->plan)
182: && ($response->plan instanceof stdClass)) {
183: $plan = Syspay_Merchant_Entity_Plan::buildFromResponse($response->plan);
184: $subscription->setPlan($plan);
185: }
186:
187: if (isset($response->next_event)
188: && ($response->next_event instanceof stdClass)) {
189: $nextEvent = Syspay_Merchant_Entity_SubscriptionEvent::buildFromResponse($response->next_event);
190: $subscription->setNextEvent($nextEvent);
191: }
192:
193: $subscription->raw = $response;
194:
195: return $subscription;
196: }
197:
198: /**
199: * Gets the value of id.
200: *
201: * @return integer
202: */
203: public function getId()
204: {
205: return $this->id;
206: }
207:
208: /**
209: * Sets the value of id.
210: *
211: * @param integer $id the id
212: *
213: * @return self
214: */
215: public function setId($id)
216: {
217: $this->id = $id;
218:
219: return $this;
220: }
221:
222: /**
223: * Gets the value of created.
224: *
225: * @return DateTime
226: */
227: public function getCreated()
228: {
229: return $this->created;
230: }
231:
232: /**
233: * Sets the value of created.
234: *
235: * @param DateTime $created the created
236: *
237: * @return self
238: */
239: public function setCreated(DateTime $created = null)
240: {
241: $this->created = $created;
242:
243: return $this;
244: }
245:
246: /**
247: * Gets the value of start_date.
248: *
249: * @return DateTime
250: */
251: public function getStartDate()
252: {
253: return $this->start_date;
254: }
255:
256: /**
257: * Sets the value of start_date.
258: *
259: * @param DateTime $start_date the start_date
260: *
261: * @return self
262: */
263: public function setStartDate(DateTime $start_date = null)
264: {
265: $this->start_date = $start_date;
266:
267: return $this;
268: }
269:
270: /**
271: * Gets the value of end_date.
272: *
273: * @return DateTime
274: */
275: public function getEndDate()
276: {
277: return $this->end_date;
278: }
279:
280: /**
281: * Sets the value of end_date.
282: *
283: * @param DateTime $end_date the end_date
284: *
285: * @return self
286: */
287: public function setEndDate(DateTime $end_date = null)
288: {
289: $this->end_date = $end_date;
290:
291: return $this;
292: }
293:
294: /**
295: * Gets the value of status.
296: *
297: * @return string
298: */
299: public function getStatus()
300: {
301: return $this->status;
302: }
303:
304: /**
305: * Sets the value of status.
306: *
307: * @param string $status the status
308: *
309: * @return self
310: */
311: public function setStatus($status)
312: {
313: $this->status = $status;
314:
315: return $this;
316: }
317:
318: /**
319: * Gets the value of phase.
320: *
321: * @return string
322: */
323: public function getPhase()
324: {
325: return $this->phase;
326: }
327:
328: /**
329: * Sets the value of phase.
330: *
331: * @param string $phase the phase
332: *
333: * @return self
334: */
335: public function setPhase($phase)
336: {
337: $this->phase = $phase;
338:
339: return $this;
340: }
341:
342: /**
343: * Gets the value of end_reason.
344: *
345: * @return string
346: */
347: public function getEndReason()
348: {
349: return $this->end_reason;
350: }
351:
352: /**
353: * Sets the value of end_reason.
354: *
355: * @param string $end_reason the end_reason
356: *
357: * @return self
358: */
359: public function setEndReason($end_reason)
360: {
361: $this->end_reason = $end_reason;
362:
363: return $this;
364: }
365:
366: /**
367: * Gets the value of payment_method.
368: *
369: * @return Syspay_Merchant_Entity_PaymentMethod
370: */
371: public function getPaymentMethod()
372: {
373: return $this->payment_method;
374: }
375:
376: /**
377: * Sets the value of payment_method.
378: *
379: * @param Syspay_Merchant_Entity_PaymentMethod $payment_method the payment_method
380: *
381: * @return self
382: */
383: public function setPaymentMethod(Syspay_Merchant_Entity_PaymentMethod $payment_method)
384: {
385: $this->payment_method = $payment_method;
386:
387: return $this;
388: }
389:
390: /**
391: * Gets the value of website_id.
392: * Kept for BC.
393: *
394: * @return integer
395: */
396: public function getWebsite()
397: {
398: return $this->website_id;
399: }
400:
401: /**
402: * Sets the value of website_id
403: * Kept for BC.
404: *
405: * @param integer $website the website id
406: *
407: * @return self
408: */
409: public function setWebsite($website)
410: {
411: $this->website_id = $website;
412:
413: return $this;
414: }
415:
416: /**
417: * Gets the value of website_id.
418: *
419: * @return integer
420: */
421: public function getWebsiteId()
422: {
423: return $this->website_id;
424: }
425:
426: /**
427: * Sets the value of website_id
428: *
429: * @param integer $websiteId the website id
430: *
431: * @return self
432: */
433: public function setWebsiteId($websiteId)
434: {
435: $this->website_id = $websiteId;
436:
437: return $this;
438: }
439:
440: /**
441: * Gets the value of ems_url.
442: *
443: * @return string
444: */
445: public function getEmsUrl()
446: {
447: return $this->ems_url;
448: }
449:
450: /**
451: * Sets the value of ems_url.
452: *
453: * @param string $ems_url the ems_url
454: *
455: * @return self
456: */
457: public function setEmsUrl($ems_url)
458: {
459: $this->ems_url = $ems_url;
460:
461: return $this;
462: }
463:
464: /**
465: * Gets the value of redirect_url.
466: *
467: * @return string
468: */
469: public function getRedirectUrl()
470: {
471: return $this->redirect_url;
472: }
473:
474: /**
475: * Sets the value of redirect_url.
476: *
477: * @param string $redirect_url the redirect_url
478: *
479: * @return self
480: */
481: public function setRedirectUrl($redirect_url)
482: {
483: $this->redirect_url = $redirect_url;
484:
485: return $this;
486: }
487:
488: /**
489: * Gets the value of customer.
490: *
491: * @return Syspay_Merchant_Entity_Customer
492: */
493: public function getCustomer()
494: {
495: return $this->customer;
496: }
497:
498: /**
499: * Sets the value of customer.
500: *
501: * @param Syspay_Merchant_Entity_Customer $customer the customer
502: *
503: * @return self
504: */
505: public function setCustomer(Syspay_Merchant_Entity_Customer $customer)
506: {
507: $this->customer = $customer;
508:
509: return $this;
510: }
511:
512: /**
513: * Gets the value of plan.
514: *
515: * @return Syspay_Merchant_Entity_Plan
516: */
517: public function getPlan()
518: {
519: return $this->plan;
520: }
521:
522: /**
523: * Sets the value of plan.
524: *
525: * @param Syspay_Merchant_Entity_Plan $plan the plan
526: *
527: * @return self
528: */
529: public function setPlan(Syspay_Merchant_Entity_Plan $plan)
530: {
531: $this->plan = $plan;
532:
533: return $this;
534: }
535:
536: /**
537: * Gets the value of plan_id.
538: *
539: * @return integer
540: */
541: public function getPlanId()
542: {
543: return $this->plan_id;
544: }
545:
546: /**
547: * Sets the value of plan_id.
548: *
549: * @param integer $plan_id the plan_id
550: *
551: * @return self
552: */
553: public function setPlanId($plan_id)
554: {
555: $this->plan_id = $plan_id;
556:
557: return $this;
558: }
559:
560: /**
561: * Gets the value of plan_type.
562: *
563: * @return string
564: */
565: public function getPlanType()
566: {
567: return $this->plan_type;
568: }
569:
570: /**
571: * Sets the value of plan_type.
572: *
573: * @param integer $plan_type the plan_type
574: *
575: * @return self
576: */
577: public function setPlanType($plan_type)
578: {
579: $this->plan_type = $plan_type;
580:
581: return $this;
582: }
583:
584: /**
585: * Gets the value of extra.
586: *
587: * @return string
588: */
589: public function getExtra()
590: {
591: return $this->extra;
592: }
593:
594: /**
595: * Sets the value of extra.
596: *
597: * @param string $extra the extra
598: *
599: * @return self
600: */
601: public function setExtra($extra)
602: {
603: $this->extra = $extra;
604:
605: return $this;
606: }
607:
608: /**
609: * Gets the value of reference.
610: *
611: * @return string
612: */
613: public function getReference()
614: {
615: return $this->reference;
616: }
617:
618: /**
619: * Sets the value of reference.
620: *
621: * @param string $reference the reference
622: *
623: * @return self
624: */
625: public function setReference($reference)
626: {
627: $this->reference = $reference;
628:
629: return $this;
630: }
631:
632: /**
633: * Gets the value of redirect.
634: *
635: * @return string
636: */
637: public function getRedirect()
638: {
639: return $this->redirect;
640: }
641:
642: /**
643: * Sets the value of redirect.
644: *
645: * @param string $redirect the redirect
646: *
647: * @return self
648: */
649: public function setRedirect($redirect)
650: {
651: $this->redirect = $redirect;
652:
653: return $this;
654: }
655:
656: /**
657: * {@inheritDoc}
658: */
659: public function toArray()
660: {
661: $data = parent::toArray();
662: if (false === empty($this->plan)) {
663: $data['plan'] = $this->plan->toArray();
664: }
665: return $data;
666: }
667:
668:
669: /**
670: * Gets the next event
671: *
672: * @return Syspay_Merchant_Entity_SubscriptionEvent
673: */
674: public function getNextEvent()
675: {
676: return $this->next_event;
677: }
678:
679: /**
680: * Sets the next event
681: *
682: * @param Syspay_Merchant_Entity_SubscriptionEvent $next_event The next subscription event
683: *
684: * @return self
685: */
686: public function setNextEvent(Syspay_Merchant_Entity_SubscriptionEvent $next_event)
687: {
688: $this->next_event = $next_event;
689:
690: return $this;
691: }
692:
693: /**
694: * Gets the capture delay
695: *
696: * @return int
697: */
698: public function getCaptureDelay()
699: {
700: return $this->capture_delay;
701: }
702:
703: /**
704: * Sets the capture delay
705: *
706: * @param int $capture_delay
707: *
708: * @return self
709: */
710: public function setCaptureDelay($capture_delay)
711: {
712: $this->capture_delay = $capture_delay;
713:
714: return $this;
715: }
716:
717:
718: }
719: