Skip to content

Commit

Permalink
UNZER-502 Fix Paylater cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Tkachev committed Oct 14, 2024
1 parent 2193bba commit d3e25d1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
24 changes: 12 additions & 12 deletions src/Model/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,68 +37,68 @@ public function __construct()
*/
public function getUnzerCreated(): ?string
{
return $this->getRawField('OXACTIONDATE');
return $this->getRawField('oxactiondate');
}

/**
* @return string|null
*/
public function getUnzerCustomerId(): ?string
{
return $this->getRawField('CUSTOMERID');
return $this->getRawField('customerid');
}

/**
* @return string|null
*/
public function getUnzerCustomerType(): ?string
{
return $this->getRawField('CUSTOMERTYPE');
return $this->getRawField('customertype');
}
/**
* @return string|null
*/
public function getUnzerState(): ?string
{
return $this->getRawField('OXACTION');
return $this->getRawField('oxaction');
}

/**
* @return string|null
*/
public function getUnzerTypeId(): ?string
{
return $this->getRawField('TYPEID');
return $this->getRawField('typeid');
}

/**
* @return string|null
*/
public function getUnzerShortId(): ?string
{
return $this->getRawField('SHORTID');
return $this->getRawField('shortid');
}

/**
* @return string|null
*/
public function getUnzerCurrency(): ?string
{
return $this->getRawField('CURRENCY');
return $this->getRawField('currency');
}

/**
* @return string|null
*/
public function getUnzerAmount(): ?string
{
return $this->getRawField('AMOUNT');
return $this->getRawField('amount');
}

public function getUnzerRemaining(): ?string
{
if ($this->getUnzerState() === 'partly') {
return $this->getRawField('REMAINING');
return $this->getRawField('remaining');
}

return $this->getUnzerAmount();
Expand All @@ -109,12 +109,12 @@ public function getUnzerRemaining(): ?string
*/
public function getUnzerTraceId(): ?string
{
return $this->getRawField('TRACEID');
return $this->getRawField('traceid');
}

public function setTransStatus(string $status): void
{
$this->setFieldData('OXTRANSSTATUS', $status);
$this->setFieldData('oxtransstatus', $status);
}

/**
Expand All @@ -123,7 +123,7 @@ public function setTransStatus(string $status): void
public function getUnzerMetaData(): ?array
{
/** @var string $json */
$json = $this->getRawField('METADATA');
$json = $this->getRawField('metadata');
if ($json) {
/** @var array $jsonDecoded */
$jsonDecoded = json_decode($json, true);
Expand Down
12 changes: 10 additions & 2 deletions src/Service/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ public function doUnzerCancel(
string $reason
): Exception|bool|UnzerApiException {
try {
$sdk = $this->unzerSDKLoader->getUnzerSDKbyPaymentType($unzerid);
$userData = $this->unzerSDKLoader->getCustomerTypeCurByPaymentId($unzerid);
$sdk = $this->unzerSDKLoader->getUnzerSDK(
$userData['oxpaymenttype'],
$userData['currency'],
$userData['customertype']
);

if ($chargeid) {
$unzerCharge = $sdk->fetchChargeById($unzerid, $chargeid);
Expand All @@ -378,13 +383,16 @@ public function doUnzerCancel(
$cancellation = $sdk->cancelChargedPayment($payment, $cancellation);
}



/** @var string $oxuserid */
$oxuserid = $oOrder->getFieldData('oxuserid');
$this->transactionService->writeCancellationToDB(
$oOrder->getId(),
$oxuserid,
$cancellation,
$oOrder
$oOrder,
$userData['customertype']
);
} catch (UnzerApiException $e) {
return $e;
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public function writeCancellationToDB(
string $orderid,
string $userId,
?Cancellation $unzerCancel,
Order $oOrder
Order $oOrder,
string $customerType = ''
): bool {
$unzerCancelReason = '';
if ($unzerCancel !== null) {
Expand All @@ -166,6 +167,7 @@ public function writeCancellationToDB(
'oxuserid' => $userId,
'oxactiondate' => date('Y-m-d H:i:s', $this->utilsDate->getTime()),
'cancelreason' => $unzerCancelReason,
'customertype' => $customerType,
];

if ($unzerCancel instanceof Cancellation) {
Expand Down
36 changes: 25 additions & 11 deletions src/Service/UnzerSDKLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public function getUnzerSDK(string $paymentId = '', string $currency = '', strin
$sdk = $this->getUnzerSDKbyKey($key);
} catch (UnzerException $e) {
$logEntry = sprintf(
'Try to get the SDK with the Key "%s" defined by paymentId "%s", currency "%s", customerType "%s"',
'Try to get the SDK with the Key "%s" defined by paymentId "%s", currency "%s", customerType "%s" '
. $e->getTraceAsString()
,
$key,
$paymentId,
$currency,
Expand Down Expand Up @@ -120,6 +122,18 @@ public function getUnzerSDKbyKey(string $key): Unzer
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getUnzerSDKbyPaymentType(string $sPaymentId): Unzer
{
$customerData = $this->getCustomerTypeCurByPaymentId($sPaymentId);

return $this->getUnzerSDK(
(string)$customerData['oxpaymenttype'],
$customerData['currency'],
$customerData['customertype']
);
}


public function getCustomerTypeCurByPaymentId(string $sPaymentId): array
{
$queryBuilderFactory = $this->getServiceFromContainer(QueryBuilderFactoryInterface::class);
$queryBuilder = $queryBuilderFactory->create();
Expand All @@ -143,27 +157,27 @@ public function getUnzerSDKbyPaymentType(string $sPaymentId): Unzer

$result = $query->setParameters($parameters)->execute();
$row = null;
if ($result instanceof ResultStatement && $result->columnCount() === 1) {
$row = $result->fetchAssociative();
if ($result instanceof ResultStatement) {
$row = $result->fetchAllAssociative();
if (is_array($row) && count($row) > 0) {
$row = $row[0];
}
}

$customerType = '';
$currency = '';
$paymentId = '';
$oxpaymenttype = '';
if ($row) {
$currency = is_string($row['currency']) ? $row['currency'] : '';
$paymentId = is_string($row['oxpaymenttype']) ? $row['oxpaymenttype'] : '';
if ($paymentId === UnzerDefinitions::INVOICE_UNZER_PAYMENT_ID) {
$customerType = 'B2C';
$oxpaymenttype = is_string($row['oxpaymenttype']) ? $row['oxpaymenttype'] : '';
$customerType = 'B2C';
if ($oxpaymenttype === UnzerDefinitions::INVOICE_UNZER_PAYMENT_ID) {
if (!empty($row['oxdelcompany']) || !empty($row['oxbillcompany'])) {
$customerType = 'B2B';
}
}
if ($paymentId === UnzerDefinitions::INSTALLMENT_UNZER_PAYLATER_PAYMENT_ID) {
$customerType = 'B2C';
}
}

return $this->getUnzerSDK((string)$paymentId, $currency, $customerType);
return ['oxpaymenttype' => $oxpaymenttype, 'currency' => $currency, 'customertype' => $customerType];
}
}

0 comments on commit d3e25d1

Please sign in to comment.