Skip to content

Commit

Permalink
adding SimpleXmlBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
akDeveloper committed Apr 11, 2016
1 parent b2d2a03 commit ed2d5d0
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 104 deletions.
63 changes: 29 additions & 34 deletions lib/AktiveMerchant/Billing/Gateways/Centinel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
71 changes: 43 additions & 28 deletions lib/AktiveMerchant/Billing/Gateways/Modirum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -235,42 +234,58 @@ 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');
}

/**
* Adds invoice info if exists.
*
* @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');
}
}

/**
* Adds a CreditCard object
*
* @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);
Expand Down
97 changes: 97 additions & 0 deletions lib/AktiveMerchant/Common/SimpleXmlBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

namespace AktiveMerchant\Common;

use SimpleXMLElement;

/**
* XmlBuilder provides a fluent way to create xml strings.
*
* @since Class available since Release 1.1.0
* @package Aktive-Merchant
* @author Andreas Kollaros <[email protected]>
* @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 = '<?xml version="%s" encoding="%s"?><%s></%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();
}
}
6 changes: 0 additions & 6 deletions lib/AktiveMerchant/Common/XmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions tests/AktiveMerchant/Billing/Gateways/AlphaBankTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function testPurchase()
$this->options
);

echo $this->request->getBody();

$this->assert_success($response);
$this->assertTrue($response->test());

Expand Down
Loading

0 comments on commit ed2d5d0

Please sign in to comment.