flosch/stripe-bundle

该包已被废弃,不再维护。作者建议使用 stripe/stripe-php 包。

Symfony 3 扩展包,可轻松将 Stripe 的 Web 支付 SDK 集成到 Symfony 项目中。

安装数: 46,407

依赖: 0

推荐者: 0

安全性: 0

星标: 30

关注者: 3

分支: 11

开放问题: 8

类型:symfony-bundle

2.1.0 2018-10-14 21:08 UTC

This package is not auto-updated.

Last update: 2020-12-13 17:59:56 UTC


README

Stripe PHP SDK 的 symfony >= 3 集成

⚠️ 重要:在使用此包之前... ⚠️

一些(相当大的!)变化即将在不久的将来到来,将影响欧洲的在线支付解决方案,影响此包使用的 Stripe API。在使用此包之前,请仔细阅读此问题

描述

此包允许您将 Stripe SDK 作为 Symfony 服务进行操作,并提供一些辅助工具来使用不同的 Stripe API 概念,如 Stripe Connect 或订阅 API。

注意:master 分支和 1. 释放针对 Symfony 3。如果您使用的是 Symfony 4,请使用 2.0.0 分支和此包的 2. 释放。

安装

要安装此包,请运行以下命令,您将从 [Packagist][3] 获取最新版本。

composer require flosch/stripe-bundle

在 AppKernel.php 中加载所需扩展包

// app/AppKernel.php
public function registerBundles()
{
  $bundles = array(
    // [...]
    new Flosch\Bundle\StripeBundle\FloschStripeBundle()
  );
}

并设置所需的配置

# app/config/config.yml
flosch_stripe:
    stripe_api_key: "%stripe_api_key%" # The Stripe API key can be added as a symfony parameter

使用

然后,您可以在控制器内部使用此服务

$stripeClient = $this->get('flosch.stripe.client');

StripeClient php 类扩展了默认的 Stripe PHP SDK 类,允许您执行此 SDK 可以执行的所有操作。此外,它将自动使用您的 Stripe API 密钥进行身份验证,您根本无需担心。

扩展客户端

我将始终欢迎 PR 来更新此项目,但是如果您希望添加自己的自定义辅助工具,您可以轻松地自己扩展它,例如

<?php // src/YourNamespace/YourProject/Stripe/StripeClient.php

namespace YourNamespace\YourProject\Stripe;

use Flosch\Bundle\StripeBundle\Stripe\StripeClient as BaseStripeClient;

class StripeClient extends BaseStripeClient
{
    public function __construct($stripeApiKey = '')
    {
        parent::__construct($stripeApiKey);

        return $this;
    }

    public function myOwnMethod()
    {
        // Do what you want here...
    }
}

然后注册您的扩展作为服务,并使用它。

https://symfony.com.cn/doc/current/service_container/3.3-di-changes.html

services:
    YourNamespace\YourProject\Stripe\StripeClient:
        autowire: true
        autoconfigure: true
        lazy: true

    my.stripe.client:
        alias: YourNamespace\YourProject\Stripe\StripeClient
public function indexAction()
{
    // Retrieve it from the container with the old-school getter
    $stripeClient = $this->get('my.stripe.client');
}

public function indexAction(\YourNamespace\YourProject\Stripe\StripeClient $stripeClient)
{
    // Or inject it in your controllers if you use autowiring
    // Then you're ready to go...
}

辅助工具

StripeClient 当前提供四种辅助方法

通过其 ID 检索优惠券
/**
 * $planId (string)         : The existing Coupon ID (the one you define in the Stripe dashboard)
 */
$coupon = $stripeClient->retrieveCoupon($planId);
通过其 ID 检索计划
/**
 * $planId (string)         : The existing Plan ID (the one you define in the Stripe dashboard)
 */
$plan = $stripeClient->retrievePlan($planId);
通过其 ID 检索客户
/**
 * $customerId (string)         : The existing Customer ID
 */
$customer = $stripeClient->retrieveCustomer($customerId);
通过其 ID 检索费用
/**
 * $chargeId (string)         : The existing Charge ID
 */
$charge = $stripeClient->retrieveCharge($chargeId);
创建新客户
/**
 * $paymentToken (string)   : The payment token obtained using the Stripe.js library
 * $customerEmail (string)  : The customer e-mail address
 */
$stripeClient->createCustomer($paymentToken, $customerEmail);
创建并订阅新客户到现有计划
/**
 * $planId (string)         : The existing Plan ID (the one you define in the Stripe dashboard)
 * $paymentToken (string)   : The payment token obtained using the Stripe.js library
 * $customerEmail (string)  : The customer e-mail address
 * $couponId (string|null)  : An optional coupon ID
 */
$stripeClient->subscribeCustomerToPlan($planId, $paymentToken, $customerEmail, $couponId);
将现有客户订阅到现有计划
/**
 * $customerId (string)     : The existing Customer ID
 * $planId (string)         : The existing Plan ID (the one you define in the Stripe dashboard)
 * $parameters (array)      : An optional array of additional parameters
 */
$stripeClient->subscribeExistingCustomerToPlan($customerId, $planId, [
    'coupon'        => 'TEST',
    'tax_percent'   => 19.6
]);
创建一笔收费(到平台或连接的Stripe账户)
/**
 * $chargeAmount (int)              : The charge amount in cents, for instance 1000 for 10.00 (of the currency)
 * $chargeCurrency (string)         : The charge currency (for instance, "eur")
 * $paymentToken (string)           : The payment token obtained using the Stripe.js library
 * $stripeAccountId (string|null)   : (optional) The connected string account ID, default null (--> charge to the platform)
 * $applicationFee (int)            : The amount of the application fee (in cents), default to 0
 * $chargeDescription (string)      : (optional) The charge description for the customer
 */
$stripeClient->createCharge($chargeAmount, $chargeCurrency, $paymentToken, $stripeAccountId, $applicationFee, $chargeDescription);
退款
/**
 * $chargeId (string)           : The Stripe charge ID (returned by Stripe when you create a charge)
 * $refundAmount (int)          : The charge amount in cents (if null, the whole charge amount will be refunded)
 * $metadata (array)            : additional informations about the refund, default []
 * $reason (string)             : The reason of the refund, either "requested_by_customer", "duplicate" or "fraudulent"
 * $refundApplicationFee (bool) : Wether the application_fee should be refunded aswell, default true
 * $reverseTransfer (bool)      : Wether the transfer should be reversed (when using Stripe Connect "destination" parameter on charge creation), default false
 * $stripeAccountId (string)    : (optional) TheStripe account on which the charge has been made (Stripe Connect), default null (--> refund by the platform)
 */
$stripeClient->refundCharge($chargeId, $refundAmount, $metadata, $reason, $refundApplicationFee, $reverseTransfer, $stripeAccountId);