c975l / purchasecredits-bundle
用于在网站内购买和使用积分的包
Requires
- php: *
- c975l/config-bundle: ^2
- c975l/email-bundle: ^4
- c975l/includelibrary-bundle: ^1
- c975l/payment-bundle: ^4
- c975l/services-bundle: ^1
- c975l/toolbar-bundle: ^1
- doctrine/doctrine-bundle: *
- doctrine/orm: *
- knplabs/knp-paginator-bundle: *
- symfony/form: *
- symfony/security: *
- symfony/translation: *
- twig/intl-extra: *
Requires (Dev)
- dev-master
- v4.0
- 3.x-dev
- v3.6
- v3.5
- v3.4.1
- v3.4
- v3.3
- v3.2
- v3.1.1
- v3.1
- v3.0.1
- v3.0
- 2.x-dev
- v2.1.2.1
- v2.1.2
- v2.1.1.1
- v2.1.1
- v2.1
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0
- 1.x-dev
- v1.6.2
- v1.6.1
- v1.6
- v1.5.1
- v1.5
- v1.4.5.1
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4
- v1.3.3
- v1.3.2
- v1.3.1.2
- v1.3.1.1
- v1.3.1
- v1.3
- v1.2
- v1.1.1
- v1.1
- v1.0
- dev-dev
This package is auto-updated.
Last update: 2024-09-11 19:04:45 UTC
README
PurchaseCreditsBundle 具有以下功能
- 允许在您的网站内购买和使用积分
- 通过 c975LPaymentBundle 与 Stripe 交互进行支付
- 与 c975LToolbarBundle 集成
- 通过电子邮件向用户发送关于已购买积分的通知,并将销售条款作为 PDF 附件发送给用户
此 Bundle 依赖于 c975LPaymentBundle、Stripe 及其 PHP 库。因此,您必须拥有一个 Stripe 账户。
还建议使用 SSL 证书来让用户感到放心。
由于销售条款必须与礼品券一起发送给用户,因此您必须提供一个用于此 PDF 文件的路由或 URL。如果您没有此类路由,您可以考虑使用 c975LSiteBundle(它具有预定义的模型)以及 c975LPageEditBundle(它具有创建 PDF 的能力)。
Bundle 安装
步骤 1:下载 Bundle
版本 v3.x 与 Symfony 4.x 兼容。使用 v2.x 与 Symfony 3.x 兼容 使用 Composer 安装库
composer require c975l/purchasecredits-bundle
步骤 2:配置 Bundle
检查依赖项的配置
c975LPurchaseCreditsBundle 使用 c975L/ConfigBundle 管理配置参数。使用路由 "/purchase-credits/config" 并设置适当的用户角色以修改它们。
步骤 3:启用路由
然后,将它们添加到您项目的 /config/routes.yaml
文件中,以启用路由
c975_l_purchase_credits: resource: "@c975LPurchaseCreditsBundle/Controller/" type: annotation prefix: / #Multilingual website use the following #prefix: /{_locale} #defaults: { _locale: '%locale%' } #requirements: # _locale: en|fr|es
步骤 4:用户实体
您的用户实体必须有一个名为 credits
的属性,并且有合适的 getter 和 setter,以及一个 addCredits()
方法,注意其中的 +=
操作符,此方法用于添加和减去积分
//Your entity file namespace App\Entity; //Example is made using Doctrine, as the common one, but you can use any entity manager use Doctrine\ORM\Mapping as ORM; /** * User * * @ORM\Table(name="user") * @ORM\Entity */ class User { //... /** * Number of credits for User * @var int * * @ORM\Column(name="credits", type="integer", nullable=true) */ protected $credits; //... /** * Set credits * @param int * @return User */ public function setCredits($credits) { $this->credits = $credits; return $this; } /** * Get credits * @return int */ public function getCredits() { return $this->credits; } /** * Add credits (or subtracts if $credits is negative) * @param int * @return User */ public function addCredits($credits) { $this->credits += $credits; return $this; }
步骤 5:创建 MySql 表
您可以使用 php bin/console make:migration
创建迁移文件,如 Symfony 的 Doctrine 文档 中所述,或者使用 /Resources/sql/purchase-credits.sql
创建 user_transactions
表。请注意,DROP TABLE
被注释掉,以避免意外删除。
步骤 6:覆盖模板
强烈建议使用 从第三方 Bundle 覆盖模板的功能 来完全集成到您的网站中。
为此,只需在您的应用中创建以下结构 /templates/bundles/c975LPurchaseCreditsBundle/
,然后在其中复制 layout.html.twig
文件以覆盖现有的 Bundle 文件。
在 layout.html.twig
中,它主要包含扩展你的布局和定义特定变量,例如:
{% extends 'layout.html.twig' %} {% block content %} {% block purchaseCredits_content %} {% endblock %} {% endblock %}
如何使用
购买和支付的整个过程都由包管理。你需要在你的端实现的是使用积分。你可以使用以下代码:
<?php //In your controller file use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use c975L\PurchaseCreditsBundle\Service\TransactionServiceInterface; class PaymentController extends AbstractController { /** * @Route("/YOUR_ROUTE", * name="YOUR_ROUTE_NAME", * methods={"HEAD", "GET"}) */ public function YOUR_METHOD_NAME(Request $request, TransactionServiceInterface $transactionService) { //Your stuff... //Gets the manager $em = $this->getDoctrine()->getManager(); //Adds transaction, to keep trace of it and for user to see it in its list of transactions //You can call create() without argument, TransactionService will add an orderId built on the same scheme as Payment's one //The only restriction is that your orderId MUST NOT start with 'pmt' as this string is added to the Payment orderId, to provide a link to the payment $transaction = $this->transactionService->add('YOUR_OWN_ORDER_ID_OR_NULL', +-CREDITS, $description(), $this->getUser()); //You need to flush DB as $transaction and $user are persisted but not flushed $em->flush(); } }
路由
可用的不同路由(命名自解释)包括:
- purchasecredits_dashboard
- purchasecredits_purchase
- purchasecredits_transactions
Twig 访问
你可以在 Twig 中通过以下方式访问用户的积分:
{{ app.user.credits }}
积分信息
如果你想要向用户显示积分信息,你可以在你的 Twig 模板中添加以下代码。它将在一行中显示积分数量、交易链接、购买链接以及当积分 <= 0 时的警告。
{% include('@c975LPurchaseCredits/fragments/creditsInformation.html.twig') %}
交易显示
交易列表的显示是通过包完成的,但如果你想链接到特定的交易,你可以使用以下代码:
{{ path('purchasecredits_transaction_display', {'orderId': 'TRANSACTION_ORDER_ID'}) }}
用于 JavaScript 的积分 Div 数据
如果你想在 JavaScript 中使用用户的积分,可以将其插入一个 div 中,然后通过 Twig 扩展来实现:
{# Credits DivData #}
{{ purchasecredits_divData() }}
然后你可以通过以下方式访问它:
$(document).ready(function() { var credits = $('#userCredits').data('credits'); });
查看它以了解覆盖的属性。
如果这个项目能帮助你减少开发时间,你可以通过顶部的“赞助”按钮来赞助我:)