Beispiel #1
0
 /**
  * Entry point for the script
  *
  * @return  void
  *
  * @since   2.5
  */
 public function doExecute()
 {
     JFactory::getApplication('site');
     JPluginHelper::importPlugin('redcore');
     JPluginHelper::importPlugin('redpayment');
     // Set up statuses that are in their final stage
     $this->finalStatuses = array(RApiPaymentStatus::getStatusCompleted(), RApiPaymentStatus::getStatusCanceled_Reversal(), RApiPaymentStatus::getStatusDenied(), RApiPaymentStatus::getStatusExpired(), RApiPaymentStatus::getStatusRefunded(), RApiPaymentStatus::getStatusReversed());
     $this->out('============================');
     $this->out('Check Payments status change');
     $this->out('============================');
     $payments = $this->getPaymentsForChecking();
     $this->out('Number of payments for checking:' . count($payments));
     $this->out('============================');
     if (!empty($payments)) {
         foreach ($payments as $payment) {
             // Print out payment info
             $this->out('============================');
             $this->out(sprintf('Check for order name "%s" for extension "%s" using "%s" payment plugin:', $payment->order_name, $payment->extension_name, $payment->payment_name));
             $this->out('============================');
             // Preform check
             $status = RApiPaymentHelper::checkPayment($payment->id);
             // Print out status result
             foreach ($status as $statusKey => $message) {
                 if (is_array($message)) {
                     foreach ($status as $key => $value) {
                         $this->out($key . ': ' . $value);
                     }
                 } else {
                     $this->out($statusKey . ': ' . $message);
                 }
             }
             // Subtract retry count or reset it
             $paymentNew = RApiPaymentHelper::getPaymentById($payment->id);
             if (!in_array($paymentNew->status, $this->finalStatuses)) {
                 // We are still not done, we will subtract retry counter for this payment
                 $paymentNew->retry_counter -= 1;
                 RApiPaymentHelper::updatePaymentCounter($paymentNew->id, $paymentNew->retry_counter);
                 $this->out('Retry checks left: ' . $paymentNew->retry_counter);
             }
             $this->out('============================');
         }
     }
     $this->out('============================');
     $this->out('Done !');
 }
Beispiel #2
0
 /**
  * Update payment status
  *
  * @param   int  $paymentId  Id of the payment
  *
  * @return  mixed
  *
  * @since   1.5
  */
 public static function updatePaymentStatus($paymentId)
 {
     if (empty($paymentId)) {
         return false;
     }
     $db = JFactory::getDbo();
     $query = $db->getQuery(true)->select('pl.*')->from($db->qn('#__redcore_payment_log', 'pl'))->where('pl.payment_id = ' . (int) $paymentId)->order('pl.created_date ASC');
     $db->setQuery($query);
     $paymentLogs = $db->loadObjectList();
     if ($paymentLogs) {
         $paymentOriginal = self::getPaymentById($paymentId);
         $payment = JArrayHelper::fromObject($paymentOriginal);
         $customerNote = array();
         $amountPaid = 0;
         $currency = '';
         $status = RApiPaymentStatus::getStatusCreated();
         foreach ($paymentLogs as $paymentLog) {
             if ($paymentLog->status == RApiPaymentStatus::getStatusCompleted()) {
                 if (!empty($currency) && $currency != $paymentLog->currency) {
                     // We have a problem. Two different confirmed payments but in different currencies.
                     // We will only set latest payment data
                     $amountPaid = $paymentLog->amount;
                     $currency = $paymentLog->currency;
                 } else {
                     if ($payment['transaction_id'] != $paymentLog->transaction_id || $amountPaid == 0) {
                         $amountPaid += $paymentLog->amount;
                     }
                     if (!empty($paymentLog->currency)) {
                         $currency = $paymentLog->currency;
                     }
                 }
                 $payment['coupon_code'] = $paymentLog->coupon_code;
                 $payment['confirmed_date'] = $paymentLog->created_date;
             }
             if (!empty($paymentLog->transaction_id)) {
                 $payment['transaction_id'] = $paymentLog->transaction_id;
             }
             // We will take customer note from every log but not duplicates
             if (!empty($paymentLog->customer_note) && !in_array($paymentLog->customer_note, $customerNote)) {
                 $customerNote[] = $paymentLog->customer_note;
             }
             $status = RApiPaymentStatus::changeStatus($status, $paymentLog->status);
             // Handle statuses that subtract paid amount
             if (in_array($status, array(RApiPaymentStatus::getStatusRefunded(), RApiPaymentStatus::getStatusReversed()))) {
                 $amountPaid -= $paymentLog->amount;
                 if ($amountPaid < 0) {
                     $amountPaid = 0;
                 }
             }
         }
         $payment['amount_paid'] = $amountPaid;
         $payment['customer_note'] = implode("\r\n", $customerNote);
         if ($status == RApiPaymentStatus::getStatusCompleted() && $payment['amount_paid'] != $payment['amount_total']) {
             $status = RApiPaymentStatus::getStatusPending();
         }
         $payment['status'] = $status;
         if (empty($payment['currency'])) {
             $payment['currency'] = $currency;
         }
         // Currency should not be numeric
         if (!empty($payment['currency']) && is_numeric($payment['currency'])) {
             $payment['currency'] = RHelperCurrency::getIsoCode($payment['currency']);
         }
         if (!self::updatePaymentData($payment)) {
             return false;
         }
         // If we changed status we need to call extension helper file to trigger its update method
         if ($paymentOriginal->status != $payment['status']) {
             $paymentNew = self::getPaymentById($paymentId);
             self::triggerExtensionHelperMethod($paymentNew->extension_name, 'paymentStatusChanged', $paymentOriginal, $paymentNew);
         }
     }
     return true;
 }