undf / zooz-payment-bundle
将Zooz支付平台集成到您的Symfony2&AangularJS项目中
Requires
- doctrine/doctrine-bundle: 1.2.*
- doctrine/orm: >=2.2.3,<2.4-dev
This package is not auto-updated.
Last update: 2024-09-23 16:20:39 UTC
README
将Zooz支付平台集成到您的Symfony2&AangularJS项目中。
此扩展假定您已在项目中安装了AngularJs。
#安装
###步骤1:下载扩展
> composer require undf/zooz-payment-bundle dev-master
###步骤2:启用扩展
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Undf\ZoozPaymentBundle\UndfZoozPaymentBundle(), ); }
#配置
# app/config.yml undf_zooz_payment: config: unique_id: <your Zooz application unique id> app_key: <your Zoop application app key> sandbox_mode: true #Enable/disable the Zooz server sandbox mode ajax_mode: true #If true, Zooz server transaction response will be handled from client side payment: entity: YourBundle:Payment #Your payment entity class manager: your.manager.service.id #Service id of your payment manager class templates: #These templates are only used in non-ajax mode return: UndfZoozPaymentBundle:Transaction:return.html.twig #Succeed payment template cancel: UndfZoozPaymentBundle:Transaction:cancel.html.twig #Failed payment template
# app/routing.yml undf_zooz_payment: resource: "@UndfZoozPaymentBundle/Controller/" type: annotation prefix: /
#服务器端####步骤1:创建您的支付实体
namespace Your\Bundle\Entity; use Doctrine\ORM\Mapping as ORM; use Undf\ZoozPaymentBundle\Entity\BasePayment; /** * @ORM\Table() * @ORM\Entity */ class Payment extends BasePayment { ... }
注意:另外,您可以直接实现接口 Undf\ZoozPaymentBundle\Model\PaymentInterface。
####步骤2:创建您的发票项实体(可选)作为一个选项,该扩展允许您配置要包含在发票中并在Zooz窗口中显示的项目列表(仅适用于Paypal支付)。如果您想这样做,您需要创建一个InvoiceItem实体
namespace Your\Bundle\Entity; use Doctrine\ORM\Mapping as ORM; use Undf\ZoozPaymentBundle\Entity\BaseInvoiceItem; /** * @ORM\Table() * @ORM\Entity */ class InvoiceItem extends BaseInvoiceItem { ... }
注意:另外,您可以直接实现接口 Undf\ZoozPaymentBundle\Model\InvoiceItemInterface。
并且您还需要在您的Payment实体类中重写"getInvoiceItemCollection"方法
class Payment extends BasePayment { ... public function getInvoiceItemCollection() { //Return the entity property holding your item collection, where every item //must implement Undf\ZoozPaymentBundle\Model\InvoiceItemInterface return $this->collection; } ... }
####步骤3:创建您的支付管理器类
确保您的管理器类实现了Undf\ZoozPaymentBundle\Model\PaymentManagerInterface。
namespace Your\Bundle\Manager; use Symfony\Bridge\Doctrine\RegistryInterface; use Undf\ZoozPaymentBundle\Model\PaymentManagerInterface; use Undf\ZoozPaymentBundle\Model\PaymentInterface; class PaymentManager implements PaymentManagerInterface { protected $em; public function __construct(RegistryInterface $doctrine) { $this->em = $doctrine->getManagerForClass('YourBundle:Payment'); } public function update(PaymentInterface $payment) { $this->em->persist($payment); $this->em->flush(); } public function handlePaymentSuccess(PaymentInterface $payment) { ... } public function handlePaymentFail(PaymentInterface $payment) { ... } }
将您的管理器类添加到服务容器中
<service id="your.manager.service.id" class="Your\Bundle\Manager\PaymentManager">
<argument type="service" id="doctrine" />
</service>
####步骤4:创建您的交易控制器
namespace Your\Bundle\Controller use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class YourTransactionController extends Controller { /** * @Route("/your/transaction/url") */ public function yourTransactionAction() { //Validate, manage or do whatever you want to do with the transaction request ... //Just make sure you return this: $transactionHandler = $this->get('undf_zooz_payment.handler.open_transaction'); $transactionHandler->setLanguage($this->getRequest()->getLocale()); return $transactionHandler->openTransaction($payment); } }
##客户端####步骤1:包含所需脚本 在您的支付模板中添加以下行
{% include 'UndfZoozPaymentBundle:Transaction:start.html.twig' %}
####步骤2:启用AngularJs控制器 在您的“支付”按钮HTML元素中添加以下属性
data-ng-controller="ZoozPaymentCtrl"
####步骤3:触发Zooz支付窗口 在交易控制器URL回调中调用“startZooz”函数
$http.get('/your/transaction/url').success(function(response) { startZooz(response); })
####步骤4:(仅AJAX模式)处理来自Zooz服务器的交易响应 可以有多个处理成功或失败交易响应的方法,所以在此扩展中已经做的是从“ZoozPaymentCtrl”控制器中发出两个AngularJs事件。因此,为了处理来自Zooz服务器的响应,您可以监听这些事件并在之后做任何想做的事情。
以下是在控制器的示例中完成的,该控制器管理着“ZoozPaymentCtrl”控制器所在的范围
function MyCustomCtrl($scope, $window) {
$scope.$on('zooz.payment.success', function(event, response) {
$window.location.replace('/transaction/success');
});
$scope.$on('zooz.payment.error', function(event, response) {
$window.location.replace('/transaction/error');
});
}