Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize documentation & fix stripe event name #9

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 75 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@

[![Total Downloads](https://img.shields.io/packagist/dt/dystcz/lunar-api-stripe-adapter.svg?style=flat-square)](https://packagist.org/packages/dystcz/lunar-api-stripe-adapter)

TODO: Write description
This package provides a Stripe payment adapter for [Lunar API](https://github.com/dystcz/lunar-api).
It can authorize your payments and handle incoming Stripe webhooks.

## Installation
## Getting started

Should be as easy as:

1. Install the package
2. Fill in your env variables
3. Accept payments

### Installation

You can install the package via composer:

Expand All @@ -18,26 +27,80 @@ composer require dystcz/lunar-api-stripe-adapter
You can publish the config file with:

```bash
php artisan vendor:publish --provider="Dystcz\LunarApiStripeAdapter\LunarApiStripeAdapterServiceProvider" --tag="config"
php artisan vendor:publish --provider="Dystcz\LunarApiStripeAdapter\LunarApiStripeAdapterServiceProvider" --tag="lunar-api.stripe"
```

This is the contents of the published config file:
This will publish two configuration files:

```php
return [
'driver' => 'stripe',
'type' => 'card',
];
1. `config/lunar-api/stripe.php` - contains the payment adapter configuration
2. `config/stripe-webhook.php` - contains the webhook configuration

### Configuration

#### Setting up the webhooks

You can configure the Stripe webhooks in the `config/stripe-webhook.php` file.
This package builds on top of Spatie's [laravel-stripe-webhooks](https://github.com/spatie/laravel-stripe-webhooks?tab=readme-ov-file)
package, so you can use the same configuration.
For more configuration options, please refer to the [documentation](https://github.com/spatie/laravel-stripe-webhooks?tab=readme-ov-file)
of the package.

#### Setting up environment variables

Do not forget to fill in your `.env` file with the following variables:

```yaml
STRIPE_PUBLIC_KEY=pk_live_...
STRIPE_SECRET_KEY=sk_live_...
STRIPE_WEBHOOK_SECRET=whsec_...
STRIPE_WEBHOOK_CONNECTION=redis
STRIPE_WEBHOOK_QUEUE=priority
STRIPE_SIGNATURE_VERIFY=true
```

## Usage
### Stripe events and their webhook handlers

Here is a list of Stripe events which are currently handled by this package.
You can easily add your own handlers and register
them in the `config/stripe-webhook.php` file.

You can add a couple of useful methods to your handlers
by extending the `WebhookHandler` class.

#### Currently handled events

| Event | Webhook handler class | Description |
| --- | --- | --- |
| `payment_intent.succeeded` | `HandlePaymentIntentSucceeded` | Dispatches `OrderPaymentCanceled` event. |
| `payment_intent.payment_failed` | `HandlePaymentIntentFailed` | Dispatches `OrderPaymentFailed` event. |
| `payment_intent.canceled` | `HandlePaymentIntentCanceled` | Authorizes the payment via `AuthorizeStripePayment` class which dispatches the `OrderPaymentSuccessful`. |

You can listen to these and [others](https://github.com/dystcz/lunar-api/tree/26c9dedeecddf89a9d2aed418cf965525e393e40/src/Domain/Orders/Events)
events in your application and handle them accordingly.

> [!NOTE]
> All other events are handled by `HandleOtherEvent` class
> which does nothing by default, but you can easily swap the default
> handler for your own in the config.

### Advanced usage

If you ever need to implement custom logic, you can use the methods listed below.

```php
$payment = App::make(StripePaymentAdapter::class);

// Get payment driver
$payment->getDriver();

// Get payment type
$payment->getType();

// Create a payment intent
App::make(StripePaymentAdapter::class)->createIntent($cart)
$payment->createIntent($cart)

// Handle a webhook (validate and authorize payment)
App::make(StripePaymentAdapter::class)->handleWebhook($request)
$payment->handleWebhook($request)
```

## Testing
Expand Down
2 changes: 1 addition & 1 deletion config/stripe-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'payment_intent_created' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentCreated::class,
'payment_intent_succeeded' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentSucceeded::class,
'payment_intent_payment_failed' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentFailed::class,
'payment_intent_cancelled' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentCancelled::class,
'payment_intent_canceled' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentCanceled::class,
// 'payment_intent_processing' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentProcessing::class,
// 'payment_intent_requires_action' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandlePaymentIntentRequiresAction::class,
// 'source_chargeable' => \Dystcz\LunarApiStripeAdapter\Jobs\Webhooks\HandleChargeableSource::class,
Expand Down
14 changes: 0 additions & 14 deletions config/stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,4 @@
|
*/
'type' => 'stripe',

/**
* Automatic payment methods
*
* Enable automatic payment methods.
*/
'automatic_payment_methods' => true,

/**
* Payment method types
*
* The payment method types that the payment intent will accept.
*/
'payment_method_types' => ['card'],
];
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Dystcz\LunarApi\Domain\Orders\Events\OrderPaymentCanceled;

class HandlePaymentIntentCancelled extends WebhookHandler
class HandlePaymentIntentCanceled extends WebhookHandler
{
/**
* Handle failed payment intent.
Expand Down
4 changes: 2 additions & 2 deletions src/LunarApiStripeAdapterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public function boot(): void
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/stripe.php' => config_path('lunar-api/stripe.php'),
], 'lunar-api.stripe.config');
], 'lunar-api.stripe');

$this->publishes([
__DIR__.'/../config/stripe-webhooks.php' => config_path('stripe-webhooks.php'),
], 'lunar-api.stripe.config');
], 'lunar-api.stripe');
}
}
}
2 changes: 1 addition & 1 deletion tests/Feature/HandleStripeWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function constructEvent(string $jsonPayload, string $signature, string $s
// Event::assertDispatched(OrderPaymentSuccessful::class);
})->group('webhooks');

it('can handle payment_intent.cancelled event', function () {
it('can handle payment_intent.canceled event', function () {
/** @var TestCase $this */
Event::fake();
Queue::fake();
Expand Down
Loading