/** * Public function that creates a single instance */ public static function getInstance() { if (!isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * Handle completed transaction. * * @param object $transaction */ public function handleTransactionCompleted($transaction) { global $db; $manager = SapphireWavesManager::getInstance(); $plan_manager = ShopTransactionPlansManager::getInstance(); // get plan $plan = $plan_manager->getSingleItem($plan_manager->getFieldNames(), array('transaction' => $transaction->id)); // try get subscription data $current_data = $manager->getSingleItem($manager->getFieldNames(), array('user' => $transaction->system_user)); $duration = SW_PlanType::$duration[$plan->plan_name]; $unlimited = SW_PlanType::$unlimited[$plan->plan_name]; $expire_timestamp = strtotime('next month', time()); $data = array('type' => SW_PlanType::$type[$plan->plan_name], 'expires' => $db->format_timestamp($expire_timestamp), 'active' => 1); // add more time to the remaining time if (is_object($current_data)) { if (!$unlimited) { $data['remaining_time'] = $current_data->remaining_time + $duration * 60 * 60; } else { $manager->updateData($data, array('user' => $transaction->system_user)); } } else { $data['user'] = $transaction->system_user; if (!$unlimited) { $data['remaining_time'] = $duration * 60 * 60; } $manager->insertData($data); } }
/** * Send email for transaction using specified template. * * @param object $transaction * @param string $template * @return boolean */ private function sendTransactionMail($transaction, $template) { global $language; $result = false; // require contact form if (!class_exists('contact_form')) { return $result; } $email_address = null; $contact_form = contact_form::getInstance(); // template replacement data $fields = array('transaction_id' => $transaction->id, 'transaction_uid' => $transaction->uid, 'status' => $transaction->status, 'handling' => $transaction->handling, 'shipping' => $transaction->shipping, 'total' => $transaction->total, 'weight' => $transaction->weight, 'payment_method' => $transaction->payment_method, 'delivery_method' => $transaction->delivery_method, 'remark' => $transaction->remark, 'token' => $transaction->token, 'timestamp' => $transaction->timestamp); $timestamp = strtotime($transaction->timestamp); $fields['date'] = date($this->getLanguageConstant('format_date_short'), $timestamp); $fields['time'] = date($this->getLanguageConstant('format_time_short'), $timestamp); // get currency $currency_manager = ShopCurrenciesManager::getInstance(); $currency = $currency_manager->getSingleItem($currency_manager->getFieldNames(), array('id' => $transaction->currency)); if (is_object($currency)) { $fields['currency'] = $currency->currency; } // add buyer information $buyer_manager = ShopBuyersManager::getInstance(); $buyer = $buyer_manager->getSingleItem($buyer_manager->getFieldNames(), array('id' => $transaction->buyer)); if (is_object($buyer)) { $fields['buyer_first_name'] = $buyer->first_name; $fields['buyer_last_name'] = $buyer->last_name; $fields['buyer_email'] = $buyer->email; $fields['buyer_uid'] = $buyer->uid; $email_address = $buyer->email; } // add system user information $user_manager = UserManager::getInstance(); $user = $user_manager->getSingleItem($user_manager->getFieldNames(), array('id' => $transaction->system_user)); if (is_object($user)) { $fields['user_name'] = $user->username; $fields['user_fullname'] = $user->fullname; $fields['user_email'] = $user->email; if (is_null($email_address) || empty($email_address)) { $email_address = $user->email; } else { if ($email_address != $user->email) { $email_address = $email_address . ',' . $user->email; } } } // add buyer address $address_manager = ShopDeliveryAddressManager::getInstance(); $address = $address_manager->getSingleItem($address_manager->getFieldNames(), array('id' => $transaction->address)); if (is_object($address)) { $fields['address_name'] = $address->name; $fields['address_street'] = $address->street; $fields['address_street2'] = $address->street2; $fields['address_phone'] = $address->phone; $fields['address_city'] = $address->city; $fields['address_zip'] = $address->zip; $fields['address_state'] = $address->state; $fields['address_country'] = $address->country; } // create item table switch ($transaction->type) { case TransactionType::SHOPPING_CART: $item_manager = ShopTransactionItemsManager::getInstance(); $items = $item_manager->getItems($item_manager->getFieldNames(), array('transaction' => $transaction->id)); if (count($items) > 0) { // create items table $text_table = str_pad($this->getLanguageConstant('column_name'), 40); $text_table .= str_pad($this->getLanguageConstant('column_price'), 8); $text_table .= str_pad($this->getLanguageConstant('column_amount'), 6); $text_table .= str_pad($this->getLanguageConstant('column_item_total'), 8); $text_table .= "\n" . str_repeat('-', 40 + 8 + 6 + 8) . "\n"; $html_table = '<table border="0" cellspacing="5" cellpadding="0">'; $html_table .= '<thead><tr>'; $html_table .= '<td>' . $this->getLanguageConstant('column_name') . '</td>'; $html_table .= '<td>' . $this->getLanguageConstant('column_price') . '</td>'; $html_table .= '<td>' . $this->getLanguageConstant('column_amount') . '</td>'; $html_table .= '<td>' . $this->getLanguageConstant('column_item_total') . '</td>'; $html_table .= '</td></thead><tbody>'; foreach ($items as $item) { // append item name with description if (empty($data['description'])) { $line = $item->name[$language] . ' (' . $item->description . ')'; } else { $line = $item->name[$language]; } $line = utf8_wordwrap($line, 40, "\n", true); $line = mb_split("\n", $line); // append other columns $line[0] = $line[0] . str_pad($item->price, 8, ' ', STR_PAD_LEFT); $line[0] = $line[0] . str_pad($item->amount, 6, ' ', STR_PAD_LEFT); $line[0] = $line[0] . str_pad($item->total, 8, ' ', STR_PAD_LEFT); // add this item to text table $text_table .= implode("\n", $line) . "\n\n"; // form html row $row = '<tr><td>' . $item->name[$language]; if (!empty($item->description)) { $row .= ' <small>' . $item->description . '</small>'; } $row .= '</td><td>' . $item->price . '</td>'; $row .= '<td>' . $item->amount . '</td>'; $row .= '<td>' . $item->total . '</td></tr>'; // update subtotal $subtotal += $item->total; } // close text table $text_table .= str_repeat('-', 40 + 8 + 6 + 8) . "\n"; $html_table .= '</tbody>'; // create totals $text_table .= str_pad($this->getLanguageConstant('column_subtotal'), 15); $text_table .= str_pad($subtotal, 10, ' ', STR_PAD_LEFT) . "\n"; $text_table .= str_pad($this->getLanguageConstant('column_shipping'), 15); $text_table .= str_pad($transaction->shipping, 10, ' ', STR_PAD_LEFT) . "\n"; $text_table .= str_pad($this->getLanguageConstant('column_handling'), 15); $text_table .= str_pad($transaction->handling, 10, ' ', STR_PAD_LEFT) . "\n"; $text_table .= str_repeat('-', 25); $text_table .= str_pad($this->getLanguageConstant('column_total'), 15); $text_table .= str_pad($transaction->total, 10, ' ', STR_PAD_LEFT) . "\n"; $html_table .= '<tfoot>'; $html_table .= '<tr><td colspan="2"></td><td>' . $this->getLanguageConstant('column_subtotal') . '</td>'; $html_table .= '<td>' . $subtotal . '</td></tr>'; $html_table .= '<tr><td colspan="2"></td><td>' . $this->getLanguageConstant('column_shipping') . '</td>'; $html_table .= '<td>' . $transaction->shipping . '</td></tr>'; $html_table .= '<tr><td colspan="2"></td><td>' . $this->getLanguageConstant('column_handling') . '</td>'; $html_table .= '<td>' . $transaction->handling . '</td></tr>'; $html_table .= '<tr><td colspan="2"></td><td><b>' . $this->getLanguageConstant('column_total') . '</b></td>'; $html_table .= '<td><b>' . $transaction->total . '</b></td></tr>'; $html_table .= '</tfoot>'; // close table $html_table .= '</table>'; // add field $fields['item_table'] = $text_table; } break; case TransactionType::SUBSCRIPTION: $plan_manager = ShopTransactionPlansManager::getInstance(); $plan = $plan_manager->getSingleItem($plan_manager->getFieldNames(), array('transaction' => $transaction->id)); // get payment method $plan_data = null; if (isset($this->payment_methods[$transaction->payment_method])) { $payment_method = $this->payment_methods[$transaction->payment_method]; $plans = $payment_method->get_recurring_plans(); if (isset($plans[$plan->plan_name])) { $plan_data = $plans[$plan->plan_name]; } } // populate fields with plan params if (is_object($plan) && !is_null($plan_data)) { $fields['plan_text_id'] = $plan->plan_name; $fields['plan_name'] = $plan_data['name'][$language]; } break; } // we require email address for sending if (is_null($email_address) || empty($email_address)) { return $result; } // get mailer $mailer = $contact_form->getMailer(); $sender = $contact_form->getSender(); $template = $contact_form->getTemplate($template); // start creating message $mailer->start_message(); $mailer->set_subject($template['subject']); $mailer->set_sender($sender['address'], $sender['name']); $mailer->add_recipient($email_address); $mailer->set_body($template['plain_body'], $template['html_body']); $mailer->set_variables($fields); // send email $mailer->send(); return $result; }
/** * Handle recurring payment IPN. * * @param object $transaction * @param string $type * @param float $amount * @return boolean */ private function handleRecurringIPN($transaction, $type, $amount) { $result = false; $shop = shop::getInstance(); $plan_manager = ShopTransactionPlansManager::getInstance(); // get plan associated with this transaction $plan = $plan_manager->getSingleItem($plan_manager->getFieldNames(), array('transaction' => $transaction->id)); if (!is_object($plan)) { trigger_error('PayPal: Unable to handle IPN, unable to get plan for transaction: ' . $transaction->id, E_USER_WARNING); return $result; } // notification type to status relation $status = array('recurring_payment' => RecurringPayment::ACTIVE, 'recurring_payment_expired' => RecurringPayment::EXPIRED, 'recurring_payment_failed' => RecurringPayment::FAILED, 'recurring_payment_profile_created' => RecurringPayment::PENDING, 'recurring_payment_profile_cancel' => RecurringPayment::CANCELED, 'recurring_payment_skipped' => RecurringPayment::SKIPPED, 'recurring_payment_suspended' => RecurringPayment::SUSPENDED, 'recurring_payment_suspended_due_to_max_failed_payment' => RecurringPayment::SUSPENDED); // add new recurring payment $result = $shop->addRecurringPayment($plan->id, $amount, $status[$type]); return $result; }