/**
  *
  * @param \GridField $gridField
  * @param \DataObject $record
  * @param string $columnName
  * @return string|null - the HTML for the column
  */
 public function getColumnContent($gridField, $record, $columnName)
 {
     if (!$record instanceof \Payment) {
         return null;
     }
     if (!$record->canCapture()) {
         return null;
     }
     \Requirements::css('omnipay-ui/css/omnipay-ui-cms.css');
     \Requirements::javascript('omnipay-ui/javascript/omnipay-ui-cms.js');
     \Requirements::add_i18n_javascript('omnipay-ui/javascript/lang');
     $infoText = '';
     switch (GatewayInfo::captureMode($record->Gateway)) {
         case GatewayInfo::MULTIPLE:
             $infoText = 'MultiCaptureInfo';
             break;
         case GatewayInfo::PARTIAL:
             $infoText = 'SingleCaptureInfo';
             break;
         case GatewayInfo::FULL:
             $infoText = 'FullCaptureInfo';
             break;
     }
     /** @var \Money $money */
     $money = $record->dbObject('Money');
     $money->setAmount($record->getMaxCaptureAmount());
     /** @var \GridField_FormAction $field */
     $field = \GridField_FormAction::create($gridField, 'CapturePayment' . $record->ID, false, 'capturepayment', array('RecordID' => $record->ID))->addExtraClass('gridfield-button-capture payment-dialog-button')->setAttribute('title', _t('GridFieldCaptureAction.Title', 'Capture Payment'))->setAttribute('data-icon', 'button-capture')->setAttribute('data-dialog', json_encode(array('maxAmount' => $money->Nice(), 'maxAmountNum' => $money->getAmount(), 'hasAmountField' => $record->canCapture(null, true), 'infoTextKey' => $infoText, 'buttonTextKey' => 'CaptureAmount')))->setDescription(_t('GridFieldCaptureAction.Description', 'Capture authorized Payment'));
     return $field->Field();
 }
 protected function markCompleted($endStatus, ServiceResponse $serviceResponse, $gatewayMessage)
 {
     // Get partial payments
     $partials = $this->payment->getPartialPayments()->filter('Status', $this->pendingState);
     if ($partials->count() > 0) {
         $i = 0;
         $total = $originalTotal = $this->payment->MoneyAmount;
         foreach ($partials as $payment) {
             // only the first, eg. most recent payment should be considered valid. All others should be set to void
             if ($i === 0) {
                 $total = PaymentMath::add($total, $payment->MoneyAmount);
                 // deal with partial capture
                 if ($payment->MoneyAmount < 0) {
                     $payment->Status = 'Created';
                     $payment->setAmount(PaymentMath::multiply($payment->MoneyAmount, '-1'));
                     $payment->Status = 'Captured';
                 } else {
                     // void excess amounts
                     $payment->Status = 'Void';
                 }
             } else {
                 $payment->Status = 'Void';
             }
             $payment->write();
             $i++;
         }
         // Ugly hack to set the money amount
         $this->payment->Status = 'Created';
         $this->payment->setAmount($total);
         // If not everything was captured (partial),
         // the payment should be refunded or still Authorized (in case multiple captures are possible)
         if ($total > 0 && $total < $originalTotal) {
             $endStatus = GatewayInfo::captureMode($this->payment->Gateway) === GatewayInfo::MULTIPLE ? 'Authorized' : 'Refunded';
         }
     }
     parent::markCompleted($endStatus, $serviceResponse, $gatewayMessage);
     if ($endStatus === 'Captured') {
         $this->createMessage('CapturedResponse', $gatewayMessage);
     } else {
         $this->createMessage('PartiallyCapturedResponse', $gatewayMessage);
     }
     Helper::safeExtend($this->payment, 'onCaptured', $serviceResponse);
 }
Ejemplo n.º 3
0
 /**
  * Whether or not this payment can be captured
  * @param Member|int $member the member/memberID to check permissions on
  * @param boolean $partial check if payment can be partially captured. Defaults to false
  * @return bool
  */
 public function canCapture($member = null, $partial = false)
 {
     // premise
     if (!($this->Status === 'Authorized' && ($partial ? GatewayInfo::allowPartialCapture($this->Gateway) : GatewayInfo::allowCapture($this->Gateway)))) {
         return false;
     }
     if ($this->isInDB()) {
         // Check if there are partial captures and deny further captures if multiple captures aren't enabled
         $hasPartials = $this->getPartialPayments()->filter('Status', array('Captured', 'PendingCapture'))->count() > 0;
         if ($hasPartials && GatewayInfo::captureMode($this->Gateway) !== GatewayInfo::MULTIPLE) {
             return false;
         }
     }
     $extended = $this->extendedCan('canCapture', $member);
     if ($extended !== null) {
         return $extended;
     }
     return Permission::check('CAPTURE_PAYMENTS', 'any', $member);
 }