Files
2026-09-15 10:35:56 +08:00

74 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Oceanpayment\Model\Callback;
use Magento\Framework\Exception\LocalizedException;
class Parser
{
private const FIELDS = [
'notice_type',
'push_dateTime',
'response_type',
'account',
'terminal',
'signValue',
'methods',
'order_number',
'card_country',
'order_currency',
'order_amount',
'order_notes',
'card_number',
'card_type',
'payment_country',
'payment_id',
'payment_status',
'payment_authType',
'payment_details',
'payment_solutions',
'payment_risk',
'payment_amount',
'payment_exchangeRate',
'pay_barCode',
'auth_reason',
'order_operator',
'auth_code',
];
public function parse(string $body, array $parameters): array
{
if (strlen($body) > 1048576) {
throw new LocalizedException(__('Oceanpayment callback body is too large.'));
}
$source = $parameters;
if (str_starts_with(ltrim($body), '<')) {
$previous = libxml_use_internal_errors(true);
try {
$xml = simplexml_load_string($body, \SimpleXMLElement::class, LIBXML_NONET | LIBXML_NOCDATA);
} finally {
libxml_clear_errors();
libxml_use_internal_errors($previous);
}
if ($xml === false) {
throw new LocalizedException(__('Oceanpayment callback XML is invalid.'));
}
$source = [];
foreach (self::FIELDS as $field) {
$source[$field] = isset($xml->{$field}) ? (string)$xml->{$field} : '';
}
}
$result = [];
foreach (self::FIELDS as $field) {
$value = $source[$field] ?? '';
$result[$field] = is_scalar($value) ? trim((string)$value) : '';
}
return $result;
}
}