avro / stripe-bundle

Symfony2 扩展包,用于管理用户订阅和 Stripe 支付。

安装: 384

依赖: 0

建议者: 0

安全: 0

星星: 16

关注者: 6

分支: 8

开放问题: 0

类型:symfony-bundle

0.1.0 2012-12-01 17:50 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:13:29 UTC


README

一个用于与优秀的 Stripe 支付服务交互的 symfony2 扩展包。

功能

  • 允许用户支付和接收资金
  • 订阅用户到计划
  • 更新用户计划
  • 查看/打印发票和费用
  • 创建优惠券
  • 创建计划

状态

工作进行中

步骤 1:使用 composer 下载 AvroStripeBundle

在 composer.json 中添加 AvroStripeBundle

{
    "require": {
        "jdewit/stripe-bundle": "*"
    }
}

现在运行以下命令告诉 composer 下载该扩展包

$ php composer.phar update jdewit/stripe-bundle

步骤 2:启用扩展包

在内核中启用扩展包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Avro\StripeBundle\AvroStripeBundle(),
    );
}

步骤 3:更新您的用户类

<?php
namespace Application\UserBundle\Document;

use FOS\UserBundle\Document\User as BaseUser;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document
 */
class User extends BaseUser
{
    /**
     * @ODM\Id(strategy="auto")
     */
    protected $id;

    /**
     * @ODM\String
     */
    protected $stripeCustomerId;

    /**
     * @ODM\Boolean
     */
    protected $isStripeCustomerActive;

    /**
     * @ODM\String
     */
    protected $stripeAccessToken;

    /**
     * @ODM\String
     */
    protected $stripePublishableKey;

    /**
     * @ODM\ReferenceOne(targetDocument="Avro\StripeBundle\Document\Plan")
     */
    protected $plan;


    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    public function getStripeAccessToken()
    {
        return $this->stripeAccessToken;
    }

    public function setStripeAccessToken($stripeAccessToken)
    {
        $this->stripeAccessToken = $stripeAccessToken;
        return $this;
    }

    public function getStripePublishableKey()
    {
        return $this->stripePublishableKey;
    }

    public function setStripePublishableKey($stripePublishableKey)
    {
        $this->stripePublishableKey = $stripePublishableKey;
        return $this;
    }

    public function getStripePublishableKey()
    {
        return $this->stripePublishableKey;
    }

    public function setStripePublishableKey($stripePublishableKey)
    {
        $this->stripePublishableKey = $stripePublishableKey;
        return $this;
    }
    public function getStripeCustomerId()
    {
        return $this->stripeCustomerId;
    }

    public function setStripeCustomerId($stripeCustomerId)
    {
        $this->stripeCustomerId = $stripeCustomerId;
        return $this;
    }

    public function getIsStripeCustomerActive()
    {
        return $this->isStripeCustomerActive;
    }

    public function setIsStripeCustomerActive($isStripeCustomerActive)
    {
        $this->isStripeCustomerActive = $isStripeCustomerActive;
        return $this;
    }

    public function getPlan()
    {
        return $this->plan;
    }

    public function setPlan(\Avro\StripeBundle\Document\Plan $plan)
    {
        $this->plan = $plan;
        return $this;
    }

    /**
     * Get user information to prefill stripe signup form
     */
    public function getStripeConnectionParameters()
    {
        return array(
            'stripe_user[email]' => $user->getEmail() ?: '',
            //'stripe_user[url]' => $user->getWebsite() ?: '',
            //'stripe_user[phone_number]' => $user->getPhone() ?: '',
            //'stripe_user[business_name]' => $user->getCompany() ?: '',
            //'stripe_user[first_name]' => $user->getFirstName() ?: '',
            //'stripe_user[last_name]' => $user->getLastName() ?: '',
            //'stripe_user[street_address]' => $user->getAddress() ?: '',
            //'stripe_user[city]' => $user->getCity() ? $user->getCity()->getName() : '',
            //'stripe_user[state]' => $user->getProvince() ? $user->getProvince()->getName() : '',
            //'stripe_user[country]' => $user->getCountry() ? $user->getCountry()->getName() : '',
            //'stripe_user[zip]' => $user->getPostalCode() ?: '',
        );
    }
}

步骤 5:扩展扩展包

创建一个扩展此扩展包的扩展包骨架

namespace Application\StripeBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ApplicationStripeBundle extends Bundle
{
    public function getParent()
    {
        return 'AvroStripeBundle';
    }
}

步骤 6:创建计划类

计划类是一个需要扩展的超类。这允许你添加自定义方法,如使用限制等...

<?php
//Application/StripeBundle/Document/Plan
namespace Application\StripeBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Validator\Constraints as Assert;

use Avro\StripeBundle\Document\Plan as BasePlan;

/**
 * @ODM\Document
 */
class Plan extends BasePlan
{
    /**
     * @ODM\Id(strategy="none")
     */
    public $id;

// customize to your needs, not required
//    /**
//     * @ODM\Int
//     */
//    protected $limit;
//
//    /**
//     * @ODM\Boolean
//     */
//    protected $phoneSupport = false;

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }
}

步骤 7:配置应用程序的 security.yml

确保路由只对认证用户可用

# app/config/security.yml
security:
    access_control:
        - { path: ^/stripe/, role: ROLE_USER }

步骤 8:配置 AvroStripeBundle

将您的 Stripe API 密钥添加到 config.yml

# app/config/config.yml

avro_stripe:
#required
    client_id: %stripe_client_id% // define these in your parameters.yml and parameters_prod.yml
    secret_key: %stripe_secret_key%
    publishable_key: %stripe_publishable_key%

#optional
    db_driver: mongodb # other storage is yet to be implemented by... you?
    hooks_enabled: false #use bundles default hook events (send emails etc...)
    prorate: false #prorate updated subscription charges 
    redirect_routes: #set routes to redirect to, default is to redirect to homepage 
        customer_new: application_user_settings_payment
        customer_update: application_user_settings_payment
        customer_disable: application_user_settings_payment
        subscription_update: application_user_settings_subscription
        account_confirm: application_user_settings_payment
        account_disconnect: application_user_settings_payment

步骤 9:导入 AvroStripeBundle 路由文件

导入 AvroStripeBundle 路由文件。

在 YAML 中

# app/config/routing.yml
avro_stripe:
    resource: "@AvroStripeBundle/Resources/config/routing/routing.yml"

步骤 10:更新您的数据库模式

现在该扩展包已配置,您需要做的最后一件事是更新您的数据库模式。

对于 MongoDB,您可以运行以下命令来创建索引。

$ php app/console doctrine:mongodb:schema:create --index

钩子

该扩展包在 "/stripe/hook" 接收钩子,并分发您可以监听的事件

例如。

由 HookController 分发的 'charge.succeeded' 事件作为 'avro_stripe.charge.succeeded' 分发

###注意

如果您希望使用此扩展包提供的默认文本,请确保您在配置中启用了翻译器。

# app/config/config.yml

framework:
    translator: ~

为了使用内置的电子邮件功能,您必须激活并配置 SwiftmailerBundle。

下一步