1: <?php
2:
3: namespace mcfedr\Paypal\Products;
4:
5: /**
6: * Describes a recurring product
7: */
8: class Subscription extends Product {
9:
10: const DAYS = 'D';
11: const WEEKS = 'W';
12: const MONTHS = 'M';
13: const YEARS = 'Y';
14:
15: /**
16: * The length of the subscription
17: * @var int
18: */
19: public $duration;
20:
21: /**
22: * Unit used for {@link $duration}
23: * @see DAYS
24: * @see WEEKS
25: * @see MONTHS
26: * @see YEARS
27: * @var string
28: */
29: public $units;
30:
31: /**
32: * Trial price of the product
33: * @var double
34: */
35: public $trialAmount;
36:
37: /**
38: * The length of the trial subscription
39: * @var int
40: */
41: public $trialDuration;
42:
43: /**
44: * Unit used for {@link $trialDuration}
45: * @see DAYS
46: * @see WEEKS
47: * @see MONTHS
48: * @see YEARS
49: * @var string
50: */
51: public $trialUnits;
52:
53: /**
54: * Is this a recurring subscription
55: * @var bool
56: */
57: public $recurring = true;
58:
59: /**
60: * Number of times to recur
61: * @var int
62: */
63: public $recurLimit;
64:
65: /**
66: * Whether to reattempt collection of payment when it fails
67: * @var bool
68: */
69: public $reattempt = true;
70:
71: /**
72: * Can the user signup for a new subscription with this button
73: * @var bool
74: */
75: public $allowNew = true;
76:
77: /**
78: * Can the user modify their existing subscription with this button
79: * @var bool
80: */
81: public $allowModify = false;
82:
83: /**
84: * Let paypal generate a username and password
85: * @var bool
86: */
87: public $generateUsernameAndPassword = false;
88:
89: /**
90: * The paypal id for this subscription
91: * Set for notification
92: * @var string
93: */
94: public $subscriptionId;
95:
96: /**
97: * The generated username
98: * @var string
99: */
100: public $username;
101:
102: /**
103: * The generated password
104: * @var string
105: */
106: public $password;
107:
108: /**
109: * Get a product from $vars
110: *
111: * @param array $vars
112: */
113: public function __construct($vars = null) {
114: parent::__construct($vars);
115:
116: if (!is_null($vars)) {
117: if (isset($vars["subscr_id"])) {
118: $this->subscriptionId = $vars["subscr_id"];
119: }
120:
121: if (isset($vars["mc_gross"])) {
122: $this->amount = $vars["mc_gross"];
123: }
124: else if (isset($vars['mc_amount3'])) {
125: $this->amount = $vars["mc_amount3"];
126: }
127: if (isset($vars['period3'])) {
128: $p = explode(' ', $vars['period3']);
129: $this->duration = $p[0];
130: $this->units = $p[1];
131: }
132:
133: if (isset($vars['mc_amount1'])) {
134: $this->trialAmount = $vars["mc_amount1"];
135: }
136: if (isset($vars['period1'])) {
137: $p = explode(' ', $vars['period1']);
138: $this->trialDuration = $p[0];
139: $this->trialUnits = $p[1];
140: }
141:
142: if (isset($vars['reattempt'])) {
143: $this->reattempt = true;
144: }
145: else {
146: $this->reattempt = false;
147: }
148: if (isset($vars['recur_times'])) {
149: $this->recurLimit = $vars['recur_times'];
150: }
151: if (isset($vars['recurring'])) {
152: $this->recurring = true;
153: }
154: else {
155: $this->recurring = false;
156: }
157:
158: if (isset($vars['username']) && isset($vars['password'])) {
159: $this->username = $vars['username'];
160: $this->password = $vars['password'];
161: $this->generateUsernameAndPassword = true;
162: }
163: }
164: }
165:
166: /**
167: * Sets up the array with paypal vars for $product
168: *
169: * @param array $params
170: * @param string $suffix
171: */
172: public function setParams(&$params, $suffix = '') {
173: parent::setParams($params);
174:
175: $params['a3'] = $this->amount;
176: $params['p3'] = $this->duration;
177: $params['t3'] = $this->units;
178:
179: if (!empty($this->trialAmount)) {
180: $params['a1'] = $this->trialAmount;
181: if (!empty($this->trialDuration)) {
182: $params['p1'] = $this->trialDuration;
183: }
184: else {
185: $params['p1'] = $this->duration;
186: }
187: if (!empty($this->trialUnits)) {
188: $params['t1'] = $this->trialUnits;
189: }
190: else {
191: $params['t1'] = $this->units;
192: }
193: }
194:
195: if ($this->recurring) {
196: $params['src'] = 1;
197: if (!empty($this->recurLimit)) {
198: $params['srt'] = $this->recurLimit;
199: }
200: }
201:
202: if ($this->reattempt) {
203: $params['sra'] = 1;
204: }
205:
206: if ($this->allowNew && $this->allowModify) {
207: $params['modify'] = 1;
208: }
209: else if ($this->allowModify) {
210: $params['modify'] = 2;
211: }
212:
213: if ($this->generateUsernameAndPassword) {
214: $params['usr_manage'] = 1;
215: }
216: }
217:
218: }
219: