Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
paypal_business_account.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 require_once SQ_PACKAGES_PATH.'/ecommerce/paypal/paypal_order/paypal_order.inc';
20 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
59  protected function _preCreateCheck(Array &$link)
60  {
61  if (!parent::_preCreateCheck($link)) return FALSE;
62 
63  $name = trim($this->attr('name'));
64  if ($name == '') {
65  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
66  return FALSE;
67  }
68 
69  return TRUE;
70 
71  }//end _preCreateCheck()
72 
73 
81  function _getAllowedLinks()
82  {
83  $allowed_links = parent::_getAllowedLinks();
84 
85  $allowed_links[SQ_LINK_TYPE_2]['folder'] = Array('card' => 'M', 'exclusive' => FALSE);
86  $allowed_links[SQ_LINK_NOTICE]['paypal_order'] = Array('card' => 'M', 'exclusive' => FALSE);
87  //for better organization structure
88  $allowed_links[SQ_LINK_TYPE_1]['paypal_payment_button'] = Array('card' => 'M', 'exclusive' => FALSE);
89  $allowed_links[SQ_LINK_TYPE_2]['paypal_payment_button'] = Array('card' => 'M', 'exclusive' => FALSE);
90  $allowed_links[SQ_LINK_TYPE_1]['paypal_ipn_receiver'] = Array('card' => 'M', 'exclusive' => FALSE);
91  $allowed_links[SQ_LINK_TYPE_2]['paypal_ipn_receiver'] = Array('card' => 'M', 'exclusive' => FALSE);
92 
93  return $allowed_links;
94 
95  }//end _getAllowedLinks()
96 
97 
108  function _createAdditional(&$link)
109  {
110  if (!parent::_createAdditional($link)) return FALSE;
111  if (!$this->makeAndSaveInitialWebPath(strtolower($this->attr('name')), $link)) return FALSE;
112 
113  // add a bodycopy to this page when creating
114  $GLOBALS['SQ_SYSTEM']->am->includeAsset('folder');
115 
116  $sub_assets = Array(
117  'pending_orders' => 'folder',
118  'completed_orders' => 'folder',
119  );
120 
121  foreach ($sub_assets as $name => $type) {
122  $asset = new $type();
123  $copy_link = Array('asset' => $this, 'value' => $name ,'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
124 
125  $asset->setAttrValue('name', ucwords(str_replace('_',' ', $name)));
126  if (!$asset->create($copy_link)) return FALSE;
127 
128  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
129  unset($asset);
130  }
131 
132  return TRUE;
133 
134  }//end _createAdditional()
135 
136 
146  public function addPaypalOrder($payment_info, &$message)
147  {
148  $success = FALSE;
149  $am = $GLOBALS['SQ_SYSTEM']->am;
150  $order_link = $am->getLink($this->id, SQ_LINK_NOTICE, 'paypal_order', TRUE, $payment_info['txn_id']);
151  if (empty($order_link)) {
152  //order does not exist, create it
153  $new_order = $this->_createOrder($payment_info);
154  if ($new_order === FALSE) {
155  $message = "CAN NOT CREAT A NEW PAYPAL ORDER FOR THE PAYPAL TRANSACTION #{$payment_info['txn_id']}.";
156  } else {
157  $message = "A NEW PAYPAL ORDER (#{$new_order->id}) IS CREATED.";
158  $success = TRUE;
159  }
160 
161  } else {
162  //order exist
163  if ($payment_info['payment_status'] == 'Completed') {
164  $order = $am->getAsset($order_link['minorid']);
165  if ($order->attr('payment_status') != 'Completed') {
166  $success = $this->_moveOrder($order, $payment_info);
167  if ($success) {
168  $message = "THE PAYPAL ORDER #{$order->id} HAS BEEN MOVED TO THE COMPLETED FOLDER.";
169  $success = TRUE;
170  } else {
171  $message = "THE PAYPAL ORDER #{$order->id} COULD NOT BE MOVED TO THE COMPLETED FOLDER.";
172  }
173 
174  } else {
175  $message = "THE TRANSACTION #{$payment_info['txn_id']} WAS ALREADY COMPLETED.";
176  $success = TRUE;
177  }
178 
179  } else {
180  //update the status if it is different
181  if ($order->attr('payment_status') != $payment_info['payment_status']) {
182  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
183 
184  $paypal_order->setAttrValue('payment_status', $payment_info['payment_status']);
185  $paypal_order->setAttrValue('incomplete_reason', $payment_info['incomplete_reason']);
186  $paypal_order->setAttrValue('custom_info', $this->_getCustomInfo($payment_info));
187  if ($paypal_order->saveAttributes()) {
188  $message = "NEW PAYMENT STATUS {$payment_info['payment_status']} IS UPDATED TO THE PAYPAL ORDER #{$order->id}.";
189  $success = TRUE;
190  } else {
191  $message = "NEW PAYMENT STATUS {$payment_info['payment_status']} COULD NOT BE UPDATED TO THE PAYPAL ORDER #{$order->id}.";
192  }
193 
194  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
195 
196  } else {
197  $message = "THE PAYMENT STATUS {$payment_info['payment_status']} OF THE PAYPAL ORDER #{$order->id} HAS NOT BEEN CHANGED.";
198  $success = TRUE;
199  }
200  }
201  }
202 
203  //if an error occurs, send emails to inform
204  if (!$success) {
205  $this->sendEmailOnError("An error occurs under the Paypal Business Account #{$this->id}.\nERROR MESSAGE: $message");
206  }
207 
208  return $success;
209 
210  }//end addPaypalOrder()
211 
212 
221  private function _createOrder($payment_info)
222  {
223  $am = $GLOBALS['SQ_SYSTEM']->am;
224 
225  //get folder
226  $payment_completed = $payment_info['payment_status'] == 'Completed';
227  $folder_link_value = $payment_completed? 'completed_orders' : 'pending_orders';
228  $order_folder_link = $am->getLink($this->id, SQ_LINK_TYPE_2, 'folder', TRUE, $folder_link_value);
229  if (empty($order_folder_link)) return FALSE;
230 
231  $order_folder_id = $order_folder_link['minorid'];
232  $order_folder = $am->getAsset($order_folder_id);
233 
234  $success = FALSE;
235 
236  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
237  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
238  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
239 
240  $paypal_order = new Paypal_Order();
241 
242  $order_link = Array(
243  'asset' => $order_folder,
244  'link_type' => SQ_LINK_TYPE_2,
245  );
246 
247  if ($paypal_order->create($order_link)) {
248  $paypal_order->setAttrValue('txn_id', $payment_info['txn_id']);
249  $paypal_order->setAttrValue('payment_status', $payment_info['payment_status']);
250  $paypal_order->setAttrValue('incomplete_reason', $payment_info['incomplete_reason']);
251  $paypal_order->setAttrValue('total_amount', $payment_info['mc_gross']);
252  $paypal_order->setAttrValue('currency', $payment_info['mc_currency']);
253  $paypal_order->setAttrValue('custom_info', $this->_getCustomInfo($payment_info));
254 
255  $products = Array();
256 
257  if ($payment_info['txn_type'] == 'cart') {
258  for ($i = 1; $i <= $payment_info['num_cart_items']; $i++) {
259  $product = Array(
260  'name' => $payment_info['item_name'.$i],
261  'number' => $payment_info['item_number'.$i],
262  'quantity' => $payment_info['quantity'.$i],
263  'price' => $payment_info['price_'.$i],
264  'subtotal' => $payment_info['mc_gross_'.$i],
265  );
266 
267  $products[] = $product;
268  }
269  } else {
270  $product = Array(
271  'name' => $payment_info['item_name'],
272  'number' => $payment_info['item_number'],
273  'quantity' => $payment_info['quantity'],
274  'price' => $payment_info['price'],
275  'subtotal' => $payment_info['mc_gross'],
276  );
277 
278  $products = Array($product);
279  }
280 
281  $paypal_order->setAttrValue('products', $products);
282 
283  if ($paypal_order->saveAttributes()) {
284  //create notice link from this Paypal Account asset to the new Paypal_Order asset
285  if ($am->createAssetLink($this, $paypal_order, SQ_LINK_NOTICE, $payment_info['txn_id']) != 0) {
286  $success = TRUE;
287  }
288  }
289  }
290 
291  if ($success) {
292  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
293  } else {
294  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
295  }
296  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
297 
298  // restore runlevel
299  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
300 
301 
302  return ($success? $paypal_order : FALSE);
303 
304  }//end _createOrder()
305 
306 
316  private function _moveOrder($paypal_order, $payment_info)
317  {
318  $am = $GLOBALS['SQ_SYSTEM']->am;
319  // get linkid of the paypal order to move
320  $pending_orders_link = $am->getLink($this->id, SQ_LINK_TYPE_2, 'folder', TRUE, 'pending_orders');
321  if (empty($pending_orders_link)) return FALSE;
322  $order_link_info = $am->getLinkByAsset($pending_orders_link['minorid'], $paypal_order->id);
323 
324  // get destination folder assetid
325  $order_folder_link = $am->getLink($this->id, SQ_LINK_TYPE_2, 'folder', TRUE, 'completed_orders');
326 
327  $success = FALSE;
328 
329  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
330  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
331  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
332 
333  // now move the Paypal order to its final resting place
334  if ($am->moveLink($order_link_info['linkid'], $order_folder_link['minorid'], SQ_LINK_TYPE_2, 0)) {
335  $paypal_order->setAttrValue('payment_status', $payment_info['payment_status']);
336  $paypal_order->setAttrValue('incomplete_reason', $payment_info['incomplete_reason']);
337  $paypal_order->setAttrValue('custom_info', $this->_getCustomInfo($payment_info));
338 
339  if ($paypal_order->saveAttributes()) {
340  $success = TRUE;
341  }
342  }
343 
344  if ($success) {
345  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
346  } else {
347  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
348  }
349  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
350 
351  // restore runlevel
352  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
353 
354  return $success;
355 
356  }//end _moveOrder()
357 
358 
367  private function _getCustomInfo($payment_info)
368  {
369  $custom_indexes = Array('txn_type', 'test_ipn', 'payment_date', 'custom', 'button_id', 'real_custom_var', 'notify_url_query', 'business', 'target_assetid', 'ipn_event');
370  $custom_string = '';
371  foreach ($custom_indexes as $index) {
372  $message = empty($payment_info[$index])? '[Empty]' : $payment_info[$index];
373  $custom_string .= "$index=$message\n";
374  }
375 
376  return $custom_string;
377 
378  }//end _getCustomInfo()
379 
386  public function sendEmailOnError($message)
387  {
388  if ($this->attr('send_email_on_error')) {
389  $to = $this->attr('error_email');
390  if (empty($to) && SQ_CONF_TECH_EMAIL) {
391  $to = SQ_CONF_TECH_EMAIL;
392  }
393 
394  if ($to) {
395  $subject = 'Matrix - '.SQ_CONF_SYSTEM_NAME.' - An error occurs when processing Payal Orders';
396  mail($to, $subject, $message, 'From: '.SQ_CONF_TECH_EMAIL."\r\n");
397  }
398  }
399 
400  }//end sendEmailOnError()
401 
402 
413  protected function _getName($short_name=FALSE, $contextid=NULL)
414  {
415  // No context specified, using the current context
416  if ($contextid === NULL) {
417  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
418  }//end if
419 
420  // Obtain the attribute value for Name from the specified Context
421  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
422  if (empty($values) === TRUE) {
423  return parent::_getName($short_name, $contextid);
424  } else {
425  return $values[$this->id];
426  }
427 
428  }//end _getName()
429 
430 
431 }//end class
432 ?>