function validateCreditcardHTTPInput($creditcard, $contentObjectAttribute) { if (!CreditCard::validate($creditcard)) { $contentObjectAttribute->setValidationError(ezpI18n::tr('kernel/classes/datatypes', 'The Credit card number is not valid.')); return eZInputValidator::STATE_INVALID; } return eZInputValidator::STATE_ACCEPTED; }
/** * Test that the allowed card types option is working properly * * @param array $allowedCardTypes * @param string $cardNumber * @param bool $isValid */ public function testAllowedCardTypes() { $validVisa = "4111111111111111"; $validMastercard = "5555555555554444"; $validAmex = "340000000000009"; $validator = new CreditCard(); // Test Allowed VISA only $validator->setAllowedCardTypes(CreditCard::VISA); $this->assertEquals(true, $validator->validate($validVisa), "Visa card should validate okay"); $this->assertEquals(false, $validator->validate($validMastercard), "Mastercard is not in allowed card types"); $this->assertEquals(false, $validator->validate($validAmex), "Amex is not in allowed card types"); // Test Allowed Mastercard Only $validator->setAllowedCardTypes(CreditCard::MASTERCARD); $this->assertEquals(false, $validator->validate($validVisa), "Visa card is not in allowed card types"); $this->assertEquals(true, $validator->validate($validMastercard), "Mastercard should validate okay"); $this->assertEquals(false, $validator->validate($validAmex), "Amex is not in allowed card types"); // Test Allowed Amex Only $validator->setAllowedCardTypes(CreditCard::AMERICAN_EXPRESS); $this->assertEquals(false, $validator->validate($validVisa), "Visa card is not in allowed card types"); $this->assertEquals(false, $validator->validate($validMastercard), "Mastercard is not in allowed card types"); $this->assertEquals(true, $validator->validate($validAmex), "Amex should validate okay"); // Test Visa and Mastercard using bit operations $validator->setAllowedCardTypes(CreditCard::VISA | CreditCard::MASTERCARD); $this->assertEquals(true, $validator->validate($validVisa), "Visa should validate okay"); $this->assertEquals(true, $validator->validate($validMastercard), "Mastercard should validate okay"); $this->assertEquals(false, $validator->validate($validAmex), "Amex is not in allowed card types"); // Test Mastercard and Amex using an array $validator->setAllowedCardTypes(array(CreditCard::MASTERCARD, CreditCard::AMERICAN_EXPRESS)); $this->assertEquals(false, $validator->validate($validVisa), "Visa is not in allowed card type"); $this->assertEquals(true, $validator->validate($validMastercard), "Mastercard should validate okay"); $this->assertEquals(true, $validator->validate($validAmex), "Amex should validate okay"); // Check ALL $validator->setAllowedCardTypes(CreditCard::ALL); $this->assertEquals(true, $validator->validate($validVisa), "Visa should validate okay"); $this->assertEquals(true, $validator->validate($validMastercard), "Mastercard should validate okay"); $this->assertEquals(true, $validator->validate($validAmex), "Amex should validate okay"); }
/** * Validate the payment data against the gateway-specific requirements * * @param Array $data * @return ValidationResult */ public function validate($data) { $validationResult = $this->getValidationResult(); if (!isset($data['Amount'])) { $validationResult->error('Payment amount not set'); } else { if (empty($data['Amount'])) { $validationResult->error('Payment amount cannot be null'); } } if (!isset($data['Currency'])) { $validationResult->error('Payment currency not set'); } else { if (empty($data['Currency'])) { $validationResult->error('Payment currency cannot be null'); } else { if (!array_key_exists($data['Currency'], $this->getSupportedCurrencies())) { $validationResult->error('Currency ' . $data['Currency'] . ' not supported by this gateway'); } } } if (isset($data['CardNumber'])) { $options = array('firstName' => $data['FirstName'], 'lastName' => $data['LastName'], 'month' => $data['MonthExpiry'], 'year' => $data['YearExpiry'], 'type' => $data['CreditCardType']); if (is_array($data['CardNumber'])) { $options['number'] = implode('', $data['CardNumber']); } else { $options['number'] = $data['CardNumber']; } $cc = new CreditCard($options); $validationResult->combineAnd($cc->validate()); } $this->validationResult = $validationResult; return $validationResult; }
public function testEmptyCreditCardAsInvalid() { $this->assertEquals(CreditCard::validate(''), false); $this->assertEquals(CreditCard::validate(' '), false); }