ameotoko / stripe-bundle
Contao 4 的简单 Stripe 控制器
v1.0.2
2023-02-22 14:50 UTC
Requires
- php: ^7.4 || ^8.0
- contao/core-bundle: ^4.12
- stripe/stripe-php: ^7.67
- symfony/config: ^5.4
- symfony/dependency-injection: ^5.4
- symfony/http-kernel: ^5.4
Requires (Dev)
- contao/manager-plugin: ^2.0
Conflicts
- contao/manager-plugin: <2.0 || >=3.0
README
该扩展包设置基本环境,如果您想在 Contao 应用程序中使用 Stripe。
API 密钥参数
将您的 API 密钥添加到扩展包配置中,它们也将作为容器参数可用
# config/config.yml stripe: secret_key: 'sk_test_****' publishable_key: 'pk_test_****'
然后您可以在自己的服务中使用它
# config/services.yml services: App\EventListener\StripeListener: arguments: - '%stripe.secret_key%'
重要:请将您的生产 API 密钥存储在环境变量中,以避免将其提交到版本控制
# .env.local STRIPE_SECRET_KEY=sk_live_****
# config/config_prod.yml stripe: secret_key: '%env(STRIPE_SECRET_KEY)%'
端点
该扩展包在您的应用程序中创建了 2 个端点,您可以从 JavaScript 调用这些端点以创建支付意向或结账会话
<?php // my-template.html5 ?> <script src="https://js.stripe.com/v3/"></script> <script> const paymentData = { success_url: '...', cancel_url: '...', payment_method_types: ['card', 'sepa_debit', 'sofort', 'ideal', 'alipay'], mode: 'payment', billing_address_collection: 'required', line_items: [...], metadata: {...} } fetch('<?= $this->route('stripe_create_checkout_session') ?>', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(paymentData) }) .then(response => response.json()) .then(session => window.location.href = session.url) </script>
该扩展包还设置了一个 webhook 端点 /_stripe/webhook
,您可以在 Stripe 账户中进行配置,例如 https://example.com/_stripe/webhook
。
然后,您可以通过设置一个 EventListener
来处理 Stripe 发送到您的 webhook 端点的事件,该监听器监听诸如 'stripe.' + Stripe 事件名称的事件。例如,如果您想处理 Stripe 的 checkout.session.completed
# config/services.yml services: App\EventListener\StripeCheckoutSessionCompleted: tags: - { name: kernel.event_listener, event: stripe.checkout.session.completed }
事件
stripe.create_checkout.pre
(接收您从前端发送的数据对象,在将其发送到 Stripe 之前)stripe.create_checkout.post
(接收Stripe\Checkout\Session::create()
的响应)stripe.<STRIPE_WEBHOOK_EVENT>
Contao 是一个开源 PHP 内容管理系统,适合想要维护简单的专业网站的人。访问项目网站以获取更多信息。