Skip to content

Commit

Permalink
Finish gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinjuan committed Aug 10, 2019
1 parent 149aeb0 commit 1c92451
Show file tree
Hide file tree
Showing 36 changed files with 970 additions and 394 deletions.
3 changes: 1 addition & 2 deletions config/laravel-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

return [

'default_gateway' => 'mercado-pago',
'default_gateway' => '',
'currency' => 'USD',

'gateways' => [
'mercado-pago' => \litvinjuan\LaravelPayments\Gateways\MercadoPagoGateway::class,
// 'another' => Another\Gateway\Implementation::class,
],

Expand Down
8 changes: 6 additions & 2 deletions src/Exceptions/InvalidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace litvinjuan\LaravelPayments\Exceptions;

use Exception;
use Illuminate\Support\Collection;

class InvalidRequestException extends Exception
{
Expand All @@ -25,12 +24,17 @@ public static function alreadySent()

public static function missingParameters(string $key)
{
return new self('The following parameter is missing in your request: ' . $key);
return new self("The following parameter is missing in your request: [$key]");
}

public static function zeroAmount()
{
return new self('This request does not allow an amount of 0');
}

public static function notFound($request)
{
return new self("The request [$request] does not exists.");
}

}
60 changes: 38 additions & 22 deletions src/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,42 @@

use BadMethodCallException;
use Illuminate\Support\Str;
use litvinjuan\LaravelPayments\Exceptions\InvalidRequestException;
use litvinjuan\LaravelPayments\Requests\AbstractAuthorizeRequest;
use litvinjuan\LaravelPayments\Requests\AbstractCaptureRequest;
use litvinjuan\LaravelPayments\Requests\AbstractCompleteAuthorizeRequest;
use litvinjuan\LaravelPayments\Requests\AbstractCompletePurchaseRequest;
use litvinjuan\LaravelPayments\Requests\AbstractGetPaymentRequest;
use litvinjuan\LaravelPayments\Requests\AbstractPaymentNotificationRequest;
use litvinjuan\LaravelPayments\Requests\AbstractPurchaseRequest;
use litvinjuan\LaravelPayments\Requests\AbstractRefundRequest;
use litvinjuan\LaravelPayments\Requests\AbstractRequest;
use litvinjuan\LaravelPayments\Requests\RequestInterface;
use litvinjuan\LaravelPayments\Requests\AbstractValidatePaymentRequest;
use litvinjuan\LaravelPayments\Requests\AbstractVoidRequest;
use litvinjuan\LaravelPayments\Util\GatewayShortNameGenerator;

/**
* @method AbstractRequest notification(array $options = array())
* @method AbstractRequest authorize(array $options = array())
* @method AbstractRequest completeAuthorize(array $options = array())
* @method AbstractRequest capture(array $options = array())
* @method AbstractRequest purchase(array $options = array())
* @method AbstractRequest completePurchase(array $options = array())
* @method AbstractRequest refund(array $options = array())
* @method AbstractRequest getTransaction(array $options = array())
* @method AbstractRequest void(array $options = array())
* @method AbstractRequest validateTransaction(array $options = array())
* @method AbstractPaymentNotificationRequest paymentNotification(array $parameters = array())
* @method AbstractAuthorizeRequest authorize(array $parameters = array())
* @method AbstractCompleteAuthorizeRequest completeAuthorize(array $parameters = array())
* @method AbstractCaptureRequest capture(array $parameters = array())
* @method AbstractPurchaseRequest purchase(array $parameters = array())
* @method AbstractCompletePurchaseRequest completePurchase(array $parameters = array())
* @method AbstractRefundRequest refund(array $parameters = array())
* @method AbstractGetPaymentRequest getPayment(array $parameters = array())
* @method AbstractVoidRequest void(array $parameters = array())
* @method AbstractValidatePaymentRequest validatePayment(array $parameters = array())
*
* @method bool supportsNotification(array $options = array())
* @method bool supportsAuthorize(array $options = array())
* @method bool supportsCompleteAuthorize(array $options = array())
* @method bool supportsCapture(array $options = array())
* @method bool supportsPurchase(array $options = array())
* @method bool supportsCompletePurchase(array $options = array())
* @method bool supportsRefund(array $options = array())
* @method bool supportsGetTransaction(array $options = array())
* @method bool supportsVoid(array $options = array())
* @method bool supportsValidateTransaction(array $options = array())
* @method bool supportsPaymentNotification(array $parameters = array())
* @method bool supportsAuthorize(array $parameters = array())
* @method bool supportsCompleteAuthorize(array $parameters = array())
* @method bool supportsCapture(array $parameters = array())
* @method bool supportsPurchase(array $parameters = array())
* @method bool supportsCompletePurchase(array $parameters = array())
* @method bool supportsRefund(array $parameters = array())
* @method bool supportsGetPayment(array $parameters = array())
* @method bool supportsVoid(array $parameters = array())
* @method bool supportsValidatePayment(array $parameters = array())
*/
abstract class AbstractGateway implements GatewayInterface
{
Expand Down Expand Up @@ -64,8 +74,14 @@ public abstract function getDefaultParameters();
* @param string $requestClass
* @param array|null $params
* @return AbstractRequest
* @throws InvalidRequestException
*/
protected final function createRequest(string $requestClass, array $params = null) {
protected final function createRequest(string $requestClass, array $params = null)
{
if (! class_exists($requestClass)) {
throw InvalidRequestException::notFound($requestClass);
}

/** @var AbstractRequest $request */
$request = (new $requestClass());
$request->withParameters($params ?? $this->getDefaultParameters());
Expand Down
40 changes: 20 additions & 20 deletions src/Gateways/GatewayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
use litvinjuan\LaravelPayments\Requests\RequestInterface;

/**
* @method RequestInterface notification(array $options = array())
* @method RequestInterface authorize(array $options = array())
* @method RequestInterface completeAuthorize(array $options = array())
* @method RequestInterface capture(array $options = array())
* @method RequestInterface purchase(array $options = array())
* @method RequestInterface completePurchase(array $options = array())
* @method RequestInterface refund(array $options = array())
* @method RequestInterface getTransaction(array $options = array())
* @method RequestInterface void(array $options = array())
* @method RequestInterface validateTransaction(array $options = array())
* @method RequestInterface paymentNotification(array $parameters = array())
* @method RequestInterface authorize(array $parameters = array())
* @method RequestInterface completeAuthorize(array $parameters = array())
* @method RequestInterface capture(array $parameters = array())
* @method RequestInterface purchase(array $parameters = array())
* @method RequestInterface completePurchase(array $parameters = array())
* @method RequestInterface refund(array $parameters = array())
* @method RequestInterface getPayment(array $parameters = array())
* @method RequestInterface void(array $parameters = array())
* @method RequestInterface validatePayment(array $parameters = array())
*
* @method bool supportsNotification(array $options = array())
* @method bool supportsAuthorize(array $options = array())
* @method bool supportsCompleteAuthorize(array $options = array())
* @method bool supportsCapture(array $options = array())
* @method bool supportsPurchase(array $options = array())
* @method bool supportsCompletePurchase(array $options = array())
* @method bool supportsRefund(array $options = array())
* @method bool supportsGetTransaction(array $options = array())
* @method bool supportsVoid(array $options = array())
* @method bool supportsValidateTransaction(array $options = array())
* @method bool supportsPaymentNotification(array $parameters = array())
* @method bool supportsAuthorize(array $parameters = array())
* @method bool supportsCompleteAuthorize(array $parameters = array())
* @method bool supportsCapture(array $parameters = array())
* @method bool supportsPurchase(array $parameters = array())
* @method bool supportsCompletePurchase(array $parameters = array())
* @method bool supportsRefund(array $parameters = array())
* @method bool supportsGetPayment(array $parameters = array())
* @method bool supportsVoid(array $parameters = array())
* @method bool supportsValidatePayment(array $parameters = array())
*/
interface GatewayInterface
{
Expand Down
42 changes: 0 additions & 42 deletions src/Gateways/MercadoPagoGateway.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/Handlers/ChangePaymentGatewayHandler.php

This file was deleted.

40 changes: 0 additions & 40 deletions src/Handlers/PurchasePaymentHandler.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use litvinjuan\LaravelPayments\Payments\Payment;
use litvinjuan\LaravelPayments\Payments\PaymentState;

class ValidateTransactionPaymentHandler
class ValidatePaymentPaymentHandler
{

/**
Expand All @@ -19,7 +19,7 @@ class ValidateTransactionPaymentHandler
*/
public function handle(Payment $payment): bool
{
if (! $payment->state->is(PaymentState::COMPLETED)) {
if (! $payment->state->is(PaymentState::PURCHASED)) {
return false;
}

Expand All @@ -35,12 +35,12 @@ public function handle(Payment $payment): bool
$gateway = GatewayFactory::make($payment);

// Verify the gateway supports this method
if (! $gateway->supportsValidateTransaction()) {
if (! $gateway->supportsValidatePayment()) {
throw InvalidRequestException::notSupported();
}

// Create and send the request
$response = $gateway->validateTransaction()->payment($payment)->send();
$response = $gateway->validatePayment()->payment($payment)->send();

return $response->validated();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Payments/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Konekt\Enum\Eloquent\CastsEnums;
use litvinjuan\LaravelPayments\Handlers\ValidateTransactionPaymentHandler;
use litvinjuan\LaravelPayments\Handlers\ValidatePaymentPaymentHandler;
use Money\Money;

/**
Expand Down Expand Up @@ -93,7 +93,7 @@ public function setGateway(string $gateway)
public function validate()
{
try {
return (new ValidateTransactionPaymentHandler())->handle($this);
return (new ValidatePaymentPaymentHandler())->handle($this);
} catch (Exception $e) {
return false;
}
Expand Down
32 changes: 28 additions & 4 deletions src/Payments/PaymentState.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,48 @@ class PaymentState extends Enum
const __default = self::PENDING;

const PENDING = 'pending';
const PURCHASED = 'purchased';
const COMPLETED = 'completed';

const AUTHORIZING = 'authorizing';
const AUTHORIZED = 'authorized';

const CAPTURED = 'captured';

const VOIDED = 'voided';

const RETURNED = 'returned';
const PARTIALLY_RETURNED = 'partially-returned';

const PURCHASING = 'purchasing';
const PURCHASED = 'purchased'; // Completed

const FAILED = 'failed';

public static function all()
{
return [
self::PENDING,
self::AUTHORIZING,
self::AUTHORIZED,
self::CAPTURED,
self::VOIDED,
self::RETURNED,
self::PARTIALLY_RETURNED,
self::PURCHASING,
self::PURCHASED,
self::COMPLETED,
self::FAILED,
];
}

protected static $labels = [
self::PENDING => 'Pending',
self::AUTHORIZING => 'Authorizing',
self::AUTHORIZED => 'Authorized',
self::CAPTURED => 'Captured',
self::VOIDED => 'Voided',
self::RETURNED => 'Returned',
self::PARTIALLY_RETURNED => 'Partially Returned',
self::PURCHASING => 'Purchasing',
self::PURCHASED => 'Purchased',
self::COMPLETED => 'Completed',
self::FAILED => 'Failed',
];

Expand Down
Loading

0 comments on commit 1c92451

Please sign in to comment.