public function get_transaction($create = true)
 {
     if (!isset($this->transaction)) {
         $id = $this->request->param('transaction_id');
         // TODO: inject PaymentTransaction dependency
         if ($create === true) {
             $this->transaction = AWPCP_Payment_Transaction::find_or_create($id);
         } else {
             $this->transaction = AWPCP_Payment_Transaction::find_by_id($id);
         }
     }
     if (!is_null($this->transaction) && $this->transaction->is_new()) {
         $this->transaction->user_id = wp_get_current_user()->ID;
         $this->transaction->set('context', 'add-credit');
         $this->transaction->set('redirect', $this->url());
         $this->transaction->set('redirect-data', array('action' => 'payment-completed'));
     }
     return $this->transaction;
 }
示例#2
0
 public static function find_or_create($id)
 {
     $transaction = AWPCP_Payment_Transaction::find_by_id($id);
     if (is_null($transaction)) {
         $unique = awpcp_array_data('UNIQUE_ID', rand(), $_SERVER);
         $id = md5($unique . microtime() . wp_salt());
         $transaction = new AWPCP_Payment_Transaction(array('id' => $id));
     }
     return $transaction;
 }
示例#3
0
 public function process_payment()
 {
     if (!($id = awpcp_post_param('transaction_id', false))) {
         return;
     }
     $transaction = AWPCP_Payment_Transaction::find_by_id($id);
     if (!is_null($transaction) && $transaction->is_doing_checkout()) {
         $this->set_transaction_payment_method($transaction);
         $this->process_transaction($transaction);
         $errors = array();
         $this->set_transaction_status_to_payment($transaction, $errors);
         if ($transaction->payment_is_not_required() && empty($errors)) {
             $this->set_transaction_status_to_payment_completed($transaction, $errors);
             if (empty($errors)) {
                 return;
                 // nothing else to do here, pass control to the (api) user
             }
         }
         if (empty($errors)) {
             // no errors, so we must have a payment method defined
             $payment_method = $this->get_transaction_payment_method($transaction);
             $result = array('output' => $payment_method->process_payment($transaction));
         } else {
             // most likely the payment method hasn't been properly set
             $result = array('errors' => $errors);
         }
         $this->cache[$transaction->id] = $result;
     } else {
         if (!is_null($transaction) && $transaction->is_processing_payment()) {
             $this->process_transaction($transaction);
             $payment_method = $this->get_transaction_payment_method($transaction);
             $result = array('output' => $payment_method->process_payment($transaction));
             $this->cache[$transaction->id] = $result;
         }
     }
 }
示例#4
0
 public function ajax_import_payment_transactions()
 {
     global $wpdb;
     $existing_transactions = $this->count_old_payment_transactions();
     $query = 'SELECT option_name FROM ' . $wpdb->options . ' ';
     $query .= "WHERE option_name LIKE 'awpcp-payment-transaction-%' ";
     $query .= "LIMIT 0, 100";
     $transactions = $wpdb->get_col($query);
     foreach ($transactions as $option_name) {
         $hash = end(explode('-', $option_name));
         $transaction_errors = array();
         $transaction = AWPCP_Payment_Transaction::find_by_id($hash);
         if (is_null($transaction)) {
             $transaction = new AWPCP_Payment_Transaction(array('id' => $hash));
         }
         $data = maybe_unserialize(get_option($option_name, null));
         // can't process this transaction, skip and delete old data
         if (!is_array($data)) {
             delete_option($option_name);
             continue;
         }
         $errors = awpcp_array_data('__errors__', array(), $data);
         $user_id = awpcp_array_data('user-id', null, $data);
         $amount = awpcp_array_data('amount', 0.0, $data);
         $items = awpcp_array_data('__items__', array(), $data);
         $created = awpcp_array_data('__created__', current_time('mysql'), $data);
         $updated = awpcp_array_data('__updated__', current_time('mysql'), $data);
         if ($type = awpcp_array_data('payment-term-type', false, $data)) {
             if (strcmp($type, 'ad-term-fee') === 0) {
                 $data['payment-term-type'] = 'fee';
             }
         }
         foreach ($data as $name => $value) {
             $transaction->set($name, $value);
         }
         foreach ($items as $item) {
             $transaction->add_item($item->id, $item->name, '', AWPCP_Payment_Transaction::PAYMENT_TYPE_MONEY, $amount);
             // at the time of this upgrade, only one item was supported.
             break;
         }
         if (awpcp_array_data('free', false, $data)) {
             $transaction->payment_status = AWPCP_Payment_Transaction::PAYMENT_STATUS_NOT_REQUIRED;
         }
         $totals = $transaction->get_totals();
         if ($totals['money'] === 0 || $transaction->get('payment-method', false) === '') {
             $transaction->payment_status = AWPCP_Payment_Transaction::PAYMENT_STATUS_NOT_REQUIRED;
         }
         if ($totals['money'] > 0 && $transaction->get('payment-method', false)) {
             $transaction->_set_status(AWPCP_Payment_Transaction::STATUS_PAYMENT);
         }
         if ($completed = awpcp_array_data('completed', null, $data)) {
             $transaction->completed = $completed;
             $transaction->payment_status = AWPCP_Payment_Transaction::PAYMENT_STATUS_COMPLETED;
             $transaction->_set_status(AWPCP_Payment_Transaction::STATUS_COMPLETED);
         }
         unset($data['__errors__']);
         unset($data['__items__']);
         unset($data['__created__']);
         unset($data['__updated__']);
         unset($data['user-id']);
         unset($data['completed']);
         unset($data['free']);
         $transaction->user_id = $user_id;
         $transaction->created = $created;
         $transaction->updated = $updated;
         $transaction->errors = $errors;
         $transaction->version = 1;
         // remove entries from wp_options table
         if ($transaction->save()) {
             delete_option($option_name);
         }
     }
     $remaining_transactions = $this->count_old_payment_transactions();
     // we are done here, let the plugin know so othrer upgrades
     // can be initiated or the plugin features can be enabled again.
     if ($remaining_transactions === 0) {
         delete_option('awpcp-import-payment-transactions');
         $this->update_pending_upgrades_status();
     }
     return $this->ajax_response($existing_transactions, $remaining_transactions);
 }