/** * 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; }
/** * 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; }
/** * 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]; }