18 require_once SQ_INCLUDE_PATH.
'/asset.inc';
19 require_once SQ_PACKAGES_PATH.
'/ecommerce/paypal/paypal_order/paypal_order.inc';
44 parent::__construct($assetid);
61 if (!parent::_preCreateCheck($link))
return FALSE;
63 $name = trim($this->
attr(
'name'));
65 trigger_localised_error(
'CORE0083', E_USER_WARNING, $GLOBALS[
'SQ_SYSTEM']->am->getTypeInfo($this->type(),
'name'));
83 $allowed_links = parent::_getAllowedLinks();
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);
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);
93 return $allowed_links;
110 if (!parent::_createAdditional($link))
return FALSE;
114 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'folder');
117 'pending_orders' =>
'folder',
118 'completed_orders' =>
'folder',
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);
125 $asset->setAttrValue(
'name', ucwords(str_replace(
'_',
' ', $name)));
126 if (!$asset->create($copy_link))
return FALSE;
128 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
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)) {
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']}.";
157 $message =
"A NEW PAYPAL ORDER (#{$new_order->id}) IS CREATED.";
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);
168 $message =
"THE PAYPAL ORDER #{$order->id} HAS BEEN MOVED TO THE COMPLETED FOLDER.";
171 $message =
"THE PAYPAL ORDER #{$order->id} COULD NOT BE MOVED TO THE COMPLETED FOLDER.";
175 $message =
"THE TRANSACTION #{$payment_info['txn_id']} WAS ALREADY COMPLETED.";
181 if ($order->attr(
'payment_status') != $payment_info[
'payment_status']) {
182 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
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}.";
191 $message =
"NEW PAYMENT STATUS {$payment_info['payment_status']} COULD NOT BE UPDATED TO THE PAYPAL ORDER #{$order->id}.";
194 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
197 $message =
"THE PAYMENT STATUS {$payment_info['payment_status']} OF THE PAYPAL ORDER #{$order->id} HAS NOT BEEN CHANGED.";
205 $this->
sendEmailOnError(
"An error occurs under the Paypal Business Account #{$this->id}.\nERROR MESSAGE: $message");
221 private function _createOrder($payment_info)
223 $am = $GLOBALS[
'SQ_SYSTEM']->am;
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;
231 $order_folder_id = $order_folder_link[
'minorid'];
232 $order_folder = $am->getAsset($order_folder_id);
236 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
237 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
238 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
243 'asset' => $order_folder,
244 'link_type' => SQ_LINK_TYPE_2,
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));
257 if ($payment_info[
'txn_type'] ==
'cart') {
258 for ($i = 1; $i <= $payment_info[
'num_cart_items']; $i++) {
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],
267 $products[] = $product;
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'],
278 $products = Array($product);
281 $paypal_order->setAttrValue(
'products', $products);
283 if ($paypal_order->saveAttributes()) {
285 if ($am->createAssetLink($this, $paypal_order, SQ_LINK_NOTICE, $payment_info[
'txn_id']) != 0) {
292 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
294 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
296 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
299 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
302 return ($success? $paypal_order : FALSE);
316 private function _moveOrder($paypal_order, $payment_info)
318 $am = $GLOBALS[
'SQ_SYSTEM']->am;
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);
325 $order_folder_link = $am->getLink($this->
id, SQ_LINK_TYPE_2,
'folder', TRUE,
'completed_orders');
329 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
330 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
331 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
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));
339 if ($paypal_order->saveAttributes()) {
345 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
347 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
349 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
352 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
367 private function _getCustomInfo($payment_info)
369 $custom_indexes = Array(
'txn_type',
'test_ipn',
'payment_date',
'custom',
'button_id',
'real_custom_var',
'notify_url_query',
'business',
'target_assetid',
'ipn_event');
371 foreach ($custom_indexes as $index) {
372 $message = empty($payment_info[$index])?
'[Empty]' : $payment_info[$index];
373 $custom_string .=
"$index=$message\n";
376 return $custom_string;
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;
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");
413 protected function _getName($short_name=FALSE, $contextid=NULL)
416 if ($contextid === NULL) {
417 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
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);
425 return $values[$this->id];