Skip to content

Commit

Permalink
Import legacy package's 2.x changes (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-kujawa authored Mar 28, 2023
1 parent dbed49d commit 6657d5a
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Contracts/PaymentRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public function authorize($data, Billable $billable = null);
*/
public function capture(PaymentTransaction $transaction, $data = []);

/**
* Retrieve the transaction details from the provider.
*
* @param \Payavel\Checkout\Models\PaymentTransaction $transaction
* @return \Payavel\Checkout\PaymentResponse
*/
public function getTransaction(PaymentTransaction $transaction);

/**
* Void a previously authorized transaction.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Contracts/PaymentResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public function authorizeResponse();
*/
public function captureResponse();

/**
* Maps details from the getTransaction() response to the expected format.
*
* @return array|mixed
*/
public function getTransactionResponse();

/**
* Maps details from the void() response to the expected format.
*
Expand Down
1 change: 1 addition & 0 deletions src/Facades/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @method static \Payavel\Checkout\PaymentResponse deletePaymentMethod(\Payavel\Checkout\Models\PaymentMethod $paymentMethod)
* @method static \Payavel\Checkout\PaymentResponse authorize($data, \Payavel\Checkout\Contracts\Billable $billable = null)
* @method static \Payavel\Checkout\PaymentResponse capture(\Payavel\Checkout\Models\PaymentTransaction $transaction, $data = [])
* @method static \Payavel\Checkout\PaymentResponse getTransaction(\Payavel\Checkout\Models\PaymentTransaction $transaction)
* @method static \Payavel\Checkout\PaymentResponse void(\Payavel\Checkout\Models\PaymentTransaction $transaction, $data = [])
* @method static \Payavel\Checkout\PaymentResponse refund(\Payavel\Checkout\Models\PaymentTransaction $transaction, $data = [])
*
Expand Down
4 changes: 3 additions & 1 deletion src/Models/PaymentTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Payavel\Checkout\Database\Factories\PaymentTransactionFactory;
use Payavel\Checkout\Models\Traits\PaymentTransactionRequests;

class PaymentTransaction extends Model
{
use HasFactory;
use HasFactory,
PaymentTransactionRequests;

/**
* The attributes that aren't mass assignable.
Expand Down
40 changes: 40 additions & 0 deletions src/Models/Traits/PaymentTransactionRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Payavel\Checkout\Models\Traits;

trait PaymentTransactionRequests
{
use ConfiguresPaymentGateway;

/**
* Fetch the transaction details from the provider.
*
* @return \Payavel\Checkout\PaymentResponse
*/
public function fetch()
{
return $this->gateway->getTransaction($this);
}

/**
* Request the provider to void the transaction.
*
* @param array|mixed $data
* @return \Payavel\Checkout\PaymentResponse
*/
public function void($data = [])
{
return $this->gateway->void($this, $data);
}

/**
* Request the provider to refund the transaction.
*
* @param array|mixed $data
* @return \Payavel\Checkout\PaymentResponse
*/
public function refund($data = [])
{
return $this->gateway->refund($this, $data);
}
}
11 changes: 11 additions & 0 deletions src/PaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public function capture(PaymentTransaction $transaction, $data = [])
return tap($this->gateway->capture($transaction, $data))->configure(__FUNCTION__, $this->provider, $this->merchant);
}

/**
* Retrieve the transaction details from the provider.
*
* @param \Payavel\Checkout\Models\PaymentTransaction $transaction
* @return \Payavel\Checkout\PaymentResponse
*/
public function getTransaction(PaymentTransaction $transaction)
{
return tap($this->gateway->getTransaction($transaction))->configure(__FUNCTION__, $this->provider, $this->merchant);
}

/**
* Void a previously authorized transaction.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Traits/PaymentRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public function capture(PaymentTransaction $transaction, $data = [])
$this->throwRuntimeException(__FUNCTION__);
}

/**
* Retrieve the transaction details from the provider.
*
* @param \Payavel\Checkout\Models\PaymentTransaction $transaction
* @return \Payavel\Checkout\PaymentResponse
*/
public function getTransaction(PaymentTransaction $transaction)
{
$this->throwRuntimeException(__FUNCTION__);
}

/**
* Void a previously authorized transaction.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Traits/PaymentResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public function captureResponse()
return $this->genericResponse(__FUNCTION__);
}

/**
* Maps details from the getTransaction() response to the expected format.
*
* @return array|mixed
*/
public function getTransactionResponse()
{
$this->throwRuntimeException(__FUNCTION__);
}

/**
* Maps details from the void() response to the expected format.
*
Expand Down
11 changes: 11 additions & 0 deletions src/stubs/payment-request.stub
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ class {{ name }}PaymentRequest extends PaymentRequest
//
}

/**
* Retrieve the transaction details from the provider.
*
* @param \Payavel\Checkout\Models\PaymentTransaction $transaction
* @return \Payavel\Checkout\PaymentResponse
*/
public function getTransaction(PaymentTransaction $transaction)
{
//
}

/**
* Void a previously authorized transaction.
*
Expand Down
10 changes: 10 additions & 0 deletions src/stubs/payment-response.stub
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ class {{ name }}PaymentResponse extends PaymentResponse
//
}

/**
* Maps details from the getTransaction() response to the expected format.
*
* @return array|mixed
*/
public function getTransactionResponse()
{
//
}

/**
* Maps details from the void() response to the expected format.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/GatewayTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public function capture(PaymentTransaction $transaction, $data = [])
return new FakePaymentResponse([]);
}

public function getTransaction(PaymentTransaction $transaction)
{
return new FakePaymentResponse([]);
}

public function void(PaymentTransaction $paymentTransaction, $data =[])
{
return new FakePaymentResponse([]);
Expand Down Expand Up @@ -194,6 +199,13 @@ public function captureResponse()
];
}

public function getTransactionResponse()
{
return [
'requestMethod' => $this->requestMethod,
];
}

public function voidResponse()
{
return [
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/TestPaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ public function capture_method_returns_configured_response()
$this->assertEquals('capture', $response->data['requestMethod']);
}

/** @test */
public function get_transaction_method_returns_configured_response()
{
$transaction = PaymentTransaction::factory()->create([
'provider_id' => Payment::getProvider()->getId(),
'merchant_id' => Payment::getMerchant()->getId(),
]);

$response = Payment::getTransaction($transaction);

$this->assertResponseIsConfigured($response);

$this->assertEquals('getTransaction', $response->data['requestMethod']);
}

/** @test */
public function void_method_returns_configured_response()
{
Expand Down

0 comments on commit 6657d5a

Please sign in to comment.