Working on Authorize Refunds

This commit is contained in:
David Bomba 2020-06-24 11:15:51 +10:00
parent 889ac65f26
commit 7e73aa85cb
5 changed files with 104 additions and 26 deletions

View File

@ -50,15 +50,15 @@ class AuthorizeTransactions
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) if (($response != null) && ($response->getMessages()->getResultCode() == "Ok"))
{ {
echo "SUCCESS: Transaction Status:" . $response->getTransaction()->getTransactionStatus() . "\n"; info( "SUCCESS: Transaction Status:" . $response->getTransaction()->getTransactionStatus() );
echo " Auth Amount:" . $response->getTransaction()->getAuthAmount() . "\n"; info( " Auth Amount:" . $response->getTransaction()->getAuthAmount() );
echo " Trans ID:" . $response->getTransaction()->getTransId() . "\n"; info( " Trans ID:" . $response->getTransaction()->getTransId() );
} }
else else
{ {
echo "ERROR : Invalid response\n"; info( "ERROR : Invalid response\n");
$errorMessages = $response->getMessages()->getMessage(); $errorMessages = $response->getMessages()->getMessage();
echo "Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n"; info( "Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() );
} }
return $response; return $response;

View File

@ -12,7 +12,9 @@
namespace App\PaymentDrivers\Authorize; namespace App\PaymentDrivers\Authorize;
use App\Models\Payment;
use App\PaymentDrivers\AuthorizePaymentDriver; use App\PaymentDrivers\AuthorizePaymentDriver;
use App\PaymentDrivers\Authorize\AuthorizeTransactions;
use net\authorize\api\contract\v1\CreateTransactionRequest; use net\authorize\api\contract\v1\CreateTransactionRequest;
use net\authorize\api\contract\v1\CustomerProfilePaymentType; use net\authorize\api\contract\v1\CustomerProfilePaymentType;
use net\authorize\api\contract\v1\PaymentProfileType; use net\authorize\api\contract\v1\PaymentProfileType;
@ -26,15 +28,23 @@ use net\authorize\api\controller\CreateTransactionController;
*/ */
class RefundTransaction class RefundTransaction
{ {
public $authorize;
public function __construct(AuthorizePaymentDriver $authorize) public $authorize_transaction;
public function __construct(AuthorizePaymentDriver $authorize, AuthorizeTransactions $authorize_transaction)
{ {
$this->authorize = $authorize; $this->authorize = $authorize;
$this->authorize_transaction = new AuthorizeTransactions($this->authorize);
} }
function refundTransaction($transaction_reference, $amount, $payment_profile_id, $profile_id) function refundTransaction(Payment $payment, $amount)
{ {
$transaction_details = $this->authorize_transaction->getTransactionDetails($payment->transaction_reference);
info(print_r($transaction_details,1));
$this->authorize->init(); $this->authorize->init();
// Set the transaction's refId // Set the transaction's refId
@ -53,7 +63,7 @@ class RefundTransaction
$transactionRequest->setTransactionType("refundTransaction"); $transactionRequest->setTransactionType("refundTransaction");
$transactionRequest->setAmount($amount); $transactionRequest->setAmount($amount);
$transactionRequest->setProfile($customerProfile); $transactionRequest->setProfile($customerProfile);
$transactionRequest->setRefTransId($transaction_reference); $transactionRequest->setRefTransId($payment->transaction_reference);
$request = new CreateTransactionRequest(); $request = new CreateTransactionRequest();
$request->setMerchantAuthentication($this->authorize->merchant_authentication); $request->setMerchantAuthentication($this->authorize->merchant_authentication);
@ -70,18 +80,30 @@ class RefundTransaction
if ($tresponse != null && $tresponse->getMessages() != null) if ($tresponse != null && $tresponse->getMessages() != null)
{ {
echo " Transaction Response code : " . $tresponse->getResponseCode() . "\n";
echo "Refund SUCCESS: " . $tresponse->getTransId() . "\n"; return [
echo " Code : " . $tresponse->getMessages()[0]->getCode() . "\n"; 'transaction_reference' => $tresponse->getTransId(),
echo " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n"; 'success' => true,
'description' => $tresponse->getMessages()[0]->getDescription(),
'code' => $tresponse->getMessages()[0]->getCode(),
'transaction_response' => $tresponse->getResponseCode()
];
} }
else else
{ {
echo "Transaction Failed \n";
if($tresponse->getErrors() != null) if($tresponse->getErrors() != null)
{ {
echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n"; return [
'transaction_reference' => '',
'transaction_response' => '',
'success' => false,
'description' => $tresponse->getErrors()[0]->getErrorText(),
'code' => $tresponse->getErrors()[0]->getErrorCode(),
];
} }
} }
} }
@ -91,22 +113,50 @@ class RefundTransaction
$tresponse = $response->getTransactionResponse(); $tresponse = $response->getTransactionResponse();
if($tresponse != null && $tresponse->getErrors() != null) if($tresponse != null && $tresponse->getErrors() != null)
{ {
echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n"; return [
'transaction_reference' => '',
'transaction_response' => '',
'success' => false,
'description' => $tresponse->getErrors()[0]->getErrorText(),
'code' => $tresponse->getErrors()[0]->getErrorCode(),
];
} }
else else
{ {
echo " Error code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
echo " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n"; return [
'transaction_reference' => '',
'transaction_response' => '',
'success' => false,
'description' => $response->getMessages()->getMessage()[0]->getText(),
'code' => $response->getMessages()->getMessage()[0]->getCode(),
];
} }
} }
} }
else else
{ {
echo "No response returned \n";
return [
'transaction_reference' => '',
'transaction_response' => '',
'success' => false,
'description' => 'No response returned',
'code' => 'No response returned',
];
} }
return $response; return [
'transaction_reference' => '',
'transaction_response' => '',
'success' => false,
'description' => 'No response returned',
'code' => 'No response returned',
];
} }

View File

@ -14,6 +14,7 @@ namespace App\PaymentDrivers;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
use App\Models\GatewayType; use App\Models\GatewayType;
use App\Models\Payment;
use App\PaymentDrivers\Authorize\AuthorizeCreditCard; use App\PaymentDrivers\Authorize\AuthorizeCreditCard;
use App\PaymentDrivers\Authorize\AuthorizePaymentMethod; use App\PaymentDrivers\Authorize\AuthorizePaymentMethod;
use net\authorize\api\constants\ANetEnvironment; use net\authorize\api\constants\ANetEnvironment;
@ -127,7 +128,7 @@ class AuthorizePaymentDriver extends BaseDriver
return false; return false;
} }
public function refund($amount, $transaction_reference, $return_client_response = false) public function refund(Payment $payment, $refund_amount, $return_client_response = false)
{ {
} }

View File

@ -82,12 +82,12 @@ class BaseDriver extends AbstractPaymentDriver
/** /**
* Executes a refund attempt for a given amount with a transaction_reference * Executes a refund attempt for a given amount with a transaction_reference
* *
* @param Payment $payment The Payment Object
* @param float $amount The amount to be refunded * @param float $amount The amount to be refunded
* @param string $transaction_reference The transaction reference
* @param boolean $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment) * @param boolean $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment)
* @return mixed * @return mixed
*/ */
public function refund($amount, $transaction_reference, $return_client_response = false) {} public function refund(Payment $payment, $amount, $return_client_response = false) {}
/** /**
* Set the inbound request payment method type for access. * Set the inbound request payment method type for access.

View File

@ -2,6 +2,10 @@
namespace Tests\Integration\PaymentDrivers; namespace Tests\Integration\PaymentDrivers;
use App\Factory\PaymentFactory;
use App\Models\CompanyGateway;
use App\PaymentDrivers\AuthorizePaymentDriver;
use Tests\MockAccountData;
use Tests\TestCase; use Tests\TestCase;
use net\authorize\api\constants\ANetEnvironment; use net\authorize\api\constants\ANetEnvironment;
use net\authorize\api\contract\v1 as AnetAPI; use net\authorize\api\contract\v1 as AnetAPI;
@ -31,6 +35,7 @@ use net\authorize\api\controller\GetMerchantDetailsController;
*/ */
class AuthorizeTest extends TestCase class AuthorizeTest extends TestCase
{ {
use MockAccountData;
public $customer_profile_id = 1512191314; public $customer_profile_id = 1512191314;
@ -44,6 +49,7 @@ class AuthorizeTest extends TestCase
$this->markTestSkipped('authorize.net not configured'); $this->markTestSkipped('authorize.net not configured');
} }
$this->makeTestData();
} }
public function testUnpackingVars() public function testUnpackingVars()
@ -372,6 +378,27 @@ class AuthorizeTest extends TestCase
} }
$this->assertNotNull($response); $this->assertNotNull($response);
$this->assertNotNull($tresponse);
//$tresponse->getTransId()
$payment = PaymentFactory::create($this->company->id, $this->user->id);
$payment->amount = 400;
$payment->client_id = $this->client->id;
$payment->date = now();
$payment->transaction_reference = $tresponse->getTransId();
$payment->save();
$company_gateway = CompanyGateway::where('gateway_key', '3b6621f970ab18887c4f6dca78d3f8bb')->first();
$authorize_payment_driver = new AuthorizePaymentDriver($company_gateway, $this->client);
$response = $authorize->refund($payment, 400);
info(print_r($response,1));
$this->assertTrue(is_array($response));
} }
} }