示例#1
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;
 }
示例#2
0
 /**
  * find an existing card in the stripe system
  *
  * (non-PHPdoc)
  *
  * @see \PhalconRest\Libraries\Payments\Processor::findCustomer()
  */
 public function findCard($external_id, $force_api_call = false)
 {
     // simple validation
     if (strlen($external_id) < 5) {
         throw new \Exception('external_id is not long enough');
     }
     if (!strstr($external_id, 'card_')) {
         throw new \Exception('invalid stripe external_id');
     }
     // consult w/ cache first
     if (!$force_api_call) {
         if (isset($this->cachedCustomers[$external_id])) {
             return $this->cachedCustomers[$external_id];
         }
     }
     // either force is true or the cache missed, pull from api
     try {
         // load customer record in order to request related card record
         $card = \PhalconRest\Models\Cards::findFirst("external_id = '{$external_id}'");
         $account = $card->Accounts;
         $customer = $this->findCustomer($account->external_id);
         $card = $customer->sources->retrieve($external_id);
         if ($card and $card->delete != true) {
             $this->cachedCards[$card->id] = $card;
             return $card;
         }
     } catch (\Stripe\Error\Base $e) {
         $this->handleStripeError($e);
     }
     // if a customer record is not found, return false
     return false;
 }
示例#3
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];
 }