Ejemplo n.º 1
0
 public function storeCreditCard(CcRecord $cc, Am_Paysystem_Result $result)
 {
     $user = $this->getDi()->userTable->load($cc->user_id);
     $profileId = $user->data()->get(Am_Paysystem_AuthorizeCim::USER_PROFILE_KEY);
     if ($this->invoice) {
         // to link log records with current invoice
         $invoice = $this->invoice;
     } else {
         // updating credit card info?
         $invoice = $this->getDi()->invoiceRecord;
         $invoice->invoice_id = 0;
         $invoice->user_id = $user->pk();
     }
     // compare stored cc for that user may be we don't need to refresh?
     if ($profileId && $cc->cc_number != '0000000000000000') {
         $storedCc = $this->getDi()->ccRecordTable->findFirstByUserId($user->pk());
         if ($storedCc && ($storedCc->cc != $cc->maskCc($cc->cc_number) || $storedCc->cc_expire != $cc->cc_expire)) {
             $tr = new Am_Paysystem_Transaction_AuthorizeCim_UpdateCustomerPaymentProfile($this, $invoice, $cc);
             $tr->run($result);
             if ($result->isFailure()) {
                 // Try to delete all profiles and create new one.
                 $result->reset();
                 $user->data()->set(self::USER_PROFILE_KEY, null)->set(self::PAYMENT_PROFILE_KEY, null)->update();
                 $deleteTr = new Am_Paysystem_Transaction_AuthorizeCim_DeleteCustomerProfile($this, $invoice, $profileId);
                 $deleteTr->run($res = new Am_Paysystem_Result());
                 $profileId = null;
             }
         }
     }
     if (!$profileId) {
         try {
             $tr = new Am_Paysystem_Transaction_AuthorizeCim_CreateCustomerProfile($this, $invoice, $cc);
             $tr->run($result);
             if (!$result->isSuccess()) {
                 if ($tr->getErrorCode() == 'E00039') {
                     $error = $result->getLastError();
                     if (preg_match('/A duplicate record with ID (\\d+) already exists/', $error, $regs) && $regs[1]) {
                         $user->data()->set(Am_Paysystem_AuthorizeCim::USER_PROFILE_KEY, $regs[1])->update();
                         $result->reset();
                         $result->setSuccess();
                     } else {
                         return;
                     }
                 } else {
                     return;
                 }
             } else {
                 $user->data()->set(Am_Paysystem_AuthorizeCim::USER_PROFILE_KEY, $tr->getProfileId())->update();
                 $user->data()->set(Am_Paysystem_AuthorizeCim::PAYMENT_PROFILE_KEY, $tr->getPaymentId())->update();
             }
         } catch (Am_Exception_Paysystem $e) {
             $result->setFailed($e->getPublicError());
             return false;
         }
     }
     $paymentProfileId = $user->data()->get(Am_Paysystem_AuthorizeCim::PAYMENT_PROFILE_KEY);
     if (!$paymentProfileId) {
         try {
             $tr = new Am_Paysystem_Transaction_AuthorizeCim_CreateCustomerPaymentProfile($this, $invoice, $cc);
             $tr->run($result);
             if (!$result->isSuccess()) {
                 return;
             }
             $user->data()->set(Am_Paysystem_AuthorizeCim::PAYMENT_PROFILE_KEY, $tr->getProfileId())->update();
         } catch (Am_Exception_Paysystem $e) {
             $result->setFailed($e->getPublicError());
             return false;
         }
     }
     ///
     $cc->cc = $cc->maskCc(@$cc->cc_number);
     $cc->cc_number = '0000000000000000';
     if ($cc->pk()) {
         $cc->update();
     } else {
         $cc->replace();
     }
     $result->setSuccess();
 }