diff --git a/app/PaymentDrivers/AbstractPaymentDriver.php b/app/PaymentDrivers/AbstractPaymentDriver.php index dcfb81558389..8b583ad1ae8e 100644 --- a/app/PaymentDrivers/AbstractPaymentDriver.php +++ b/app/PaymentDrivers/AbstractPaymentDriver.php @@ -11,6 +11,8 @@ namespace App\PaymentDrivers; +use App\Models\Payment; + abstract class AbstractPaymentDriver { @@ -18,7 +20,7 @@ abstract class AbstractPaymentDriver abstract public function purchase($amount, $return_client_response = false); - abstract public function refund($amount, $transaction_reference, $return_client_response = false); + abstract public function refund(Payment $payment, $refund_amount, $return_client_response = false); abstract public function setPaymentMethod($payment_method_id); diff --git a/app/PaymentDrivers/Authorize/AuthorizeTransactions.php b/app/PaymentDrivers/Authorize/AuthorizeTransactions.php index e5ab681c62fd..fc18107e8583 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeTransactions.php +++ b/app/PaymentDrivers/Authorize/AuthorizeTransactions.php @@ -12,6 +12,7 @@ namespace App\PaymentDrivers\Authorize; +use App\PaymentDrivers\AuthorizePaymentDriver; use net\authorize\api\contract\v1\GetTransactionDetailsRequest; use net\authorize\api\controller\GetTransactionDetailsController; diff --git a/app/PaymentDrivers/Authorize/RefundTransaction.php b/app/PaymentDrivers/Authorize/RefundTransaction.php index 7945788e7355..e7b7362c355a 100644 --- a/app/PaymentDrivers/Authorize/RefundTransaction.php +++ b/app/PaymentDrivers/Authorize/RefundTransaction.php @@ -32,7 +32,7 @@ class RefundTransaction public $authorize_transaction; - public function __construct(AuthorizePaymentDriver $authorize, AuthorizeTransactions $authorize_transaction) + public function __construct(AuthorizePaymentDriver $authorize) { $this->authorize = $authorize; $this->authorize_transaction = new AuthorizeTransactions($this->authorize); @@ -40,10 +40,10 @@ class RefundTransaction function refundTransaction(Payment $payment, $amount) { + error_reporting (E_ALL & ~E_DEPRECATED); $transaction_details = $this->authorize_transaction->getTransactionDetails($payment->transaction_reference); - info(print_r($transaction_details,1)); $this->authorize->init(); @@ -51,11 +51,11 @@ class RefundTransaction $refId = 'ref' . time(); $paymentProfile = new PaymentProfileType(); - $paymentProfile->setPaymentProfileId( $payment_profile_id ); + $paymentProfile->setPaymentProfileId( $transaction_details->getTransaction()->getProfile()->getCustomerPaymentProfileId() ); // set customer profile $customerProfile = new CustomerProfilePaymentType(); - $customerProfile->setCustomerProfileId( $profile_id ); + $customerProfile->setCustomerProfileId( $transaction_details->getTransaction()->getProfile()->getCustomerProfileId() ); $customerProfile->setPaymentProfile( $paymentProfile ); //create a transaction diff --git a/app/PaymentDrivers/AuthorizePaymentDriver.php b/app/PaymentDrivers/AuthorizePaymentDriver.php index aedadbb0991c..bc83570df46c 100644 --- a/app/PaymentDrivers/AuthorizePaymentDriver.php +++ b/app/PaymentDrivers/AuthorizePaymentDriver.php @@ -17,6 +17,7 @@ use App\Models\GatewayType; use App\Models\Payment; use App\PaymentDrivers\Authorize\AuthorizeCreditCard; use App\PaymentDrivers\Authorize\AuthorizePaymentMethod; +use App\PaymentDrivers\Authorize\RefundTransaction; use net\authorize\api\constants\ANetEnvironment; use net\authorize\api\contract\v1\CreateTransactionRequest; use net\authorize\api\contract\v1\GetMerchantDetailsRequest; @@ -111,16 +112,12 @@ class AuthorizePaymentDriver extends BaseDriver public function processPaymentView($data) { - return $this->payment_method->processPaymentView($data); - } public function processPaymentResponse($request) { - return $this->payment_method->processPaymentResponse($request); - } public function purchase($amount, $return_client_response = false) @@ -130,7 +127,7 @@ class AuthorizePaymentDriver extends BaseDriver public function refund(Payment $payment, $refund_amount, $return_client_response = false) { - + return (new RefundTransaction($this))->refundTransaction($payment, $refund_amount); } public function findClientGatewayRecord() :?ClientGatewayToken diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 822eaadbfb5c..677df382d8be 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -57,7 +57,7 @@ class RefundPayment if ($this->refund_data['gateway_refund'] !== false && $this->total_refund > 0) { - $gateway = CompanyGateway::find($this->company_gateway_id); + $gateway = CompanyGateway::find($this->payment->company_gateway_id); if ($gateway) { $response = $gateway->driver($this->payment->client)->refund($this->payment, $this->total_refund); @@ -65,7 +65,7 @@ class RefundPayment if (!$response) { throw new PaymentRefundFailed(); } - + info(print_r($response,1)); //todo //need to check the gateway response has successfully be transacted. @@ -290,127 +290,3 @@ class RefundPayment return $this->payment; } } - - - - - -/* - - private function refundPaymentWithNoInvoices(array $data) - { - - $this->createActivity($data, $credit_note->id); - - //determine if we need to refund via gateway - if ($data['gateway_refund'] !== false) { - //todo process gateway refund, on success, reduce the credit note balance to 0 - } - - $this->save(); - - //$this->client->paid_to_date -= $data['amount']; - $this->client->save(); - - return $this->fresh(); - } - - - private function refundPaymentWithInvoices($data) - { - - if ($data['gateway_refund'] !== false && $this->total_refund > 0) { - $gateway = CompanyGateway::find($this->company_gateway_id); - - if ($gateway) { - $response = $gateway->driver($this->client)->refund($this, $this->total_refund); - - if (!$response) { - throw new PaymentRefundFailed(); - } - } - } - - if ($this->total_refund > 0) { - $this->refunded += $this->total_refund; - } - - $this->save(); - - $client_balance_adjustment = $this->adjustInvoices($data); - - $credit_note->ledger()->updateCreditBalance($client_balance_adjustment, $ledger_string); - - $this->client->paid_to_date -= $data['amount']; - $this->client->save(); - - - return $this; - } - - private function createActivity(array $data, int $credit_id) - { - $fields = new \stdClass; - $activity_repo = new ActivityRepository(); - - $fields->payment_id = $this->id; - $fields->user_id = $this->user_id; - $fields->company_id = $this->company_id; - $fields->activity_type_id = Activity::REFUNDED_PAYMENT; - $fields->credit_id = $credit_id; - - if (isset($data['invoices'])) { - foreach ($data['invoices'] as $invoice) { - $fields->invoice_id = $invoice->id; - - $activity_repo->save($fields, $this); - } - } else { - $activity_repo->save($fields, $this); - } - } - - - private function buildCreditNote(array $data) :?Credit - { - $credit_note = CreditFactory::create($this->company_id, $this->user_id); - $credit_note->assigned_user_id = isset($this->assigned_user_id) ?: null; - $credit_note->date = $data['date']; - $credit_note->status_id = Credit::STATUS_SENT; - $credit_note->client_id = $this->client->id; - $credit_note->amount = $data['amount']; - $credit_note->balance = $data['amount']; - - return $credit_note; - } - - private function adjustInvoices(array $data) - { - $adjustment_amount = 0; - - foreach ($data['invoices'] as $refunded_invoice) { - $invoice = Invoice::find($refunded_invoice['invoice_id']); - - $invoice->service()->updateBalance($refunded_invoice['amount'])->save(); - - if ($invoice->amount == $invoice->balance) { - $invoice->service()->setStatus(Invoice::STATUS_SENT); - } else { - $invoice->service()->setStatus(Invoice::STATUS_PARTIAL); - } - - $client = $invoice->client; - - $adjustment_amount += $refunded_invoice['amount']; - $client->balance += $refunded_invoice['amount']; - - $client->save(); - - //todo adjust ledger balance here? or after and reference the credit and its total - } - - return $adjustment_amount; - } -} - - */ \ No newline at end of file diff --git a/tests/Integration/PaymentDrivers/AuthorizeTest.php b/tests/Integration/PaymentDrivers/AuthorizeTest.php index ae09f13ebeba..2dd85be48551 100644 --- a/tests/Integration/PaymentDrivers/AuthorizeTest.php +++ b/tests/Integration/PaymentDrivers/AuthorizeTest.php @@ -37,9 +37,9 @@ class AuthorizeTest extends TestCase { use MockAccountData; - public $customer_profile_id = 1512191314; + public $customer_profile_id = 1512373273; - public $customer_payment_profile = 1512219932; + public $customer_payment_profile = 1512424103; public function setUp() :void { @@ -154,15 +154,7 @@ class AuthorizeTest extends TestCase // Create an array of any shipping addresses $shippingProfiles[] = $customerShippingAddress; $refId = 'ref' . time(); - $email = "test@gmail.com"; - - // // Create a new CustomerPaymentProfile object - // $paymentProfile = new AnetAPI\CustomerPaymentProfileType(); - // $paymentProfile->setCustomerType('individual'); - // $paymentProfile->setBillTo($billTo); - // $paymentProfile->setPayment($paymentCreditCard); - // $paymentProfiles[] = $paymentProfile; - + $email = "test12@gmail.com"; // Create a new CustomerProfileType and add the payment profile object $customerProfile = new CustomerProfileType(); @@ -194,6 +186,8 @@ class AuthorizeTest extends TestCase $errorMessages = $response->getMessages()->getMessage(); info("Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n"); } + + info("the new customer profile id = ". $response->getCustomerProfileId()); $this->assertNotNull($response); @@ -247,16 +241,16 @@ class AuthorizeTest extends TestCase // Set credit card information for payment profile $creditCard = new CreditCardType(); - $creditCard->setCardNumber("4007000000027"); - $creditCard->setExpirationDate("2038-12"); - $creditCard->setCardCode("142"); + $creditCard->setCardNumber("4111111111111111"); + $creditCard->setExpirationDate("2024-01"); + $creditCard->setCardCode("100"); $paymentCreditCard = new PaymentType(); $paymentCreditCard->setCreditCard($creditCard); // Create the Bill To info for new payment type $billto = new CustomerAddressType(); - $billto->setFirstName("Ellen"); - $billto->setLastName("Johnson"); + $billto->setFirstName("Elas"); + $billto->setLastName("Joson"); $billto->setCompany("Souveniropolis"); $billto->setAddress("14 Main Street"); $billto->setCity("Pecan Springs"); @@ -321,7 +315,7 @@ class AuthorizeTest extends TestCase $transactionRequestType = new TransactionRequestType(); $transactionRequestType->setTransactionType( "authCaptureTransaction"); - $transactionRequestType->setAmount(400); + $transactionRequestType->setAmount(350); $transactionRequestType->setProfile($profileToCharge); $request = new CreateTransactionRequest(); @@ -381,23 +375,27 @@ class AuthorizeTest extends TestCase $this->assertNotNull($tresponse); - //$tresponse->getTransId() + /* Testing refunds - need to research more as payments are in a pending state so cannot be 'refunded'*/ + + // info("transaction reference = " . $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(); + // $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->company_gateway_id = 1; - $company_gateway = CompanyGateway::where('gateway_key', '3b6621f970ab18887c4f6dca78d3f8bb')->first(); + // $payment->save(); - $authorize_payment_driver = new AuthorizePaymentDriver($company_gateway, $this->client); - $response = $authorize->refund($payment, 400); + // $company_gateway = CompanyGateway::where('gateway_key', '3b6621f970ab18887c4f6dca78d3f8bb')->first(); - info(print_r($response,1)); + // $authorize_payment_driver = new AuthorizePaymentDriver($company_gateway, $this->client); + // $response = $authorize_payment_driver->refund($payment, 350); - $this->assertTrue(is_array($response)); + // info(print_r($response,1)); + + // $this->assertTrue(is_array($response)); }