think.studio / laravel-gocardless-payment
适用于 Laravel 的 GoCardless 支付实现
1.0.0
2024-02-28 09:41 UTC
Requires
- php: ^8.1
- gocardless/gocardless-pro: ^5.7
- illuminate/support: ^9.0|^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.14
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.7
README
该包专为特定用例创建,不支持/维护 GoCardless 的所有选项。包基于官方的 php 包
安装
composer require think.studio/laravel-gocardless-payment
可选:您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="GoCardlessPayment\ServiceProvider" --tag="config" # If you want override default migrations php artisan vendor:publish --provider="GoCardlessPayment\ServiceProvider" --tag="migrations"
放置所需的凭据
GOCARDLESS_ACCESS_TOKEN="sandbox_XxxxXXXxxxxXXXXxx-xXxxxXXx_XX-xxxX" GOCARDLESS_WEBHOOK_ENDPOINT_SECRET="XXXXxxxxXXXXXxxxXXXXxxxXXXXxx"
自定义默认配置
修改任何应用服务提供者
public function register() { // Do not run default migrations \GoCardlessPayment\GoCardlessPayment::ignoreMigrations(); // Do not use default routes provided by package \GoCardlessPayment\GoCardlessPayment::ignoreRoutes(); // Override repository to get local customer $this->app->singleton(LocalCustomerRepository::class, function (Application $app) { return new MyCustomLocalCustomerRepository(); }); // Override Api functionality $this->app->bind(Api::class, function (Application $app) { return new CustomApi(); }); }
用法
注意:要使用 API 请求,您的服务器 IP 应位于 GoCardless 服务覆盖的国家,否则 API 请求将失败(您可以通过 VPN 进行本地测试/开发尝试)
创建授权检查 URL
作为第一步,服务器应生成 授权 URL 并将用户重定向到该 URL。
use GoCardlessPayment\MandateCheckout\BillingRequest; use GoCardlessPayment\MandateCheckout\BillingRequestFlow; use GoCardlessPayment\MandateCheckout\MandateCheckoutPage; use GoCardlessPayment\MandateCheckout\MandateRequest; use GoCardlessPayment\MandateCheckout\Metadata; use GoCardlessPayment\MandateCheckout\ReturnUrls; /** @var \GoCardlessPayment\Contracts\GoCardlessCustomer $user */ $user = getCurrentUser(); $url = MandateCheckoutPage::make( BillingRequest::make() ->mandateRequest( MandateRequest::make() ->scheme('bacs') ->verifyWhenAvailable() ), BillingRequestFlow::make() ->returnUrls(ReturnUrls::make(route('example.route'))) )->useCustomer($user)->requestCheckoutUrl(); return Redirect::to($url);
或完全管理的请求
use GoCardlessPayment\MandateCheckout\BillingRequest; use GoCardlessPayment\MandateCheckout\BillingRequestFlow; use GoCardlessPayment\MandateCheckout\MandateCheckoutPage; use GoCardlessPayment\MandateCheckout\MandateRequest; use GoCardlessPayment\MandateCheckout\Metadata; use GoCardlessPayment\MandateCheckout\ReturnUrls; $url = MandateCheckoutPage::make( BillingRequest::make() ->mandateRequest( MandateRequest::make() ->scheme('bacs') ->verifyWhenAvailable() )->metadata( Metadata::make() ->add('crm_user', $user->getKey()) )->links(Links::make()->addCustomer($user->gocardlessKey())), BillingRequestFlow::make() ->prefilledCustomer( PrefilledCustomer::make() ->givenName($user->first_name) ->familyName($user->last_name) ->email($user->email) ->postalCode($user->postalcode) ->addressLine1($user->street) ->addressLine2($user->locality) ->city($user->town) ->region($user->county) ->countryCode($user->country_code) ) ->returnUrls(ReturnUrls::make(route('example.route'))) )->requestCheckoutUrl(); return Redirect::to($url);
Webhook 安装
接收关于创建授权的 webhook 并将其存储在数据库中。
本地服务器 webhook
首先,在您的笔记本电脑上安装 cli。
然后,您可以运行监听器并将其转发到您的本地站点。示例
gc listen --forward https:///gocardless/webhook # Or to jus preview webhooks content without real processing by app you can use "simple" listen method: # gc listen
Cli 命令
导入授权
注意:如果本地存储不包含与授权“客户”相关的数据,则将跳过导入。
php artisan gocardless-payment:import:mandate MD123FOOBAR