Skip to content

Commit

Permalink
Merge pull request #3 from xujiongze/master
Browse files Browse the repository at this point in the history
Add support vanilo 4.X
  • Loading branch information
fulopattila122 authored Jul 24, 2024
2 parents ae45e0e + 1d8c42d commit cbc4164
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 18 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
"issues": "https://github.com/vanilophp/stripe"
},
"require": {
"php": "^7.4|^8.0",
"konekt/concord": "^1.5",
"vanilo/payment": "^2.1",
"vanilo/contracts": "^2.1",
"php": "^8.2",
"konekt/concord": "^1.13",
"laravel/framework": "^10.0|^11.0",
"vanilo/payment": "^4.0",
"vanilo/contracts": "^4.0",
"stripe/stripe-php": "^7.75"
},
"autoload": {
Expand Down
20 changes: 17 additions & 3 deletions src/Factories/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@

use Illuminate\Http\Request;
use Stripe\Event;
use Stripe\Stripe;
use Stripe\PaymentIntent;
use Vanilo\Payment\Contracts\PaymentResponse;
use Vanilo\Stripe\Messages\StripePaymentResponse;
use Vanilo\Stripe\Messages\StripeReturnPaymentResponse;
use Vanilo\Stripe\Messages\StripeWebhookPaymentResponse;
use Vanilo\Stripe\Concerns\HasStripeConfiguration;

final class ResponseFactory
{
public function create(Request $request, array $options): PaymentResponse
use HasStripeConfiguration;

public function create(Request $request, array $options,$secretKey): PaymentResponse
{
return new StripePaymentResponse(
if($request->payment_intent){
$this->secretKey = $secretKey;
Stripe::setApiKey($this->secretKey);
return new StripeReturnPaymentResponse(
PaymentIntent::retrieve($request->payment_intent, [])
);
}

return new StripeWebhookPaymentResponse(
Event::constructFrom($request->all())
);
}
Expand Down
29 changes: 22 additions & 7 deletions src/Messages/StripePaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ class StripePaymentRequest implements PaymentRequest
public function getHtmlSnippet(array $options = []): ?string
{
Stripe::setApiKey($this->secretKey);
$paymentIntent = PaymentIntent::create([
'amount' => $this->amount * 100,
'currency' => $this->currency,
'metadata' => [
'payment_id' => $this->paymentId
]
]);
$paymentIntent =$this->createIntent();

return View::make(
$this->view,
Expand All @@ -45,6 +39,22 @@ public function getHtmlSnippet(array $options = []): ?string
)->render();
}

public function createIntent()
{
Stripe::setApiKey($this->secretKey);
return PaymentIntent::create([
'amount' => $this->amount * 100,
'currency' => $this->currency,
'metadata' => [
'payment_id' => $this->paymentId
]
]);
}

public function getPublicKey ()
{
return $this->publicKey;
}
public function willRedirect(): bool
{
return true;
Expand Down Expand Up @@ -85,6 +95,11 @@ public function setAmount(float $amount): self
return $this;
}

public function getRemoteId(): ?string
{
return null;
}

public function setView(string $view): self
{
$this->view = $view;
Expand Down
78 changes: 78 additions & 0 deletions src/Messages/StripeReturnPaymentResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Vanilo\Stripe\Messages;

use Konekt\Enum\Enum;
use Stripe\PaymentIntent;
use Stripe\Event;
use Vanilo\Payment\Contracts\PaymentResponse;
use Vanilo\Payment\Contracts\PaymentStatus;
use Vanilo\Payment\Models\PaymentStatusProxy;
use Vanilo\Stripe\Models\StripeEventType;

class StripeReturnPaymentResponse implements PaymentResponse
{
protected PaymentIntent $intent;

protected ?PaymentStatus $status = null;

public function __construct(PaymentIntent $intent)
{
$this->intent = $intent;
}

public function wasSuccessful(): bool
{
return StripeEventType::INTENT_SUCCEEDED === $this->intent->status;
}

public function getMessage(): ?string
{
return $this->intent->description;
}

public function getTransactionId(): ?string
{
return $this->intent->id;
}

public function getTransactionAmount(): float
{
return $this->getAmountPaid() ?? 0;
}

public function getAmountPaid(): ?float
{
return (float) $this->intent->amount / 100;
}

public function getPaymentId(): string
{
return $this->intent->metadata->payment_id;
}

public function getStatus(): PaymentStatus
{
if (null === $this->status) {
switch ($this->getNativeStatus()->value()) {
case StripeEventType::INTENT_SUCCEEDED:
$this->status = PaymentStatusProxy::PAID();
break;
case StripeEventType::INTENT_PROCESSING:
$this->status = PaymentStatusProxy::ON_HOLD();
break;
default:
$this->status = PaymentStatusProxy::DECLINED();
}
}

return $this->status;
}

public function getNativeStatus(): Enum
{
return StripeEventType::create($this->eventType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Vanilo\Payment\Models\PaymentStatusProxy;
use Vanilo\Stripe\Models\StripeEventType;

class StripePaymentResponse implements PaymentResponse
class StripeWebhookPaymentResponse implements PaymentResponse
{
protected string $eventType;

Expand Down Expand Up @@ -45,6 +45,11 @@ public function getTransactionId(): ?string
return $this->charge->id;
}

public function getTransactionAmount(): float
{
return $this->getAmountPaid() ?? 0;
}

public function getAmountPaid(): ?float
{
if ($this->getNativeStatus()->isChargeRefunded()) {
Expand All @@ -56,7 +61,7 @@ public function getAmountPaid(): ?float

public function getPaymentId(): string
{
return $this->charge->metadata->payment_id;
return $this->charge->metadata->payment_id ?? "-1";
}

public function getStatus(): PaymentStatus
Expand Down
8 changes: 7 additions & 1 deletion src/Models/StripeEventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ class StripeEventType extends Enum
public const CHARGE_SUCCEEDED = 'charge.succeeded';
public const CHARGE_UPDATED = 'charge.updated';

protected static $unknownValuesFallbackToDefault = true;
public const INTENT_REQUIRES_PAYMENT_METHOD = 'requires_payment_method';
public const INTENT_REQUIRES_CONFIRMATION = 'requires_confirmation';
public const INTENT_REQUIRES_ACTION = 'requires_action';
public const INTENT_PROCESSING = 'processing';
public const INTENT_SUCCEEDED = 'succeeded';
public const INTENT_CANCELED = 'canceled';

}
13 changes: 12 additions & 1 deletion src/StripePaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Vanilo\Stripe\Concerns\HasStripeInteraction;
use Vanilo\Stripe\Factories\RequestFactory;
use Vanilo\Stripe\Factories\ResponseFactory;
use Vanilo\Payment\Contracts\TransactionHandler;

class StripePaymentGateway implements PaymentGateway
{
Expand Down Expand Up @@ -57,7 +58,17 @@ public function processPaymentResponse(Request $request, array $options = []): P
$this->responseFactory = new ResponseFactory();
}

return $this->responseFactory->create($request, $options);
return $this->responseFactory->create($request, $options,$this->secretKey);
}

public static function svgIcon(): string
{
return self::$svg ??= file_get_contents(__DIR__ . '/resources/logo.svg');
}

public function transactionHandler(): ?TransactionHandler
{
return null;
}

public function isOffline(): bool
Expand Down
1 change: 1 addition & 0 deletions src/resources/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cbc4164

Please sign in to comment.