Skip to content

Commit

Permalink
implement cancel action
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Mar 6, 2018
1 parent c4d1c79 commit 587d5c2
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
90 changes: 90 additions & 0 deletions Action/Api/CancelAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace DachcomDigital\Payum\Powerpay\Action\Api;

use DachcomDigital\Payum\Powerpay\Api;
use DachcomDigital\Payum\Powerpay\Exception\PowerpayException;
use DachcomDigital\Payum\Powerpay\Request\Api\Cancel;
use DachcomDigital\Payum\Powerpay\Request\Api\Confirm;
use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;

class CancelAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
{
use GatewayAwareTrait;
use PowerpayAwareTrait;

/**
* @var Api
*/
protected $api;

/**
* {@inheritDoc}
*/
public function setApi($api)
{
if (false == $api instanceof Api) {
throw new UnsupportedApiException('Not supported.');
}
$this->api = $api;
}

/**
* @param Cancel $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$details = ArrayObject::ensureArrayObject($request->getModel());
$details->validateNotEmpty(['card_number']);

try {
$result = $this->api->generateCancelRequest($details);

$resultData['response_date'] = $result['ResponseDate'];
$resultData['skipped'] = false;

//there was an error.
if (isset($result['ResponseCode'])) {
$resultData['response_code'] = $result['ResponseCode'];
} else {

$resultData['card_statistics_total_number'] = $result['CardStatistics']['Total']['@attributes']['number'];
$resultData['card_statistics_total_amount'] = $result['CardStatistics']['Total']['@attributes']['amount'];
$resultData['card_statistics_purchase_number'] = $result['CardStatistics']['Purchase']['@attributes']['number'];
$resultData['card_statistics_purchase_amount'] = $result['CardStatistics']['Purchase']['@attributes']['amount'];
$resultData['card_statistics_credit_number'] = $result['CardStatistics']['Credit']['@attributes']['number'];
$resultData['card_statistics_credit_amount'] = $result['CardStatistics']['Credit']['@attributes']['amount'];
$resultData['card_statistics_reversal_number'] = $result['CardStatistics']['Reversal']['@attributes']['number'];
$resultData['card_statistics_reversal_amount'] = $result['CardStatistics']['Reversal']['@attributes']['amount'];

if (isset($result['Skipped'])) {
$resultData['skipped'] = true;
$resultData['skipped_reason'] = $result['Skipped']['Reason'];
}
}

$request->setResult($resultData);

} catch (PowerpayException $e) {
$this->populateDetailsWithError($details, $e, $request);
}
}

/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof Cancel &&
$request->getModel() instanceof \ArrayAccess;
}
}
58 changes: 56 additions & 2 deletions Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ public function generateConfirmRequest(ArrayObject $details)
$conversion = $xml->addChild('Conversation');

$financialRequest = $conversion->addChild('FinancialRequest');
$financialRequest->addAttribute('protocol', 'PaymentServer_V2_9');
$financialRequest->addAttribute('protocol', $protocol);
$financialRequest->addAttribute('msgnum', $msgNum);

$financialRequest->addChild('RequestDate', $genDate);
$financialRequest->addChild('TransactionType', "debit");
$financialRequest->addChild('TransactionType', $details['transaction_type']);
$financialRequest->addChild('Currency', $details['currency']);
$financialRequest->addChild('Amount', $details['amount']);

Expand Down Expand Up @@ -208,6 +208,60 @@ public function generateConfirmRequest(ArrayObject $details)
return $request;
}

/**
* @param ArrayObject $details
* @return array
* @throws PowerpayException
*/
public function generateCancelRequest(ArrayObject $details)
{
$msgNum = uniqid();
$genDate = date('YmdHis');
$protocol = 'PaymentServer_V2_9';

$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><Confirmation></Confirmation>');

$xml->addAttribute('protocol', $protocol);
$xml->addAttribute('msgnum', $msgNum);
$xml->addAttribute('gendate', $genDate);

$conversion = $xml->addChild('Conversation');

$financialRequest = $conversion->addChild('FinancialRequest');
$financialRequest->addAttribute('protocol', $protocol);
$financialRequest->addAttribute('msgnum', $msgNum);

$financialRequest->addChild('RequestDate', $genDate);
$financialRequest->addChild('TransactionType', $details['transaction_type']);
$financialRequest->addChild('Currency', $details['currency']);
$financialRequest->addChild('Amount', 0);

$financialRequest->addChild('MerchantId', $this->options['merchantId']);
$financialRequest->addChild('FilialId', $this->options['filialId']);
$financialRequest->addChild('TerminalId', $this->options['terminalId']);

$response = $conversion->addChild('Response');
$response->addAttribute('msgnum', $msgNum);

$response->addChild('ResponseCode', $details['response_code']);
$response->addChild('ResponseDate', $details['response_date']);
$response->addChild('AuthorizationCode', $details['authorization_code']);
$response->addChild('Currency', $details['currency']);
$response->addChild('Balance', 0);
$response->addChild('CardNumber', $details['card_number']);
$response->addChild('ExpirationDate', $details['expiration_date']);

try {
$request = $this->doRequest([
'xml' => $xml->asXML()
]);
} catch (\Exception $e) {
throw new PowerpayException($e->getMessage());
}

return $request;
}

/**
* @param array $fields
*
Expand Down
2 changes: 2 additions & 0 deletions PowerpayGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DachcomDigital\Payum\Powerpay;

use DachcomDigital\Payum\Powerpay\Action\Api\ActivateAction;
use DachcomDigital\Payum\Powerpay\Action\Api\CancelAction;
use DachcomDigital\Payum\Powerpay\Action\Api\ConfirmAction;
use DachcomDigital\Payum\Powerpay\Action\Api\ReserveAmountAction;
use DachcomDigital\Payum\Powerpay\Action\Api\Transformer\CustomerTransformerAction;
Expand All @@ -29,6 +30,7 @@ protected function populateConfig(ArrayObject $config)

'payum.action.api.activate' => new ActivateAction(),
'payum.action.api.confirm' => new ConfirmAction(),
'payum.action.api.cancel' => new CancelAction(),
'payum.action.api.reserve_amount' => new ReserveAmountAction(),

'payum.action.api.transformer.customer' => new CustomerTransformerAction(),
Expand Down
30 changes: 30 additions & 0 deletions Request/Api/Cancel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace DachcomDigital\Payum\Powerpay\Request\Api;

use Payum\Core\Request\Generic;

class Cancel extends Generic
{
/**
* @var array
*/
protected $result;

/**
* @param $result
*/
public function setResult($result)
{
$this->result = $result;
}

/**
* @return array
*/
public function getResult()
{
return $this->result;
}

}

0 comments on commit 587d5c2

Please sign in to comment.