From ed2d5d09d01e76c52865f126d9d56eff2b16086e Mon Sep 17 00:00:00 2001 From: akDeveloper Date: Mon, 11 Apr 2016 18:39:44 +0300 Subject: [PATCH] adding SimpleXmlBuilder. --- .../Billing/Gateways/Centinel.php | 63 ++++++------ .../Billing/Gateways/Modirum.php | 71 ++++++++------ .../Common/SimpleXmlBuilder.php | 97 +++++++++++++++++++ lib/AktiveMerchant/Common/XmlBuilder.php | 6 -- .../Billing/Gateways/AlphaBankTest.php | 2 + .../Billing/Gateways/CentinelTest.php | 74 +++++++------- tests/AktiveMerchant/TestCase.php | 3 +- 7 files changed, 212 insertions(+), 104 deletions(-) create mode 100644 lib/AktiveMerchant/Common/SimpleXmlBuilder.php diff --git a/lib/AktiveMerchant/Billing/Gateways/Centinel.php b/lib/AktiveMerchant/Billing/Gateways/Centinel.php index 4e4c49e..3b56202 100644 --- a/lib/AktiveMerchant/Billing/Gateways/Centinel.php +++ b/lib/AktiveMerchant/Billing/Gateways/Centinel.php @@ -8,7 +8,7 @@ use AktiveMerchant\Billing\CreditCard; use AktiveMerchant\Billing\Gateways\Centinel\CentinelResponse; use AktiveMerchant\Common\Options; -use AktiveMerchant\Common\XmlBuilder; +use AktiveMerchant\Common\SimpleXmlBuilder; /** * Integration of Centinel gateway @@ -77,11 +77,10 @@ public function lookup($money, CreditCard $creditcard, $options = array()) Options::required('order_id', $options); $options = new Options($options); - $this->buildXml(static::LOOKUP, function ($xml) use ($money, $creditcard, $options) { - $this->addInvoice($money, $options, $xml); - $this->addCreditcard($creditcard, $xml); - $options['description'] and $this->addOrderDescription($options['description'], $xml); - }); + $this->buildXml(static::LOOKUP); + $this->addInvoice($money, $options); + $this->addCreditcard($creditcard); + $options['description'] and $this->addOrderDescription($options['description']); return $this->commit(static::LOOKUP, $money, $options); @@ -92,63 +91,59 @@ public function authenticate($options = array()) Options::required('payload, transaction_id', $options); $options = new Options($options); - $this->buildXml(static::AUTHENTICATE, function ($xml) use ($options) { - $this->addCmpiLookupData($options, $xml); - }); + $this->buildXml(static::AUTHENTICATE); + $this->addCmpiLookupData($options); return $this->commit(static::AUTHENTICATE, null, $options); } /* Private */ - private function buildXml($action, $block) + private function buildXml($action) { - $this->xml = new XmlBuilder(); - $this->xml->instruct('1.0', 'UTF-8'); - $this->xml->CardinalMPI(function ($xml) use ($action, $block) { - $xml->MsgType($action); - $xml->Version(static::VERSION); - $xml->ProcessorId($this->options['processor_id']); - $xml->MerchantId($this->options['login']); - $xml->TransactionPwd($this->options['password']); - $xml->TransactionType('C'); - $block($xml); - }); + $this->xml = new SimpleXmlBuilder('1.0', 'UTF-8'); + $this->xml->CardinalMPI(); + $this->xml->MsgType($action); + $this->xml->Version(static::VERSION); + $this->xml->ProcessorId($this->options['processor_id']); + $this->xml->MerchantId($this->options['login']); + $this->xml->TransactionPwd($this->options['password']); + $this->xml->TransactionType('C'); } - private function addCmpiLookupData($options, $xml) + private function addCmpiLookupData($options) { - $xml->TransactionId($options['transaction_id']); - $xml->PAResPayload($options['payload']); + $this->xml->TransactionId($options['transaction_id']); + $this->xml->PAResPayload($options['payload']); } private function addOrderDescription($description, $xml) { - $xml->OrderDescription($description); + $this->xml->OrderDescription($description); } - private function addInvoice($money, $options, $xml) + private function addInvoice($money, $options) { $order_number = isset($options['order_id']) ? $options['order_id'] : null; $amount = $this->isTest() ? $this->amount("1") : $this->amount($money); $default_currency = static::$default_currency; - $xml->OrderNumber($order_number); - $xml->CurrencyCode($this->currency_lookup($default_currency)); - $xml->Amount($amount); + $this->xml->OrderNumber($order_number); + $this->xml->CurrencyCode($this->currency_lookup($default_currency)); + $this->xml->Amount($amount); if ($options['installment']) { - $xml->Installment($options['installment']); + $this->xml->Installment($options['installment']); } } - private function addCreditcard(CreditCard $creditcard, $xml) + private function addCreditcard(CreditCard $creditcard) { $month = $this->cc_format($creditcard->month, 'two_digits'); $year = $this->cc_format($creditcard->year, 'four_digits'); - $xml->CardNumber($creditcard->number); - $xml->CardExpMonth($month); - $xml->CardExpYear($year); + $this->xml->CardNumber($creditcard->number); + $this->xml->CardExpMonth($month); + $this->xml->CardExpYear($year); } private function parse($body) diff --git a/lib/AktiveMerchant/Billing/Gateways/Modirum.php b/lib/AktiveMerchant/Billing/Gateways/Modirum.php index 47b8d67..0456933 100644 --- a/lib/AktiveMerchant/Billing/Gateways/Modirum.php +++ b/lib/AktiveMerchant/Billing/Gateways/Modirum.php @@ -133,9 +133,9 @@ public function authorize($money, CreditCard $creditcard, $options = array()) $options = new Options($options); $this->buildXml(static::AUTHORIZE, $options, function ($xml) use ($money, $creditcard, $options) { - $this->addInvoice($money, $options, $xml); - $this->addCreditcard($creditcard, $options, $xml); - $this->addThreedSecure($options, $xml); + $this->addInvoice($money, $options, static::AUTHORIZE); + $this->addCreditcard($creditcard, $options, static::AUTHORIZE); + $this->addThreedSecure($options, static::AUTHORIZE); }); return $this->commit(static::AUTHORIZE, $money); @@ -148,11 +148,10 @@ public function purchase($money, CreditCard $creditcard, $options = array()) { $options = new Options($options); - $this->buildXml(static::SALE, $options, function ($xml) use ($money, $creditcard, $options) { - $this->addInvoice($money, $options, $xml); - $this->addCreditcard($creditcard, $options, $xml); - $this->addThreedSecure($options, $xml); - }); + $this->buildXml(static::SALE); + $this->addInvoice($money, $options, $xml); + $this->addCreditcard($creditcard, $options, $xml); + $this->addThreedSecure($options, $xml); #$this->add_address($options); @@ -235,14 +234,10 @@ protected function buildXml($action, $options, $block) $action = $action . 'Request'; $messageId = md5(uniqid($options['order_id'], true)); $this->xml = new XmlBuilder(); - $this->xml->Message(function ($xml) use ($action, $block) { - $xml->$action(function ($xml) use ($block) { - $xml->Authentication(function ($xml) { - $xml->Mid($this->options['merchant_id']); - }); - $block($xml); - }); - }, array('lang' => 'en', 'messageId' => $messageId, 'version' => '1.0')); + $this->xml->Message(null, null, array('lang' => 'en', 'messageId' => $messageId, 'version' => '1.0')); + $this->xml->$action(null, 'Message'); + $this->xml->Authentication(null, $action); + $this->xml->Mid($this->options['merchant_id'], 'Authentication'); } /** @@ -250,18 +245,18 @@ protected function buildXml($action, $options, $block) * * @param array $options */ - private function addInvoice($money, $options, $xml) + private function addInvoice($money, $options, $action) { - $xml->OrderInfo(function ($xml) use ($money, $options) { - $xml->OrderId($options['order_id']); - $xml->OrderDesc($options['order_id']); - $xml->OrderAmount($this->amount($money)); - $xml->Currency(static::$default_currency); - $xml->PayerEmail(""); - if (true == $options['moto']) { - $xml->MOTO(1); - } - }); + $xml = $this->xml; + $xml->OrderInfo(null, $action . 'Request'); + $xml->OrderId($options['order_id'], 'OrderInfo'); + $xml->OrderDesc($options['order_id'], 'OrderInfo'); + $xml->OrderAmount($this->amount($money), 'OrderInfo'); + $xml->Currency(static::$default_currency, 'OrderInfo'); + $xml->PayerEmail("", 'OrderInfi'); + if (true == $options['moto']) { + $xml->MOTO(1, 'OrderInfo'); + } } /** @@ -269,8 +264,28 @@ private function addInvoice($money, $options, $xml) * * @param CreditCard $creditcard */ - private function addCreditcard(CreditCard $creditcard, $options, $xml) + private function addCreditcard(CreditCard $creditcard, $options, $action) { + $xml = $this->xml; + $year = $this->cc_format($creditcard->year, 'two_digits'); + $month = $this->cc_format($creditcard->month, 'two_digits'); + + $xml->PaymentInfo(null, $action . 'Request') + ->PayMethod( + $this->CARD_MAPPINGS[CreditCard::type($creditcard->number)], + 'PaymentInfo' + ) + ->CardPan($creditcard->number, 'PaymentInfo') + ->CardExpDate("{$year}{$month}", 'PaymentInfo') + ->CardCvv2($creditcard->verification_value, 'PaymentInfo') + ->CardHolderName(trim($creditcard->name()), 'PaymentInfo'); + + if ($options['installments']) { + $xml->InstallmentParameters(null, 'PayentInfo') + ->ExtInstallmentoffset(0, 'InstallmentParameters') + ->ExtInstallmentperiod($options['installments'], 'InstallmentParameters'); + } + return; $xml->PaymentInfo(function ($xml) use ($creditcard, $options) { $xml->PayMethod($this->CARD_MAPPINGS[CreditCard::type($creditcard->number)]); $xml->CardPan($creditcard->number); diff --git a/lib/AktiveMerchant/Common/SimpleXmlBuilder.php b/lib/AktiveMerchant/Common/SimpleXmlBuilder.php new file mode 100644 index 0000000..7080eb6 --- /dev/null +++ b/lib/AktiveMerchant/Common/SimpleXmlBuilder.php @@ -0,0 +1,97 @@ + + * @license MIT {@link http://opensource.org/licenses/mit-license.php} + */ +class SimpleXmlBuilder +{ + private $root; + + private $version; + + private $encoding; + + /** + * @param string $version Default '1.0' + * @param string $encoding Default 'UTF-8' + */ + public function __construct($version = '1.0', $encoding = 'UTF-8') + { + $this->version = $version; + $this->encoding = $encoding; + } + + /** + * @param string $name The name of element + * @param array $args An array with: + * - (string) value of element + * - (string) node element to add this element as child. + * - (array) attributes of element + * + * @return XmlBuilder + */ + public function __call($name, $args) + { + $value = array_shift($args); + $parentNode = array_shift($args); + $attrs = array_shift($args) ?: array(); + + if (null === $this->root) { + $this->createRootNode($name, $attrs); + + return $this; + } + + $this->createNode($name, $value, $attrs, $parentNode); + + return $this; + } + + private function createNode($name, $value, array $attrs = array(), $parentNode = null) + { + if ($parentNode) { + $node = $this->xpath('//' . $parentNode)->addChild($name, $value); + } else { + $node = $this->root->addChild($name, $value); + } + $this->addAttributes($node, $attrs); + } + + private function createRootNode($tag, array $attrs = array()) + { + $root = '<%s>'; + $string = sprintf($root, $this->version, $this->encoding, $tag, $tag); + $this->root = new SimpleXmlElement($string); + $this->addAttributes($this->root, $attrs); + } + + private function addAttributes($node, array $attrs) + { + foreach ($attrs as $name => $value) { + $node->addAttribute($name, $value); + } + } + + private function xpath($path) + { + $results = $this->root->xpath($path); + + return reset($results); + } + + public function __toString() + { + return $this->root->asXML(); + } +} diff --git a/lib/AktiveMerchant/Common/XmlBuilder.php b/lib/AktiveMerchant/Common/XmlBuilder.php index 7fd71e1..e1f6038 100644 --- a/lib/AktiveMerchant/Common/XmlBuilder.php +++ b/lib/AktiveMerchant/Common/XmlBuilder.php @@ -39,12 +39,6 @@ public function docType($qualifiedName, $publicId = null, $systemId = null) $this->writer->endDTD(); } - public function docType($qualifiedName, $publicId = null, $systemId = null) - { - $this->writer->startDTD($qualifiedName, $publicId, $systemId); - $this->writer->endDTD(); - } - public function build() { $args = func_get_args(); diff --git a/tests/AktiveMerchant/Billing/Gateways/AlphaBankTest.php b/tests/AktiveMerchant/Billing/Gateways/AlphaBankTest.php index 4817974..fcd639f 100644 --- a/tests/AktiveMerchant/Billing/Gateways/AlphaBankTest.php +++ b/tests/AktiveMerchant/Billing/Gateways/AlphaBankTest.php @@ -59,6 +59,8 @@ public function testPurchase() $this->options ); + echo $this->request->getBody(); + $this->assert_success($response); $this->assertTrue($response->test()); diff --git a/tests/AktiveMerchant/Billing/Gateways/CentinelTest.php b/tests/AktiveMerchant/Billing/Gateways/CentinelTest.php index 6df884e..5d8637e 100644 --- a/tests/AktiveMerchant/Billing/Gateways/CentinelTest.php +++ b/tests/AktiveMerchant/Billing/Gateways/CentinelTest.php @@ -2,12 +2,13 @@ /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ -use AktiveMerchant\Billing\Gateways\Centinel; +namespace AktiveMerchant\Billing\Gateways; + use AktiveMerchant\Billing\Base; use AktiveMerchant\Billing\CreditCard; -use AktiveMerchant\Event\RequestEvents; +use AktiveMerchant\TestCase; -class CentinelTest extends \AktiveMerchant\TestCase +class CentinelTest extends TestCase { public $gateway; @@ -58,59 +59,64 @@ public function testInitialization() $this->assertNotNull($this->creditcard); } - function testLookup() + public function testLookup() { - $this->mock_request($this->successful_lookup_response()); + $this->mock_request($this->successfulLookupResponse()); - /* - $this->gateway->addListener(RequestEvents::POST_SEND, function($event){ - var_dump(str_replace('cmpi_msg=', null, urldecode($event->getRequest()->getBody()))); - var_dump($event->getRequest()->getResponseBody()); - });*/ $auth = $this->gateway->lookup( $this->amount, $this->creditcard, $this->options ); + $request_body = $this->getRequestBody(); + $this->assertEquals( + $this->successfulLookupRequest(), + $request_body + ); + $this->assertTrue($auth->success()); } - function testAuthenticate() + public function testAuthenticate() { - $this->mock_request($this->successful_authenticate_response()); + $this->mock_request($this->successfulAuthenticateResponse()); $auth = $this->gateway->authenticate($this->options); + $request_body = $this->getRequestBody(); + $this->assertEquals( + $this->successfulAuthenticateRequest(), + $request_body + ); + $this->assertTrue($auth->success()); } - private function successful_lookup_response() + private function successfulLookupRequest() { - return ' -C -0 - -75f986t76f6 -2584 -eNpVUk1TwjAQ/SsM402nSUuKwSC/3gSoH5PL -Y -https://www.somewebsite.com/acs -07 -'; + return ' +cmpi_lookup1.7zxyC1234569781005105105105105100112017'; } - private function successful_authenticate_response() + private function successfulAuthenticateRequest() { - return ' - -0 -Y -Y -AAAAAAAAAAAAAAAAAAAAAAAAA= -05 -k4Vf36ijnJX54kwHQNqUr8/ruvs= -'; + return ' +cmpi_authenticate1.7zxyC78910payload'; } + private function successfulLookupResponse() + { + return ' C 0 75f986t76f6 2584 eNpVUk1TwjAQ/SsM402nSUuKwSC/3gSoH5PL Y https://www.somewebsite.com/acs 07 '; + } + + private function successfulAuthenticateResponse() + { + return ' 0 Y Y AAAAAAAAAAAAAAAAAAAAAAAAA= 05 k4Vf36ijnJX54kwHQNqUr8/ruvs= '; + } + + private function getRequestBody() + { + return str_replace('cmpi_msg=', null, urldecode($this->request->getBody())); + } } diff --git a/tests/AktiveMerchant/TestCase.php b/tests/AktiveMerchant/TestCase.php index b55f305..abe6e6e 100644 --- a/tests/AktiveMerchant/TestCase.php +++ b/tests/AktiveMerchant/TestCase.php @@ -31,8 +31,7 @@ protected function mock_request($answer) array('getResponseBody') ); - $this->request->expects($this->once()) - ->method('getResponseBody') + $this->request->method('getResponseBody') ->will($this->returnValue($answer)); $this->gateway->setRequest($this->request);