示例#1
0
 /**
  * before a new card is saved, store it in the remote card processor
  * if adding the card fails, then do not save to server
  *
  * (non-PHPdoc)
  *
  * @see \PhalconRest\API\Entity::afterSave()
  */
 function beforeSave($object, $id)
 {
     $processor = $this->getDI()->get('paymentProcessor');
     $account = \PhalconRest\Models\Accounts::findFirst($object->account_id);
     if (!$account->external_id or $account->external_id == null) {
         $accountExternalId = $processor->createCustomer($account);
     } else {
         $accountExternalId = $account->external_id;
     }
     // run credit card through a series of validation tests
     $result = CreditCard::validCreditCard($object->number, $object->vendor);
     if ($result['valid'] == false) {
         throw new ValidationException("Bad Credit Card Supplied", ['dev' => "Bad card number supplied:  {$object->number}", 'code' => '5846846848644984'], ['number' => 'The supplied credit card number is invalid.']);
     } else {
         $object->number = $result['number'];
     }
     $result = CreditCard::validDate($object->expiration_year, $object->expiration_month);
     if ($result == false) {
         throw new ValidationException("Bad Expiration Date Supplied", ['dev' => "Bad expiration month or year:  {$object->expiration_month} | {$object->expiration_year}", 'code' => '81618161684684'], ['expiration_month' => 'The supplied expiration month is invalid.', 'expiration_year' => 'The supplied expiration year is invalid.']);
     }
     // put this in until we better populate the credit card form
     // TODO fix CVC in app
     // $object->cvc = '123';
     $object->external_id = $processor->createCard($accountExternalId, $object);
     // clear out data we do NOT want to store
     $object->number = substr($object->number, strlen($object->number) - 4, 4);
     unset($object->cvc);
     return $object;
 }
示例#2
0
 /**
  * before a new payment, process credit card payments with 3rd party processor
  * if adding a credit card payment is detected and fails, do not save the payment
  *
  * (non-PHPdoc)
  *
  * @see \PhalconRest\API\Entity::beforeSave()
  */
 function beforeSave($object, $id)
 {
     if ($object->mode == 'credit') {
         $processor = $this->getDI()->get('paymentProcessor');
         if ($object->card_id > 0) {
             $card = \PhalconRest\Models\Cards::findFirst($object->card_id);
             $account = \PhalconRest\Models\Accounts::findFirst($object->account_id);
             if (!$card->external_id or $card->external_id == null) {
                 // error, need a valid external_id in order to process the credit card
                 // consider adding the card on the fly?
                 throw new \Exception('Selected card does not have enough information to process.');
             }
             $object->external_id = $processor->chargeCard(['card_id' => $card->external_id, 'amount' => $object->amount, 'account_id' => $account->external_id]);
         } else {
             // must be a new card to charge
             $object->external_id = $processor->chargeCard((array) $object);
         }
     }
     if ($object->mode == 'refund' and isset($id)) {
         // see if the save is going FROM card to refund and apply refund logic
         $payment = \PhalconRest\Models\Payments::findFirst($id);
         if ($payment->mode == 'credit') {
             $processor = $this->getDI()->get('paymentProcessor');
             $refund_id = $processor->refundCharge(['charge_id' => $object->external_id]);
             $object->refund_id = $refund_id;
             $object->refunded_on = date('Y-m-d');
         }
     }
     return $object;
 }
示例#3
0
 /**
  * create a card holder account on the stripe system
  *
  * (non-PHPdoc)
  *
  * @see \PhalconRest\Libraries\Payments\Processor::createCustomer()
  */
 public function createCustomer(\PhalconRest\Models\Accounts $account)
 {
     // check that this customer doesn't already exist
     // skip cache to be sure the latest record is pulled
     if ($account->external_id) {
         $customer = $this->findCustomer($account->external_id);
         // match found, no need to create a new customer record
         if ($customer) {
             $this->cachedCustomers[$account->external_id] = $customer;
             return $account->external_id;
         }
     }
     try {
         $result = \Stripe\Customer::create(array("description" => $account->id));
     } catch (\Stripe\Error\Base $e) {
         $this->handleStripeError($e);
     }
     $account->external_id = $result->id;
     if (!$account->save()) {
         throw new HTTPException("Could not save Payment Information for account", 404, array('code' => 1872391762862, 'dev' => 'StripeAdapter->createCustomer failed to save external_id: ' . $result->id), $account->getMessages());
         return false;
     }
     return $result->id;
 }
示例#4
0
 /**
  * a simple point of entry to test out logic in smores
  * placed here since it is a non-secure controller and route
  */
 public function scratch1()
 {
     $processor = $this->getDI()->get('paymentProcessor');
     $account = \PhalconRest\Models\Accounts::findFirst(103);
     $accountExternalId = $processor->createCustomer($account);
     $card = \PhalconRest\Models\Cards::findFirst(4);
     $cardExternalId = $processor->createCard($accountExternalId, $card, '4242424242424242', '123');
     return ['accountExternalId' => $accountExternalId, 'cardExternalId' => $cardExternalId];
 }