├── .editorconfig ├── .github ├── CONTRIBUTING.md ├── FUNDING.yml ├── SECURITY.md └── workflows │ ├── backward-compat.yml │ ├── infection.yml │ ├── phpstan.yml │ ├── pint.yml │ └── run-tests.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json ├── composer.lock ├── config └── stripe-integration.php ├── docs └── usage.md ├── infection.json5 ├── phpstan.neon ├── phpunit.xml.dist ├── pint.json ├── src ├── Behaviors │ ├── ConfiguresCashier.php │ ├── ManagesPaymentIntents.php │ └── PreparesPaymentMethods.php ├── DataTransferObjects │ ├── OffsessionChargeData.php │ ├── PaymentIntentData.php │ ├── PaymentMethodAttachmentData.php │ └── StripeChargeData.php ├── Decorators │ └── StripePaymentAmount.php ├── Providers │ └── StripePaymentProvider.php └── StripeIntegrationServiceProvider.php └── tests ├── AmountDecoratorTest.php ├── ConditionableTest.php ├── PackageConfigurationTest.php ├── StripeChargeTest.php ├── StripePaymentProviderTest.php ├── Stubs └── User.php └── TestCase.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_size = 4 6 | indent_style = space 7 | end_of_line = lf 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - # **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: ["https://paypal.me/observername"] 2 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you discover any security related issues, please email `contact@observer.name` instead of using the issue tracker. 4 | -------------------------------------------------------------------------------- /.github/workflows/backward-compat.yml: -------------------------------------------------------------------------------- 1 | name: backward-compat 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | backwards-compatibility-check: 11 | name: "Backwards-compatibility check" 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | with: 16 | fetch-depth: 0 17 | 18 | - name: "Install PHP" 19 | uses: shivammathur/setup-php@v2 20 | with: 21 | php-version: "8.2" 22 | 23 | - name: "Install dependencies" 24 | run: "composer install" 25 | 26 | - name: "Check for BC breaks" 27 | run: "vendor/bin/roave-backward-compatibility-check --format=github-actions" 28 | -------------------------------------------------------------------------------- /.github/workflows/infection.yml: -------------------------------------------------------------------------------- 1 | name: infection 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | infection: 11 | name: Mutation testing 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Setup PHP 17 | uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: '8.1' 20 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, fileinfo, sodium 21 | coverage: xdebug 22 | 23 | - name: Cache composer dependencies 24 | uses: actions/cache@v2 25 | with: 26 | path: vendor 27 | key: composer-${{ hashFiles('composer.lock') }} 28 | 29 | - name: Run composer install 30 | run: composer install -n --prefer-dist 31 | 32 | - name: Run infection 33 | run: ./vendor/bin/infection --show-mutations --min-msi=100 --min-covered-msi=100 34 | -------------------------------------------------------------------------------- /.github/workflows/phpstan.yml: -------------------------------------------------------------------------------- 1 | name: phpstan 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | psalm: 11 | name: Running static analysis 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | 16 | - name: Setup PHP 17 | uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: '8.1' 20 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl 21 | coverage: none 22 | 23 | - name: Cache composer dependencies 24 | uses: actions/cache@v2 25 | with: 26 | path: vendor 27 | key: composer-${{ hashFiles('composer.lock') }} 28 | 29 | - name: Run composer install 30 | run: composer install -n --prefer-dist 31 | 32 | - name: Run phpstan 33 | run: ./vendor/bin/phpstan 34 | -------------------------------------------------------------------------------- /.github/workflows/pint.yml: -------------------------------------------------------------------------------- 1 | name: pint 2 | on: 3 | push: 4 | branches: [ main ] 5 | pull_request: 6 | branches: [ main ] 7 | 8 | jobs: 9 | pint: 10 | name: Running Laravel Pint 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | 15 | - name: Setup PHP 16 | uses: shivammathur/setup-php@v2 17 | with: 18 | php-version: '8.1' 19 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl 20 | coverage: none 21 | 22 | - name: Cache composer dependencies 23 | uses: actions/cache@v2 24 | with: 25 | path: vendor 26 | key: composer-${{ hashFiles('composer.lock') }} 27 | 28 | - name: Run composer install 29 | run: composer install -n --prefer-dist 30 | 31 | - name: Run Laravel Pint 32 | run: ./vendor/bin/pint --test -v 33 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | test: 11 | runs-on: ${{ matrix.os }} 12 | strategy: 13 | fail-fast: true 14 | matrix: 15 | os: [ubuntu-latest, windows-latest] 16 | php: [8.1, 8.2] 17 | laravel: [10.*, 9.*] 18 | stability: [prefer-lowest, prefer-stable] 19 | 20 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} 21 | 22 | steps: 23 | - name: Checkout code 24 | uses: actions/checkout@v2 25 | 26 | - name: Setup PHP 27 | uses: shivammathur/setup-php@v2 28 | with: 29 | php-version: ${{ matrix.php }} 30 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, fileinfo, sodium 31 | coverage: xdebug 32 | 33 | - name: Setup problem matchers 34 | run: | 35 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 36 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 37 | 38 | - name: Install dependencies 39 | run: | 40 | composer require "laravel/framework:${{ matrix.laravel }}" "nesbot/carbon:^2.64.1" --dev --no-interaction --no-update 41 | composer update --${{ matrix.stability }} --prefer-dist --no-interaction 42 | 43 | - name: Execute tests 44 | run: vendor/bin/phpunit --coverage-clover build/clover.xml 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .php_cs 3 | .php_cs.cache 4 | .phpunit.result.cache 5 | build 6 | coverage 7 | phpunit.xml 8 | psalm.xml 9 | testbench.yaml 10 | vendor 11 | node_modules 12 | .php-cs-fixer.cache 13 | infection.log 14 | infection.html 15 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Michael Rubel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Stripe Charge](https://github.com/michael-rubel/laravel-stripe-integration/assets/37669560/f1767ad2-64ec-414b-9fcb-51c6806765c7) 2 | 3 | # Laravel Stripe Integration 4 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/michael-rubel/laravel-stripe-integration.svg?style=flat-square&logo=packagist)](https://packagist.org/packages/michael-rubel/laravel-stripe-integration) 5 | [![Code Quality](https://img.shields.io/scrutinizer/quality/g/michael-rubel/laravel-stripe-integration.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/michael-rubel/laravel-stripe-integration/?branch=main) 6 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/michael-rubel/laravel-stripe-integration.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/michael-rubel/laravel-stripe-integration/?branch=main) 7 | [![Infection](https://img.shields.io/github/actions/workflow/status/michael-rubel/laravel-stripe-integration/infection.yml?branch=main&style=flat-square&label=infection&logo=php)](https://github.com/michael-rubel/laravel-stripe-integration/actions) 8 | [![Larastan](https://img.shields.io/github/actions/workflow/status/michael-rubel/laravel-stripe-integration/phpstan.yml?branch=main&style=flat-square&label=larastan&logo=laravel)](https://github.com/michael-rubel/laravel-stripe-integration/actions) 9 | 10 | This package is a ready-to-use integration with Stripe. The package uses `laravel/cashier` package internally. 11 | 12 | PHP `^8.1` and Laravel `^9.0` is required to use this package. 13 | 14 | ## #StandWithUkraine 15 | [![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md) 16 | 17 | ### Features supported 18 | - Basic card charge 19 | - "Off-session" charge 20 | - Payment intent management 21 | 22 | ## Installation 23 | Install the package using composer: 24 | ```bash 25 | composer require michael-rubel/laravel-stripe-integration 26 | ``` 27 | 28 | Publish the config and fill Stripe keys in `.env`: 29 | ```bash 30 | php artisan vendor:publish --tag="stripe-integration-config" 31 | ``` 32 | 33 | ## Main classes 34 | - [`StripePaymentProvider`](https://github.com/michael-rubel/laravel-stripe-integration/blob/main/src/Providers/StripePaymentProvider.php) 35 | - [`StripePaymentAmount`](https://github.com/michael-rubel/laravel-stripe-integration/blob/main/src/Decorators/StripePaymentAmount.php) 36 | 37 | [Usage example](https://github.com/michael-rubel/laravel-stripe-integration/blob/main/docs/usage.md) 38 | 39 | ## Testing 40 | ```bash 41 | composer test 42 | ``` 43 | 44 | ## License 45 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 46 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "michael-rubel/laravel-stripe-integration", 3 | "description": "This package represents ready-to-use integration with Stripe.", 4 | "keywords": [ 5 | "michael-rubel", 6 | "laravel", 7 | "laravel-stripe-integration" 8 | ], 9 | "homepage": "https://github.com/michael-rubel/laravel-stripe-integration", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Michael Rubel", 14 | "email": "michael@laravel.software", 15 | "role": "Maintainer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.1", 20 | "illuminate/contracts": "^9.0|^10.0", 21 | "laravel/cashier": "^13.0|^14.0", 22 | "spatie/laravel-package-tools": "^1.11", 23 | "michael-rubel/laravel-enhanced-container": "^10.0|^11.0" 24 | }, 25 | "require-dev": { 26 | "brianium/paratest": "^6.3", 27 | "infection/infection": "^0.27.0", 28 | "laravel/pint": "^1.0", 29 | "mockery/mockery": "^1.4.4", 30 | "nunomaduro/collision": "^6.0", 31 | "nunomaduro/larastan": "^2.0", 32 | "orchestra/testbench": "^7.0|^8.0", 33 | "phpunit/phpunit": "^9.5", 34 | "roave/backward-compatibility-check": "^7.0|^8.0" 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "MichaelRubel\\StripeIntegration\\": "src" 39 | } 40 | }, 41 | "autoload-dev": { 42 | "psr-4": { 43 | "MichaelRubel\\StripeIntegration\\Tests\\": "tests" 44 | } 45 | }, 46 | "scripts": { 47 | "test": "./vendor/bin/testbench package:test --no-coverage", 48 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 49 | }, 50 | "config": { 51 | "sort-packages": true, 52 | "allow-plugins": { 53 | "infection/extension-installer": true 54 | } 55 | }, 56 | "extra": { 57 | "laravel": { 58 | "providers": [ 59 | "MichaelRubel\\StripeIntegration\\StripeIntegrationServiceProvider" 60 | ] 61 | } 62 | }, 63 | "minimum-stability": "dev", 64 | "prefer-stable": true 65 | } 66 | -------------------------------------------------------------------------------- /config/stripe-integration.php: -------------------------------------------------------------------------------- 1 | env('STRIPE_KEY'), 17 | 'secret' => env('STRIPE_SECRET'), 18 | 19 | ]; 20 | -------------------------------------------------------------------------------- /docs/usage.md: -------------------------------------------------------------------------------- 1 | ## Usage example 2 | ```php 3 | /* 4 | |-------------------------------------------------------------------------- 5 | | Notes 6 | |-------------------------------------------------------------------------- 7 | | Don't copy & paste the code below. It's only an example flow 8 | | definition. You should change the code based on your needs. 9 | | 10 | | If you wonder what is the `CallProxy`, it is a method binding mechanism, 11 | | i.e. you can use this to mock methods through the Service Container. 12 | | https://github.com/michael-rubel/laravel-enhanced-container#method-binding 13 | */ 14 | 15 | class StripeCharge implements Action 16 | { 17 | /** 18 | * @var CallProxy 19 | */ 20 | private CallProxy $paymentProvider; 21 | 22 | /** 23 | * @param PaymentProviderContract $paymentProvider 24 | */ 25 | public function __construct(PaymentProviderContract $paymentProvider) 26 | { 27 | $this->paymentProvider = call($paymentProvider); 28 | } 29 | 30 | /** 31 | * Execute the job. 32 | * 33 | * @return mixed 34 | */ 35 | public function handle(): mixed 36 | { 37 | $this->paymentProvider->setCashierCurrencyAs(new Currency('USD')); 38 | 39 | $customer = $this->paymentProvider->makeCustomerUsing(auth()->user()); 40 | 41 | $paymentMethod = $this->paymentProvider->setPaymentMethodFor(auth()->user(), 'payment_method'); 42 | 43 | $this->paymentProvider->attachPaymentMethodToCustomer( 44 | new PaymentMethodAttachmentData( 45 | paymentMethod: $paymentMethod, 46 | customer: $customer, 47 | ) 48 | ); 49 | 50 | $amount = new StripePaymentAmount( 51 | amount: 1000, 52 | currency: config('cashier.currency'), 53 | ); 54 | 55 | $chargeData = new StripeChargeData( 56 | model: auth()->user(), 57 | paymentAmount: $amount, 58 | paymentMethod: $paymentMethod, 59 | options: ['description' => 'Your nice description.'], 60 | ); 61 | 62 | $payment = $this->paymentProvider->charge($chargeData); 63 | 64 | // you can check $payment->status now 65 | } 66 | } 67 | ``` 68 | -------------------------------------------------------------------------------- /infection.json5: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "vendor/infection/infection/resources/schema.json", 3 | "source": { 4 | "directories": [ 5 | "." 6 | ], 7 | "excludes": [ 8 | "vendor", 9 | "tests" 10 | ] 11 | }, 12 | "logs": { 13 | "text": "infection.log", 14 | "html": "infection.html", 15 | "github": true 16 | }, 17 | "mutators": { 18 | "@default": true 19 | } 20 | } -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - ./vendor/nunomaduro/larastan/extension.neon 3 | 4 | parameters: 5 | 6 | paths: 7 | - src 8 | 9 | level: max 10 | 11 | ignoreErrors: 12 | - '#Access to an undefined property Laravel\\Cashier\\PaymentMethod\|Stripe\\PaymentMethod\:\:\$id\.#' 13 | - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Model\:\:#' 14 | - '#Parameter \#1 \$config of class Stripe\\StripeClient constructor expects array\\|string, mixed given\.#' 15 | - '#Access to an undefined property Illuminate\\Database\\Eloquent\\Model\:\:\$stripe_id\.#' 16 | - '#Unable to resolve the template type TValue in call to function collect#' 17 | - '#Parameter \#1 \$code of class Money\\Currency constructor expects non\-empty\-string, string given\.#' 18 | - '#Call to an undefined method MichaelRubel\\EnhancedContainer\\Core\\CallProxy\:\:(.*)#' 19 | - '#Cannot call method stripeId\(\) on Illuminate\\Database\\Eloquent\\Model\|null\.#' 20 | - '#Cannot call method (.*) on mixed\.#' 21 | 22 | checkMissingIterableValueType: false 23 | 24 | reportUnmatchedIgnoredErrors: false 25 | 26 | checkOctaneCompatibility: true 27 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 23 | tests 24 | 25 | 26 | 27 | 28 | ./src 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /pint.json: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "laravel", 3 | "rules": { 4 | "phpdoc_separation": false, 5 | "declare_strict_types": true, 6 | "concat_space": { 7 | "spacing": "one" 8 | }, 9 | "class_attributes_separation": { 10 | "elements": { 11 | "const": "only_if_meta", 12 | "property": "only_if_meta" 13 | } 14 | }, 15 | "binary_operator_spaces": { 16 | "default": "single_space", 17 | "operators": {"=>": null, "=": "align_single_space_minimal"} 18 | }, 19 | "no_superfluous_phpdoc_tags": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Behaviors/ConfiguresCashier.php: -------------------------------------------------------------------------------- 1 | $currency->getCode(), 22 | ]); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Behaviors/ManagesPaymentIntents.php: -------------------------------------------------------------------------------- 1 | 'off_session']) 26 | ->merge($options) 27 | ->toArray(); 28 | 29 | return call($model)->createSetupIntent($options); 30 | } 31 | 32 | /** 33 | * Create a payment intent. 34 | * 35 | * @param PaymentIntentData $data 36 | * 37 | * @return PaymentIntent 38 | */ 39 | public function createPaymentIntent(PaymentIntentData $data): PaymentIntent 40 | { 41 | $initialParams = [ 42 | 'amount' => $data->paymentAmount?->getAmount(), 43 | 'currency' => $data->paymentAmount?->getCurrency()->getCode(), 44 | 'payment_method_types' => ['card'], 45 | ]; 46 | 47 | $intentParams = collect($initialParams) 48 | ->when($data->model?->stripeId(), function (Collection $params) use ($data) { 49 | return $params->merge(['customer' => $data->model->stripeId()]); 50 | }) 51 | ->merge($data->params) 52 | ->toArray(); 53 | 54 | return call($this->stripeClient->paymentIntents)->create($intentParams, $data->options); 55 | } 56 | 57 | /** 58 | * Update the payment intent. 59 | * 60 | * @param PaymentIntentData $data 61 | * 62 | * @return PaymentIntent 63 | */ 64 | public function updatePaymentIntent(PaymentIntentData $data): PaymentIntent 65 | { 66 | $updateIntentParams = collect($data->params) 67 | ->when($data->model?->stripeId(), function (Collection $params) use ($data) { 68 | return $params->merge(['customer' => $data->model->stripeId()]); 69 | }) 70 | ->toArray(); 71 | 72 | return call($this->stripeClient->paymentIntents)->update( 73 | $data->intentId, $updateIntentParams, $data->options, 74 | ); 75 | } 76 | 77 | /** 78 | * Retrieve the payment intent. 79 | * 80 | * @param PaymentIntentData $data 81 | * 82 | * @return PaymentIntent 83 | */ 84 | public function retrievePaymentIntent(PaymentIntentData $data): PaymentIntent 85 | { 86 | return call($this->stripeClient->paymentIntents)->retrieve( 87 | $data->intentId, $data->params, $data->options, 88 | ); 89 | } 90 | 91 | /** 92 | * Confirm the payment intent. 93 | * 94 | * @param PaymentIntentData $data 95 | * 96 | * @return PaymentIntent 97 | */ 98 | public function confirmPaymentIntent(PaymentIntentData $data): PaymentIntent 99 | { 100 | return call($this->stripeClient->paymentIntents)->confirm( 101 | $data->paymentIntent->id ?? $data->intentId, $data->params, $data->options, 102 | ); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/Behaviors/PreparesPaymentMethods.php: -------------------------------------------------------------------------------- 1 | createOrGetStripeCustomer(); 25 | } 26 | 27 | /** 28 | * Update a default payment method. 29 | * 30 | * @param Model $model 31 | * @param PaymentMethod|string $paymentMethod 32 | * 33 | * @return CashierPaymentMethod|null 34 | */ 35 | public function setPaymentMethodFor(Model $model, PaymentMethod|string $paymentMethod): ?CashierPaymentMethod 36 | { 37 | return call($model)->updateDefaultPaymentMethod($paymentMethod); 38 | } 39 | 40 | /** 41 | * Attach the payment method to the customer. 42 | * 43 | * @param PaymentMethodAttachmentData $data 44 | * 45 | * @return PaymentMethod 46 | */ 47 | public function attachPaymentMethodToCustomer(PaymentMethodAttachmentData $data): PaymentMethod 48 | { 49 | $params = collect(['customer' => $data->customer->id]) 50 | ->merge($data->params) 51 | ->toArray(); 52 | 53 | return call($this->stripeClient->paymentMethods)->attach( 54 | $data->paymentMethod->id, $params, $data->options 55 | ); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/DataTransferObjects/OffsessionChargeData.php: -------------------------------------------------------------------------------- 1 | multiplier === 0) { 32 | throw new \DivisionByZeroError('Zero is forbidden.'); 33 | } 34 | 35 | if (empty($this->currency)) { 36 | throw new \InvalidArgumentException('Currency code should not be empty.'); 37 | } 38 | 39 | // Configures the payment amount decorator. 40 | // Used mainly for converting the amount 41 | // to payment-system friendly units. 42 | $this->money = new Money( 43 | amount: $this->toPaymentSystemUnits(), 44 | currency: new Currency( 45 | Str::upper($this->currency) 46 | ) 47 | ); 48 | } 49 | 50 | /** 51 | * @return int 52 | */ 53 | public function getAmount(): int 54 | { 55 | return (int) $this->money->getAmount(); 56 | } 57 | 58 | /** 59 | * Convert to "smallest common currency units". 60 | * 61 | * @return int 62 | */ 63 | public function toPaymentSystemUnits(): int 64 | { 65 | return (int) ($this->amount * $this->multiplier); 66 | } 67 | 68 | /** 69 | * Revert from "smallest common currency units". 70 | * 71 | * @return float 72 | */ 73 | public function fromPaymentSystemUnits(): float 74 | { 75 | return $this->money->getAmount() / $this->multiplier; 76 | } 77 | 78 | /** 79 | * Forward call to the money object if the method doesn't exist in the decorator. 80 | * 81 | * @param string $method 82 | * @param array $parameters 83 | * @return mixed 84 | */ 85 | public function __call(string $method, array $parameters): mixed 86 | { 87 | return call($this->money)->{$method}(...$parameters); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Providers/StripePaymentProvider.php: -------------------------------------------------------------------------------- 1 | model)->charge( 52 | $data->paymentAmount->getAmount(), 53 | $data->paymentMethod->id, 54 | $data->options, 55 | ); 56 | } 57 | 58 | /** 59 | * Perform an "off-session" charge. 60 | * 61 | * @param OffsessionChargeData $data 62 | * 63 | * @return PaymentIntent 64 | * @throws ApiErrorException 65 | */ 66 | public function offsessionCharge(OffsessionChargeData $data): PaymentIntent 67 | { 68 | $paymentIntent = $this->createPaymentIntent(new PaymentIntentData( 69 | paymentAmount: $data->paymentAmount, 70 | model: $data->model, 71 | params: $data->intentParams, 72 | options: $data->intentOptions, 73 | )); 74 | 75 | $confirmationParams = collect(['payment_method' => call($data->model)->defaultPaymentMethod()?->id]) 76 | ->merge($data->confirmationParams) 77 | ->toArray(); 78 | 79 | return $this->confirmPaymentIntent(new PaymentIntentData( 80 | paymentIntent: $paymentIntent, 81 | params: $confirmationParams, 82 | options: $data->confirmationOptions, 83 | )); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/StripeIntegrationServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('laravel-stripe-integration') 24 | ->hasConfigFile(); 25 | } 26 | 27 | /** 28 | * Register any package services. 29 | * 30 | * @return void 31 | */ 32 | public function packageRegistered(): void 33 | { 34 | $this->app->bind(StripeClient::class, fn () => new StripeClient( 35 | config('stripe-integration.secret', env('STRIPE_SECRET')) 36 | )); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/AmountDecoratorTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(StripePaymentAmount::class, $decorator); 19 | } 20 | 21 | /** @test */ 22 | public function testThrowsDivisionByZeroError() 23 | { 24 | $this->expectException(\DivisionByZeroError::class); 25 | 26 | new StripePaymentAmount(20000, 'pln', 0); 27 | } 28 | 29 | /** @test */ 30 | public function testThrowsInvalidArgumentWhenCurrencyIsEmpty() 31 | { 32 | $this->expectException(\InvalidArgumentException::class); 33 | 34 | new StripePaymentAmount(10, ''); 35 | } 36 | 37 | /** @test */ 38 | public function testCanConvertToSmallestCommonCurrencyUnit() 39 | { 40 | $decorator = new StripePaymentAmount(1005, 'pln'); 41 | $converted = $decorator->toPaymentSystemUnits(); 42 | $this->assertSame(100500, $converted); 43 | 44 | $decorator = new StripePaymentAmount(1005.50, 'pln'); 45 | $converted = $decorator->toPaymentSystemUnits(); 46 | $this->assertSame(100550, $converted); 47 | 48 | $decorator = new StripePaymentAmount(1005.550, 'pln'); 49 | $converted = $decorator->toPaymentSystemUnits(); 50 | $this->assertSame(100555, $converted); 51 | 52 | $decorator = new StripePaymentAmount(153000.777, 'pln'); 53 | $converted = $decorator->toPaymentSystemUnits(); 54 | $this->assertSame(15300077, $converted); 55 | } 56 | 57 | /** @test */ 58 | public function testCanRevertFromSmallestCommonCurrencyUnit() 59 | { 60 | $decorator = new StripePaymentAmount(1005.49, 'usd'); 61 | 62 | $this->assertSame(100549, $decorator->toPaymentSystemUnits()); 63 | 64 | $reverted = $decorator->fromPaymentSystemUnits(); 65 | 66 | $this->assertSame(1005.49, $reverted); 67 | } 68 | 69 | /** @test */ 70 | public function testCanForwardCallToMoneyObject() 71 | { 72 | $decorator = new StripePaymentAmount(5000, 'usd'); 73 | 74 | $this->assertSame(250000.0, (float) $decorator->divide(2)->getAmount()); 75 | } 76 | 77 | /** @test */ 78 | public function testMainAssertionsAreSuccessful() 79 | { 80 | $decorator = new StripePaymentAmount(1090.7, 'USD'); 81 | 82 | $this->assertInstanceOf(Money::class, $decorator->money); 83 | $this->assertSame(109070, $decorator->getAmount()); 84 | $this->assertSame(1090.7, $decorator->amount); 85 | $this->assertEquals(new Currency('USD'), $decorator->getCurrency()); 86 | $this->assertSame(100, $decorator->multiplier); 87 | 88 | $decorator = new StripePaymentAmount(1005.50, 'PLN'); 89 | 90 | $this->assertInstanceOf(Money::class, $decorator->money); 91 | $this->assertSame(100550, $decorator->getAmount()); 92 | $this->assertSame(1005.50, $decorator->amount); 93 | $this->assertSame('PLN', $decorator->getCurrency()->getCode()); 94 | $this->assertSame(100, $decorator->multiplier); 95 | 96 | $decorator = new StripePaymentAmount(1005.50, 'GBP'); 97 | 98 | $this->assertInstanceOf(Money::class, $decorator->money); 99 | $this->assertSame(100550, $decorator->getAmount()); 100 | $this->assertSame(1005.50, $decorator->amount); 101 | $this->assertSame('GBP', $decorator->getCurrency()->getCode()); 102 | $this->assertSame(100, $decorator->multiplier); 103 | 104 | $decorator = new StripePaymentAmount(1005, 'UAH'); 105 | 106 | $this->assertInstanceOf(Money::class, $decorator->money); 107 | $this->assertSame(100500, $decorator->getAmount()); 108 | $this->assertSame(1005.0, $decorator->amount); 109 | $this->assertSame('UAH', $decorator->getCurrency()->getCode()); 110 | $this->assertSame(100, $decorator->multiplier); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /tests/ConditionableTest.php: -------------------------------------------------------------------------------- 1 | user = new User; 27 | 28 | $this->be($this->user); 29 | 30 | config(['stripe-integration.secret' => 'sk_test_test']); 31 | 32 | bind(User::class)->method()->charge( 33 | fn ($service, $app, $params) => new Payment( 34 | tap(new PaymentIntent('test_id'), function ($intent) use ($params) { 35 | $this->basicCharge($params)->each(fn ($value, $key) => $intent->offsetSet($key, $value)); 36 | }) 37 | ) 38 | ); 39 | } 40 | 41 | /** @test */ 42 | public function testCanUseWhenInPaymentProvider() 43 | { 44 | $payment = call(StripePaymentProvider::class)->when(true, function ($provider) { 45 | return $provider->charge(new StripeChargeData( 46 | model: $this->user, 47 | paymentAmount: new StripePaymentAmount(100, 'usd'), 48 | paymentMethod: $provider->setPaymentMethodFor($this->user, 'test_payment_method'), 49 | options: ['description' => 'test'], 50 | )); 51 | }); 52 | 53 | $this->assertStringContainsString('succeeded', $payment->status); 54 | $this->assertEquals(10000, $payment->amount); 55 | $this->assertSame('test', $payment->description); 56 | } 57 | 58 | public function testCanUseUnlessInPaymentProvider() 59 | { 60 | $payment = call(StripePaymentProvider::class)->unless(false, function ($provider) { 61 | return $provider->charge(new StripeChargeData( 62 | model: $this->user, 63 | paymentAmount: new StripePaymentAmount(200, 'usd'), 64 | paymentMethod: $provider->setPaymentMethodFor($this->user, 'test_payment_method'), 65 | options: ['description' => 'test2'], 66 | )); 67 | }); 68 | 69 | $this->assertStringContainsString('succeeded', $payment->status); 70 | $this->assertEquals(20000, $payment->amount); 71 | $this->assertSame('test2', $payment->description); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /tests/PackageConfigurationTest.php: -------------------------------------------------------------------------------- 1 | app['config']->set('stripe-integration.secret', 'test-key'); 14 | 15 | $client = $this->app->make(StripeClient::class); 16 | 17 | $this->assertSame('test-key', $client->getApiKey()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/StripeChargeTest.php: -------------------------------------------------------------------------------- 1 | user = new User; 37 | 38 | $this->be($this->user); 39 | 40 | config(['stripe-integration.secret' => 'sk_test_test']); 41 | 42 | bind(User::class)->method()->charge( 43 | fn ($service, $app, $params) => new Payment( 44 | tap(new PaymentIntent('test_id'), function ($intent) use ($params) { 45 | $this->basicCharge($params)->each(fn ($value, $key) => $intent->offsetSet($key, $value)); 46 | }) 47 | ) 48 | ); 49 | 50 | bind(User::class) 51 | ->method() 52 | ->defaultPaymentMethod(fn () => new PaymentMethod('test_id')); 53 | 54 | bind(PaymentIntentService::class) 55 | ->method() 56 | ->create(fn () => new PaymentIntent('test_id')); 57 | 58 | bind(PaymentIntentService::class) 59 | ->method() 60 | ->confirm( 61 | fn ($service, $app, $params) => tap(new PaymentIntent('test_id'), function ($intent) use ($params) { 62 | $this->offsessionCharge()->each(fn ($value, $key) => $intent->offsetSet($key, $value)); 63 | $intent->offsetSet('payment_method', $params['params']['payment_method']); 64 | }) 65 | ); 66 | } 67 | 68 | /** @test */ 69 | public function basicUsageChargeTest() 70 | { 71 | $cost = new StripePaymentAmount( 72 | amount: 1000, 73 | currency: 'USD', 74 | ); 75 | 76 | $this->paymentProvider = call(StripePaymentProvider::class); 77 | 78 | $customer = $this->paymentProvider->makeCustomerUsing($this->user); 79 | 80 | $paymentMethod = $this->paymentProvider->setPaymentMethodFor($this->user, 'test_paymentMethod'); 81 | 82 | $paymentMethod = $this->paymentProvider->attachPaymentMethodToCustomer( 83 | new PaymentMethodAttachmentData( 84 | paymentMethod: $paymentMethod, 85 | customer: $customer, 86 | ) 87 | ); 88 | 89 | $chargeData = new StripeChargeData( 90 | model: $this->user, 91 | paymentAmount: $cost, 92 | paymentMethod: $paymentMethod, 93 | options: ['description' => 'Test Stripe Charge Description'], 94 | ); 95 | 96 | $payment = $this->paymentProvider->charge($chargeData); 97 | 98 | $this->assertSame('test_id', $paymentMethod->customer); 99 | $this->assertStringContainsString('succeeded', $payment->status); 100 | $this->assertStringContainsString('Test Stripe Charge Description', $payment->description); 101 | $this->assertEquals($cost->getAmount(), $payment->amount); 102 | } 103 | 104 | /** @test */ 105 | public function offsessionChargeTest() 106 | { 107 | $cost = new StripePaymentAmount( 108 | amount: 1000, 109 | currency: 'USD', 110 | ); 111 | 112 | $this->paymentProvider = call(StripePaymentProvider::class); 113 | 114 | $customer = $this->paymentProvider->makeCustomerUsing($this->user); 115 | 116 | $paymentMethod = $this->paymentProvider->setPaymentMethodFor($this->user, 'test_payment_method'); 117 | 118 | $this->paymentProvider->attachPaymentMethodToCustomer( 119 | new PaymentMethodAttachmentData( 120 | paymentMethod: $paymentMethod, 121 | customer: $customer, 122 | ) 123 | ); 124 | 125 | $chargeData = new OffsessionChargeData( 126 | model: $this->user, 127 | paymentAmount: $cost, 128 | intentParams: ['description' => 'Offsession Charge Description'], 129 | ); 130 | 131 | $payment = $this->paymentProvider->offsessionCharge($chargeData); 132 | 133 | $this->assertSame('succeeded', $payment->status); 134 | $this->assertSame('test_id', $payment->payment_method); 135 | } 136 | 137 | /** @test */ 138 | public function offsessionChargeWithoutPaymentMethod() 139 | { 140 | $cost = new StripePaymentAmount( 141 | amount: 1000, 142 | currency: 'USD', 143 | ); 144 | 145 | $this->paymentProvider = call(StripePaymentProvider::class); 146 | 147 | bind(User::class) 148 | ->method() 149 | ->defaultPaymentMethod(fn () => null); 150 | 151 | $payment = $this->paymentProvider->offsessionCharge(new OffsessionChargeData( 152 | model: $this->user, 153 | paymentAmount: $cost, 154 | intentParams: ['description' => 'Offsession Charge Description'], 155 | )); 156 | 157 | $this->assertSame('succeeded', $payment->status); 158 | $this->assertNull($payment->payment_method); 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /tests/StripePaymentProviderTest.php: -------------------------------------------------------------------------------- 1 | collapse()->each(function ($value, $key) use ($intent) { 35 | $intent->offsetSet($key, $value); 36 | }); 37 | 38 | return $intent; 39 | }; 40 | 41 | $methods = ['create', 'update', 'confirm', 'retrieve']; 42 | 43 | collect($methods)->each( 44 | fn ($method) => bind(PaymentIntentService::class) 45 | ->method() 46 | ->{$method}($mapPaymentIntentParams) 47 | ); 48 | } 49 | 50 | /** @test */ 51 | public function testCanInstantiateStripePaymentProvider() 52 | { 53 | $paymentProvider = new StripePaymentProvider( 54 | new StripeClient( 55 | config('stripe-integration.secret') 56 | ) 57 | ); 58 | 59 | $this->assertInstanceOf(StripePaymentProvider::class, $paymentProvider); 60 | } 61 | 62 | /** @test */ 63 | public function testCanInstantiateStripePaymentProviderThroughContainer() 64 | { 65 | $paymentProvider = app(StripePaymentProvider::class); 66 | 67 | $this->assertInstanceOf(StripePaymentProvider::class, $paymentProvider); 68 | } 69 | 70 | /** @test */ 71 | public function testCanInstantiateStripePaymentProviderThroughCallProxy() 72 | { 73 | $paymentProvider = call(StripePaymentProvider::class)->getInternal(Call::INSTANCE); 74 | 75 | $this->assertInstanceOf(StripePaymentProvider::class, $paymentProvider); 76 | } 77 | 78 | /** @test */ 79 | public function testCanSetsetCashierCurrencyAs() 80 | { 81 | $paymentProvider = app(StripePaymentProvider::class); 82 | 83 | $paymentProvider->setCashierCurrencyAs( 84 | new Currency('USD') 85 | ); 86 | 87 | $this->assertStringContainsString('USD', config('cashier.currency')); 88 | } 89 | 90 | /** @test */ 91 | public function testCanCreateSetupIntent() 92 | { 93 | $paymentProvider = app(StripePaymentProvider::class); 94 | 95 | $intent = $paymentProvider->setupIntentUsing(new User); 96 | 97 | $this->assertInstanceOf(SetupIntent::class, $intent); 98 | $this->assertSame('off_session', $intent->usage); 99 | } 100 | 101 | /** @test */ 102 | public function testCanMakeCustomerUsing() 103 | { 104 | $paymentProvider = app(StripePaymentProvider::class); 105 | 106 | $customer = $paymentProvider->makeCustomerUsing(new User); 107 | 108 | $this->assertInstanceOf(Customer::class, $customer); 109 | } 110 | 111 | /** @test */ 112 | public function testCanSetPaymentMethodFor() 113 | { 114 | $paymentProvider = app(StripePaymentProvider::class); 115 | 116 | $paymentMethod = $paymentProvider->setPaymentMethodFor( 117 | new User, 118 | new PaymentMethod('test_id') 119 | ); 120 | 121 | $this->assertInstanceOf(CashierPaymentMethod::class, $paymentMethod); 122 | } 123 | 124 | /** @test */ 125 | public function testCanAttachPaymentMethodToCustomer() 126 | { 127 | $paymentProvider = app(StripePaymentProvider::class); 128 | 129 | $paymentMethod = $paymentProvider->attachPaymentMethodToCustomer( 130 | new PaymentMethodAttachmentData( 131 | paymentMethod: new PaymentMethod('test_id'), 132 | customer: new Customer('test_id'), 133 | ) 134 | ); 135 | 136 | $this->assertInstanceOf(PaymentMethod::class, $paymentMethod); 137 | } 138 | 139 | /** @test */ 140 | public function testCanCreatePaymentIntent() 141 | { 142 | $paymentProvider = app(StripePaymentProvider::class); 143 | 144 | $paymentIntent = $paymentProvider->createPaymentIntent( 145 | new PaymentIntentData( 146 | paymentAmount: new StripePaymentAmount(100, 'PLN'), 147 | model: new User(['stripe_id' => 'test']), 148 | ) 149 | ); 150 | 151 | $this->assertInstanceOf(PaymentIntent::class, $paymentIntent); 152 | $this->assertSame('test', $paymentIntent->customer); 153 | $this->assertSame(10000, $paymentIntent->amount); 154 | $this->assertSame('PLN', $paymentIntent->currency); 155 | $this->assertSame(['card'], $paymentIntent->payment_method_types); 156 | 157 | $paymentIntent = $paymentProvider->createPaymentIntent(new PaymentIntentData); 158 | 159 | $this->assertInstanceOf(PaymentIntent::class, $paymentIntent); 160 | } 161 | 162 | /** @test */ 163 | public function testCanUpdatePaymentIntent() 164 | { 165 | $paymentProvider = app(StripePaymentProvider::class); 166 | 167 | $updatedPaymentIntent = $paymentProvider->updatePaymentIntent( 168 | new PaymentIntentData( 169 | intentId: 'test_id', 170 | model: new User(['stripe_id' => 'test']), 171 | params: ['description' => 123], 172 | ) 173 | ); 174 | 175 | $this->assertInstanceOf(PaymentIntent::class, $updatedPaymentIntent); 176 | $this->assertSame('test', $updatedPaymentIntent->customer); 177 | 178 | $updatedPaymentIntent = $paymentProvider->updatePaymentIntent(new PaymentIntentData); 179 | 180 | $this->assertInstanceOf(PaymentIntent::class, $updatedPaymentIntent); 181 | } 182 | 183 | /** @test */ 184 | public function testCanConfirmPaymentIntent() 185 | { 186 | $paymentProvider = app(StripePaymentProvider::class); 187 | 188 | $confirmedPaymentIntent = $paymentProvider->confirmPaymentIntent( 189 | new PaymentIntentData(intentId: 'diff_id', paymentIntent: new PaymentIntent('test_id')) 190 | ); 191 | $this->assertInstanceOf(PaymentIntent::class, $confirmedPaymentIntent); 192 | $this->assertSame('test_id', $confirmedPaymentIntent->id); 193 | 194 | $confirmedPaymentIntent = $paymentProvider->confirmPaymentIntent( 195 | new PaymentIntentData(intentId: 'diff_id') 196 | ); 197 | $this->assertInstanceOf(PaymentIntent::class, $confirmedPaymentIntent); 198 | $this->assertSame('diff_id', $confirmedPaymentIntent->id); 199 | } 200 | 201 | /** @test */ 202 | public function testCanRetrievePaymentIntent() 203 | { 204 | $paymentProvider = app(StripePaymentProvider::class); 205 | 206 | $updatedPaymentIntent = $paymentProvider->retrievePaymentIntent( 207 | new PaymentIntentData( 208 | intentId: 'test_id', 209 | params: ['description' => 123], 210 | ) 211 | ); 212 | 213 | $this->assertInstanceOf(PaymentIntent::class, $updatedPaymentIntent); 214 | } 215 | } 216 | -------------------------------------------------------------------------------- /tests/Stubs/User.php: -------------------------------------------------------------------------------- 1 | method()->createSetupIntent(function ($service, $app, $params) { 25 | $intent = new SetupIntent; 26 | 27 | collect($params)->collapse()->each(function ($value, $key) use ($intent) { 28 | $intent->offsetSet($key, $value); 29 | }); 30 | 31 | return $intent; 32 | }); 33 | bind(User::class)->method()->createOrGetStripeCustomer(fn () => new Customer('test_id')); 34 | 35 | $paymentMethod = new PaymentMethod('test_id'); 36 | $paymentMethod->offsetSet('customer', null); 37 | 38 | bind(User::class)->method()->updateDefaultPaymentMethod( 39 | fn () => new CashierPaymentMethod(new User, $paymentMethod) 40 | ); 41 | 42 | bind(PaymentMethodService::class) 43 | ->method() 44 | ->attach(function ($service, $app, $params) { 45 | $intent = new PaymentMethod('test_id'); 46 | 47 | collect($params['params'])->each(function ($value, $key) use ($intent) { 48 | $intent->offsetSet($key, $value); 49 | }); 50 | 51 | return $intent; 52 | }); 53 | } 54 | 55 | protected function getPackageProviders($app): array 56 | { 57 | return [ 58 | StripeIntegrationServiceProvider::class, 59 | ]; 60 | } 61 | 62 | public function getEnvironmentSetUp($app): void 63 | { 64 | config()->set('testing'); 65 | } 66 | 67 | public function basicCharge(array $params): Collection 68 | { 69 | return collect([ 70 | 'amount' => $params['amount'], 71 | 'description' => $params['options']['description'], 72 | 'payment_method' => $params['paymentMethod'], 73 | 'status' => 'succeeded', 74 | ]); 75 | } 76 | 77 | public function offsessionCharge(): Collection 78 | { 79 | return collect([ 80 | 'status' => 'succeeded', 81 | ]); 82 | } 83 | } 84 | --------------------------------------------------------------------------------