diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index e30228fb..6b588efc 100644 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -27,6 +27,11 @@ abstract class AbstractGateway implements GatewayInterface */ protected $httpRequest; + /** + * @var \Omnipay\Common\Metadata\MetadataInterface The metadata instance. + */ + protected $metadata; + /** * Create a new gateway instance * @@ -37,6 +42,8 @@ public function __construct(ClientInterface $httpClient = null, HttpRequest $htt { $this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); $this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); + $this->metadata = $this->createMetadata(); + $this->initialize(); } @@ -48,9 +55,12 @@ public function getShortName() public function initialize(array $parameters = array()) { $this->parameters = new ParameterBag; + $this->registerParameters(); - // set default parameters - foreach ($this->getDefaultParameters() as $key => $value) { + // Set parameters + foreach ($this->getMetadata()->getParameters() as $parameter) { + $key = $parameter->getName(); + $value = $parameter->getDefaultValue(); if (is_array($value)) { $this->parameters->set($key, reset($value)); } else { @@ -214,4 +224,29 @@ protected function getDefaultHttpRequest() { return HttpRequest::createFromGlobals(); } + + /** + * Registers gateway parameters. + * This method is invoked during gateway initialization to + * register all available parameters that are exposed to + * the client that uses the gateway. + */ + abstract protected function registerParameters(); + + /** + * Creates the gateway metadata. + * This method is invoked during gateway initialization to + * register all available parameters that are exposed to + * the client that uses the gateway. + */ + abstract protected function createMetadata(); + + /** + * Returns the gateway metdata. + * @return \Omnipay\Common\Metadata\MetadataInterface The metadata instance. + */ + public function getMetadata() + { + return $this->metadata; + } } diff --git a/src/Omnipay/Common/GatewayInterface.php b/src/Omnipay/Common/GatewayInterface.php index 87bed4b2..93e09c2a 100644 --- a/src/Omnipay/Common/GatewayInterface.php +++ b/src/Omnipay/Common/GatewayInterface.php @@ -22,17 +22,6 @@ public function getName(); */ public function getShortName(); - /** - * Define gateway parameters, in the following format: - * - * array( - * 'username' => '', // string variable - * 'testMode' => false, // boolean variable - * 'landingPage' => array('billing', 'login'), // enum variable, first item is default - * ); - */ - public function getDefaultParameters(); - /** * Initialize gateway with parameters */ @@ -53,4 +42,10 @@ public function getParameters(); * @return \Omnipay\Common\Message\RequestInterface */ public function purchase(array $parameters = array()); + + /** + * Returns the gateway metdata. + * @return \Omnipay\Common\Metadata\MetadataInterface The metadata instance. + */ + public function getMetadata(); } diff --git a/src/Omnipay/Common/Metadata/MetadataAbstract.php b/src/Omnipay/Common/Metadata/MetadataAbstract.php new file mode 100644 index 00000000..133ca849 --- /dev/null +++ b/src/Omnipay/Common/Metadata/MetadataAbstract.php @@ -0,0 +1,46 @@ +paramaters = array(); + } + + /** + * Adds a new parameter. + * @param ParameterInterface $parameter The parameter instance. + */ + public function addParameter($parameter) + { + $class = new \ReflectionClass($parameter); + if($class->implementsInterface('Omnipay\Common\Parameter\ParameterInterface') === false) + throw new \RuntimeException('The paramater must implement Omnipay\Common\Parameter\ParameterInterface'); + + $this->paramaters[$parameter->getName()] = $parameter; + return $this; + } + + /** + * Returns the list of parameters. + * @return array The list of {@link ParameterInterface} instances. + */ + public function getParameters() + { + return $this->paramaters; + } +} \ No newline at end of file diff --git a/src/Omnipay/Common/Metadata/MetadataInterface.php b/src/Omnipay/Common/Metadata/MetadataInterface.php new file mode 100644 index 00000000..a026932f --- /dev/null +++ b/src/Omnipay/Common/Metadata/MetadataInterface.php @@ -0,0 +1,39 @@ +name = $name; + $this->allowedValues = array(); + } + + /** + * Returns the list of allowed values. + * @return array The list of allowed values. + */ + public function getAllowedValues() + { + return $this->allowedValues; + } + + /** + * Returns the default value. + * @return mixed The default value. + */ + public function getDefaultValue() + { + return $this->defaultValue; + } + + /** + * Returns the description. + * @return string The description. + */ + public function getDescription() + { + return $this->description; + } + + /** + * Returns the name. + * @return string The name. + */ + public function getName() + { + return $this->name; + } + + /** + * Returns the label. + * @return string The label. + */ + public function getLabel() + { + return $this->label; + } + + /** + * Checks if the paramter is required and + * its value cannot be empty. + * @return boolean True if the parameter is required or + * false otherwise. + */ + public function isRequired() + { + return $this->isRequired; + } + + /** + * Sets the list of allowed values. + * @param array $values The list of allowed values. + * @return Parameter The current instance. + */ + public function setAllowedValues($values) + { + $this->allowedValues = $values; + return $this; + } + + /** + * Sets the default value. + * @param mixed $value The default value. + * @return Parameter The current instance. + */ + public function setDefaultValue($value) + { + $this->defaultValue = $value; + return $this; + } + + /** + * Sets the description. + * @param string $value The description. + * @return Parameter The current instance. + */ + public function setDescription($value) + { + $this->description = $value; + return $this; + } + + /** + * Sets the name. + * @param string $value The name. + * @return Parameter The current instance. + */ + public function setName($value) + { + $this->id = $value; + return $this; + } + + /** + * Sets the label. + * @param string $value The label. + * @return Parameter The current instance. + */ + public function setLabel($value) + { + $this->label = $value; + return $this; + } + + /** + * Sets if the parameter is required. + * @param boolean $required True if required or false otherwise. + * @return Parameter The current instance. + */ + public function setIsRequired($required) + { + $this->isRequired = $required; + return $this; + } +} \ No newline at end of file diff --git a/src/Omnipay/Common/Parameter/ParameterInterface.php b/src/Omnipay/Common/Parameter/ParameterInterface.php new file mode 100644 index 00000000..11cb45cd --- /dev/null +++ b/src/Omnipay/Common/Parameter/ParameterInterface.php @@ -0,0 +1,89 @@ +getParameter('solutionType'); @@ -84,4 +75,38 @@ public function completePurchase(array $parameters = array()) { return $this->createRequest('\Omnipay\PayPal\Message\ExpressCompletePurchaseRequest', $parameters); } + + /** + * Registers gateway parameters. + */ + protected function registerParameters() + { + parent::registerParameters(); + + // solutionType parameter + $parameter = new Parameter('solutionType'); + $parameter->setLabel(_('Solution type')) + ->setDescription(_('The type of checkout flow')) + ->setDefaultValue('Sole') + ->setAllowedValues(array('Sole', 'Mark')) + ->setIsRequired(true); + $this->getMetadata()->addParameter($parameter); + + // landingPage parameter + $parameter = new Parameter('landingPage'); + $parameter->setLabel(_('Landing page')) + ->setDescription(_('The type of PayPal page to display')) + ->setDefaultValue('Billing') + ->setAllowedValues(array('Billing', 'Login')) + ->setIsRequired(true); + $this->getMetadata()->addParameter($parameter); + + // headerImageUrl parameter + $parameter = new Parameter('headerImageUrl'); + $parameter->setLabel(_('Landing page')) + ->setDescription(_('The URL for the image you want to appear at the top left of the payment page')) + ->setDefaultValue('') + ->setIsRequired(true); + $this->getMetadata()->addParameter($parameter); + } } diff --git a/src/Omnipay/PayPal/ExpressMetadata.php b/src/Omnipay/PayPal/ExpressMetadata.php new file mode 100644 index 00000000..9a302fed --- /dev/null +++ b/src/Omnipay/PayPal/ExpressMetadata.php @@ -0,0 +1,38 @@ +createRequest('\Omnipay\PayPal\Message\RefundRequest', $parameters); } + + /** + * Creates the gateway metadata. + * This method is invoked during gateway initialization to + * register all available parameters that are exposed to + * the client that uses the gateway. + */ + protected function createMetadata() + { + return new ProMetadata; + } + + /** + * Registers gateway parameters. + */ + protected function registerParameters() + { + // username parameter + $parameter = new Parameter('username'); + $parameter->setLabel(_('Username')) + ->setDescription(_('The PayPal username')) + ->setDefaultValue('') + ->setIsRequired(true); + $this->getMetadata()->addParameter($parameter); + + // password parameter + $parameter = new Parameter('password'); + $parameter->setLabel(_('Password')) + ->setDescription(_('The PayPal password')) + ->setDefaultValue('') + ->setIsRequired(true); + $this->getMetadata()->addParameter($parameter); + + // signature parameter + $parameter = new Parameter('signature'); + $parameter->setLabel(_('Signature')) + ->setDescription(_('The signature')) + ->setDefaultValue('') + ->setIsRequired(true); + $this->getMetadata()->addParameter($parameter); + + // testMode parameter + $parameter = new Parameter('testMode'); + $parameter->setLabel(_('Test mode')) + ->setDescription(_('Enable this option to test the gateway')) + ->setDefaultValue('') + ->setIsRequired(true); + } } diff --git a/src/Omnipay/PayPal/ProMetadata.php b/src/Omnipay/PayPal/ProMetadata.php new file mode 100644 index 00000000..7fe1e1ab --- /dev/null +++ b/src/Omnipay/PayPal/ProMetadata.php @@ -0,0 +1,38 @@ +