Skip to content

Commit

Permalink
Merge pull request #2 from dystcz/0.6
Browse files Browse the repository at this point in the history
Return client secret when creating payment intent
  • Loading branch information
theimerj authored Oct 3, 2023
2 parents e462c36 + e21d3c4 commit 81eded6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 24 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/phpstan.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: PHPStan

on:
push:
paths:
- '**.php'
- 'phpstan.neon.dist'
# push:
# paths:
# - '**.php'
# - 'phpstan.neon.dist'

jobs:
phpstan:
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dystcz/lunar-api-stripe-adapter",
"description": "This is my package lunar-api-stripe-adapter",
"description": "Lunar API Stripe Adapter",
"keywords": [
"dystcz",
"lunar",
Expand All @@ -20,10 +20,9 @@
],
"require": {
"php": "^8.2",
"dystcz/lunar-api": "^0.6",
"dystcz/lunar-api": "^0.6.0",
"illuminate/contracts": "^10.0",
"lunarphp/stripe": "^0.6",
"spatie/laravel-package-tools": "^1.14.0"
"lunarphp/stripe": "^0.6.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
3 changes: 3 additions & 0 deletions config/lunar-api-stripe-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
return [
'driver' => 'stripe',
'type' => 'card',
'automatic_payment_methods' => true,
'payment_method_types' => ['card'],
'capture_method' => 'automatic',
];
30 changes: 18 additions & 12 deletions src/LunarApiStripeAdapterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

namespace Dystcz\LunarApiStripeAdapter;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Dystcz\LunarApiStripeAdapter\Managers\StripeManager;
use Illuminate\Support\ServiceProvider;

class LunarApiStripeAdapterServiceProvider extends PackageServiceProvider
class LunarApiStripeAdapterServiceProvider extends ServiceProvider
{
public function configurePackage(Package $package): void
/**
* Bootstrap the application services.
*/
public function boot(): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package->name('lunar-api-stripe-adapter')->hasConfigFile();
$this->app->singleton('gc:stripe', function ($app) {
return $app->make(StripeManager::class);
});

StripePaymentAdapter::register();
}

public function packageRegistered()
/**
* Register the application services.
*/
public function register(): void
{
StripePaymentAdapter::register();
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/lunar-api-stripe-adapter.php', 'lunar-api-stripe-adapter');
}
}
51 changes: 51 additions & 0 deletions src/Managers/StripeManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Dystcz\LunarApiStripeAdapter\Managers;

use Illuminate\Support\Facades\Config;
use Lunar\Stripe\Managers\StripeManager as LunarStripeManager;
use Stripe\PaymentIntent;

class StripeManager extends LunarStripeManager
{
public function __construct()
{
parent::__construct();
}

/**
* Build the intent
*
* @param int $value
* @param string $currencyCode
* @param \Lunar\Models\CartAddress $shipping
*/
protected function buildIntent($value, $currencyCode, $shipping): PaymentIntent
{
$intentData = [
'amount' => $value,
'currency' => $currencyCode,
'capture_method' => Config::get('lunar-api-stripe-adapter.capture_method', 'automatic'),
'shipping' => [
'name' => "{$shipping->first_name} {$shipping->last_name}",
'address' => [
'city' => $shipping->city,
'country' => $shipping->country->iso2,
'line1' => $shipping->line_one,
'line2' => $shipping->line_two,
'postal_code' => $shipping->postcode,
'state' => $shipping->state,
],
],
];

$intentData = array_merge(
$intentData,
Config::get('lunar-api-stripe-adapter.automatic_payment_methods.enabled', false)
? ['automatic_payment_methods' => ['enabled' => 'true']]
: ['payment_method_types' => Config::get('lunar-api-stripe-adapter.payment_method_types', ['card'])]
);

return PaymentIntent::create($intentData);
}
}
5 changes: 3 additions & 2 deletions src/StripePaymentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public function createIntent(Cart $cart): PaymentIntent
{
$this->cart = $cart;

/** @var \Stripe\PaymentIntent $intent */
/** @var Stripe\PaymentIntent $intent */
$intent = StripeFacade::createIntent($cart->calculate());

$this->createTransaction($intent->id, $intent->amount);

return new PaymentIntent(
id: $intent->id
id: $intent->id,
client_secret: $intent->client_secret,
);
}

Expand Down
3 changes: 1 addition & 2 deletions tests/CreatePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@

expect($response->json('meta.payment_intent.id'))
->toBe($this->cart->fresh()->meta['payment_intent']);
})
->with(['stripe']);
})->with(['stripe']);

0 comments on commit 81eded6

Please sign in to comment.