bartpie/stripe-bundle

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

安装次数: 3

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 10

类型:symfony-bundle

2.1.0 2018-10-14 21:08 UTC

This package is auto-updated.

Last update: 2024-09-18 21:23:58 UTC


README

Stripe PHP SDK 的 symfony >= 3 集成

此扩展包允许您将 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);