Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+Payment, PaymentTerm, PaymentService #41

Merged
merged 26 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/CXml/Model/Classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use JMS\Serializer\Annotation as Serializer;

#[Serializer\AccessorOrder(order: 'custom', custom: ['value'])]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you deleted this serializer property?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the need?

readonly class Classification
{
public function __construct(
Expand Down
22 changes: 22 additions & 0 deletions src/CXml/Model/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace CXml\Model;

use JMS\Serializer\Annotation as Serializer;

class Payment
{
public function __construct(
#[Serializer\XmlElement]
#[Serializer\SerializedName('PaymentService')]
private PaymentService $paymentService,
) {
}

public function getPaymentService(): PaymentService
{
return $this->paymentService;
}
}
31 changes: 31 additions & 0 deletions src/CXml/Model/PaymentService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace CXml\Model;

use JMS\Serializer\Annotation as Serializer;

class PaymentService
{
use IdReferencesTrait;
use ExtrinsicsTrait;

public function __construct(
#[Serializer\XmlAttribute]
readonly private string $method,
#[Serializer\XmlAttribute]
readonly private ?string $provider = null,
) {
}

public function getMethod(): string
{
return $this->method;
}

public function getProvider(): ?string
{
return $this->provider;
}
}
18 changes: 18 additions & 0 deletions src/CXml/Model/PaymentTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace CXml\Model;

use JMS\Serializer\Annotation as Serializer;

class PaymentTerm
{
use ExtrinsicsTrait;

public function __construct(
#[Serializer\XmlAttribute]
private int $payInNumberOfDays,
) {
}
}
32 changes: 31 additions & 1 deletion src/CXml/Model/Request/OrderRequestHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
use CXml\Model\ExtrinsicsTrait;
use CXml\Model\IdReferencesTrait;
use CXml\Model\MoneyWrapper;
use CXml\Model\Payment;
use CXml\Model\PaymentTerm;
use CXml\Model\Shipping;
use CXml\Model\ShipTo;
use CXml\Model\SupplierOrderInfo;
use CXml\Model\Tax;
use DateTimeInterface;
use JMS\Serializer\Annotation as Serializer;

#[Serializer\AccessorOrder(order: 'custom', custom: ['total', 'shipTo', 'billTo', 'businessPartners', 'shipping', 'tax', 'contacts', 'comments', 'supplierOrderInfo', 'idReferences', 'extrinsics'])]
#[Serializer\AccessorOrder(order: 'custom', custom: ['total', 'shipTo', 'billTo', 'businessPartners', 'shipping', 'tax', 'payment', 'paymentTerm', 'contacts', 'comments', 'supplierOrderInfo', 'idReferences', 'extrinsics'])]
class OrderRequestHeader
{
use CommentsTrait;
Expand All @@ -36,6 +38,14 @@ class OrderRequestHeader
#[Serializer\SerializedName('Tax')]
private ?Tax $tax = null;

#[Serializer\XmlElement]
#[Serializer\SerializedName('Payment')]
private ?Payment $payment = null;

#[Serializer\XmlElement]
#[Serializer\SerializedName('PaymentTerm')]
private ?PaymentTerm $paymentTerm = null;

#[Serializer\SerializedName('SupplierOrderInfo')]
private ?SupplierOrderInfo $supplierOrderInfo = null;

Expand Down Expand Up @@ -168,4 +178,24 @@ public function addBusinessPartner(BusinessPartner $businessPartner): void
{
$this->businessPartners[] = $businessPartner;
}

public function getPayment(): ?Payment
{
return $this->payment;
}

public function setPayment(?Payment $payment): void
{
$this->payment = $payment;
}

public function getPaymentTerm(): ?PaymentTerm
{
return $this->paymentTerm;
}

public function setPaymentTerm(?PaymentTerm $paymentTerm): void
{
$this->paymentTerm = $paymentTerm;
}
}
14 changes: 8 additions & 6 deletions src/CXml/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
{
public const DOC_TYPE_VERSION = '1.2.063';

private function __construct(private SerializerInterface $jmsSerializer)
{
private function __construct(
private SerializerInterface $jmsSerializer,
private string $dtdUri,
) {
}

public static function create(): self
public static function create(string $dtdUri = 'http://xml.cxml.org/schemas/cXML/' . self::DOC_TYPE_VERSION . '/cXML.dtd'): self
{
$jmsSerializer = SerializerBuilder::create()
->configureListeners(static function (EventDispatcherInterface $dispatcher): void {
Expand All @@ -55,7 +57,7 @@ public static function create(): self
)
->build();

return new self($jmsSerializer);
return new self($jmsSerializer, $dtdUri);
}

public function deserialize(string $xml): CXml
Expand All @@ -71,11 +73,11 @@ public function deserialize(string $xml): CXml
return $this->jmsSerializer->deserialize($xml, CXml::class, 'xml');
}

public function serialize(CXml $cxml, string $docTypeVersion = self::DOC_TYPE_VERSION): string
public function serialize(CXml $cxml): string
{
$xml = $this->jmsSerializer->serialize($cxml, 'xml');

$docType = '<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/' . $docTypeVersion . '/cXML.dtd">';
$docType = '<!DOCTYPE cXML SYSTEM "' . $this->dtdUri . '">' . "\n";
$xmlPrefix = '<?xml version="1.0" encoding="UTF-8"?>';

// add doctype, as it is mandatory in cXML
Expand Down
24 changes: 15 additions & 9 deletions src/CXml/Validation/DtdValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@

readonly class DtdValidator
{
public function __construct(private string $pathToCxmlDtds)
public function __construct(
private array $pathToDtds,
) {
Assertion::notEmpty($pathToDtds);
}

public static function forDtdDirectory(string $directory): self
{
Assertion::directory($pathToCxmlDtds);
Assertion::file($pathToCxmlDtds . '/cXML.dtd');
Assertion::file($pathToCxmlDtds . '/Fulfill.dtd');
Assertion::file($pathToCxmlDtds . '/Quote.dtd');
Assertion::directory($directory);

$pathToDtds = glob($directory . '/*.dtd');
Assertion::notEmpty($pathToDtds);

return new self($pathToDtds);
}

/**
Expand All @@ -38,9 +46,7 @@ public function validateAgainstDtd(string $xml): void
$old = new DOMDocument();
$old->loadXML($xml);

$validateFiles = ['cXML.dtd', 'Fulfill.dtd', 'Quote.dtd'];

$this->validateAgainstMultipleDtd($validateFiles, $old);
$this->validateAgainstMultipleDtd($this->pathToDtds, $old);

// reset throwing of php errors for libxml
libxml_use_internal_errors($internalErrors);
Expand All @@ -54,7 +60,7 @@ private function injectDtd(DOMDocument $originalDomDocument, string $dtdFilename
$creator = new DOMImplementation();

try {
$doctype = $creator->createDocumentType('cXML', '', $this->pathToCxmlDtds . '/' . $dtdFilename);
$doctype = $creator->createDocumentType('cXML', '', $dtdFilename);
$new = $creator->createDocument('', '', $doctype);
} catch (DOMException $domException) {
throw new CXmlInvalidException($domException->getMessage(), (string)$originalDomDocument->saveXML(), $domException);
Expand Down
26 changes: 25 additions & 1 deletion tests/CXmlTest/Handling/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public static function getEndpointData(): iterable
'QuoteMessage',
];

yield [
self::loadFixture('order_request.xml'),
'OrderRequest',
];

// TODO add more cases
}

Expand All @@ -47,7 +52,7 @@ private static function loadFixture(string $filename): ?string
public function testEndpoint(string $requestCxml, string $expectedHandlerCalled): void
{
$serializer = Serializer::create();
$messageValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
$messageValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050');

$credentialRepository = new Registry();
$credentialRepository->registerCredential(
Expand Down Expand Up @@ -90,8 +95,27 @@ public function handle(PayloadInterface $payload, Context $context): ?ResponsePa
}
};

$orderRequestHandler = new class($actualHandlerCalled) implements HandlerInterface {
public function __construct(private string &$actualHandlerCalled)
{
}

public static function getRequestName(): string
{
return 'OrderRequest';
}

public function handle(PayloadInterface $payload, Context $context): ?ResponsePayloadInterface
{
$this->actualHandlerCalled = 'OrderRequest';

return null;
}
};

$handlerRegistry = new HandlerRegistry();
$handlerRegistry->register($quoteMessageHandler);
$handlerRegistry->register($orderRequestHandler);

$builder = Builder::create();

Expand Down
80 changes: 80 additions & 0 deletions tests/CXmlTest/Handling/fixtures/order_request.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.060/cXML.dtd">
<cXML payloadID="1737963337.319.1846511.7249@eu-hub" timestamp="2025-01-27T07:35:37+00:00">
<Header>
<From>
<Credential domain="NetworkId">
<Identity>AN00000123</Identity>
</Credential>
</From>
<To>
<Credential domain="NetworkId">
<Identity>AN00000456</Identity>
</Credential>
</To>
<Sender>
<Credential domain="NetworkId">
<Identity>AN00000123</Identity>
<SharedSecret>Secret!123</SharedSecret>
</Credential>
<UserAgent>Suppliers Super Order Processor</UserAgent>
</Sender>
</Header>
<Request deploymentMode="test">
<OrderRequest>
<OrderRequestHeader orderID="acme-1-1" orderDate="2025-01-27T07:35:36+00:00" type="new" requestedDeliveryDate="2025-01-28T23:00:00+00:00">
<Total>
<Money currency="USD">10.00</Money>
</Total>
<ShipTo>
<Address>
<Name xml:lang="en">TEST.USA ACME</Name>
<PostalAddress>
<DeliverTo>ACME</DeliverTo>
<DeliverTo>TEST.USA</DeliverTo>
<Street>22 E 4th Ave</Street>
<City>Columbus</City>
<State>OH</State>
<PostalCode>43201-3551</PostalCode>
<Country isoCountryCode="US">United States</Country>
<Extrinsic name="phone">+49121255566</Extrinsic>
</PostalAddress>
</Address>
</ShipTo>
<BillTo>
<Address>
<Name xml:lang="en">TEST.USA ACME</Name>
<PostalAddress>
<DeliverTo>ACME</DeliverTo>
<DeliverTo>TEST.USA</DeliverTo>
<Street>22 E 4th Ave</Street>
<City>Columbus</City>
<State>OH</State>
<PostalCode>43201-3551</PostalCode>
<Country isoCountryCode="US">United States</Country>
</PostalAddress>
</Address>
</BillTo>
<Payment>
<PaymentService method="cc" provider="stripe">
<IdReference identifier="ch_..." domain="charge_id"/>
<IdReference identifier="pi_..." domain="intent_id"/>
</PaymentService>
</Payment>
</OrderRequestHeader>
<ItemOut lineNumber="1" quantity="1">
<ItemID>
<SupplierPartID>QA-ACME-AUTOTEST</SupplierPartID>
<BuyerPartID>QA-ACME-AUTOTEST</BuyerPartID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">10.00</Money>
</UnitPrice>
<Description xml:lang="en">Testing</Description>
<UnitOfMeasure>EA</UnitOfMeasure>
<Classification domain="supplier_part_id">QA-ACME-AUTOTEST</Classification>
</ItemDetail>
</ItemOut>
</OrderRequest>
</Request>
</cXML>
2 changes: 1 addition & 1 deletion tests/CXmlTest/Model/OrderRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class OrderRequestTest extends TestCase implements PayloadIdentityFactoryI

protected function setUp(): void
{
$this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
$this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
}

public function testMinimumExample(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/CXmlTest/Model/ProductActivityMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ProductActivityMessageTest extends TestCase implements PayloadIdenti

protected function setUp(): void
{
$this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
$this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
}

public function testMinimumExample(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/CXmlTest/Model/PunchOutSetupRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class PunchOutSetupRequestTest extends TestCase implements PayloadIdentity

protected function setUp(): void
{
$this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
$this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
}

public function testMinimumExample(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class PunchoutOrderMessageAdvancedPricingTest extends TestCase implements

protected function setUp(): void
{
$this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
$this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
}

public function testMinimumExampleAdvPricing(): void
Expand Down
Loading
Loading