lexik/paybox-bundle

此包已被弃用且不再维护。没有建议的替代包。

LexikPayboxBundle 简化了 Paybox 支付系统的实现。

安装次数: 131,533

依赖: 0

建议者: 0

安全性: 0

星标: 40

关注者: 16

分支: 48

开放问题: 13

类型:symfony-bundle

v2.1.3 2019-01-18 15:25 UTC

This package is auto-updated.

Last update: 2024-03-19 08:13:16 UTC


README

Build Status Latest Stable Version SensioLabsInsight

重要!

此包部分维护。不会添加新功能,但会合并一些 PR 以保持兼容性或安全性。

LexikPayboxBundle 通过为您完成所有繁琐的工作,简化了 Paybox 支付系统的使用。

LexikPayboxBundle 在请求过程中默默进行:

  • 参数的 hmac 哈希计算。
  • 在请求之前对服务器进行测试,以确保其正常运行。
  • 在 ipn 响应上使用 openssl 进行签名验证。
  • 在响应上触发事件。

您只需提供交易参数,自定义响应页面,并等待在 ipn 响应上触发的事件。

要求

  • PECL hash >= 1.1
  • openssl 已启用

安装

使用 composer 进行安装

composer require lexik/paybox-bundle

将此包添加到您的 app/AppKernel.php 中

public function registerBundles()
{
    return array(
        // ...
        new Lexik\Bundle\PayboxBundle\LexikPayboxBundle(),
        // ...
    );
}

配置

您的个人账户信息必须在 config.yml 中设置

# Lexik Paybox Bundle
lexik_paybox:
    parameters:
        production: false        # Switches between Paybox test and production servers (preprod-tpe <> tpe)
        site:        '9999999'   # Site number provided by the bank
        rank:        '99'        # Rank number provided by the bank
        login:       '999999999' # Customer's login provided by Paybox
        hmac:
            key: '01234...BCDEF' # Key used to compute the hmac hash, provided by Paybox

附加配置

lexik_paybox:
    parameters:
        currencies:  # Optionnal parameters, this is the default value
            - '036'  # AUD
            - '124'  # CAD
            - '756'  # CHF
            - '826'  # GBP
            - '840'  # USD
            - '978'  # EUR
        hmac:
            algorithm:      sha512 # signature algorithm
            signature_name: Sign   # customize the signature parameter name

路由集合必须在您的 routing.yml 中设置

# Lexik Paybox Bundle
lexik_paybox:
    resource: '@LexikPayboxBundle/Resources/config/routing.yml'

Paybox 系统的使用

该包包含一个示例控制器 SampleController.php,其中包含两个操作。

...
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Sample action to call a payment.
 * It create the form to submit with all parameters.
 */
public function callAction()
{
    $paybox = $this->get('lexik_paybox.request_handler');
    $paybox->setParameters(array(
        'PBX_CMD'          => 'CMD'.time(),
        'PBX_DEVISE'       => '978',
        'PBX_PORTEUR'      => 'test@paybox.com',
        'PBX_RETOUR'       => 'Mt:M;Ref:R;Auto:A;Erreur:E',
        'PBX_TOTAL'        => '1000',
        'PBX_TYPEPAIEMENT' => 'CARTE',
        'PBX_TYPECARTE'    => 'CB',
        'PBX_EFFECTUE'     => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'success'), UrlGeneratorInterface::ABSOLUTE_URL),
        'PBX_REFUSE'       => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'denied'), UrlGeneratorInterface::ABSOLUTE_URL),
        'PBX_ANNULE'       => $this->generateUrl('lexik_paybox_sample_return', array('status' => 'canceled'), UrlGeneratorInterface::ABSOLUTE_URL),
        'PBX_RUF1'         => 'POST',
        'PBX_REPONDRE_A'   => $this->generateUrl('lexik_paybox_ipn', array('time' => time()), UrlGeneratorInterface::ABSOLUTE_URL),
    ));

    return $this->render(
        'LexikPayboxBundle:Sample:index.html.twig',
        array(
            'url'  => $paybox->getUrl(),
            'form' => $paybox->getForm()->createView(),
        )
    );
}
...
/**
 * Sample action of a confirmation payment page on witch the user is sent
 * after he seizes his payment informations on the Paybox's platform.
 * This action must only containts presentation logic.
 */
public function responseAction($status)
{
    return $this->render(
        'LexikPayboxBundle:Sample:return.html.twig',
        array(
            'status'     => $status,
            'parameters' => $this->getRequest()->query,
        )
    );
}
...

getUrl() 方法在后台进行服务器检查,如果目标服务器无响应,则抛出异常。

在您的业务逻辑中,必须在即时支付通知 (IPN) 发生时进行支付确认。该插件包含一个控制器和操作,用于管理此 IPN 并触发事件。事件包含请求期间传输的所有数据以及一个布尔值,指示签名验证是否成功。

该包包含一个监听器示例,它简单地在每次 ipn 调用上创建一个文件。

namespace Lexik\Bundle\PayboxBundle\Listener;

use Symfony\Component\Filesystem\Filesystem;

use Lexik\Bundle\PayboxBundle\Event\PayboxResponseEvent;

/**
 * Simple listener that create a file for each ipn call.
 */
class PayboxResponseListener
{
    private $rootDir;

    private $filesystem;

    /**
     * Constructor.
     *
     * @param string     $rootDir
     * @param Filesystem $filesystem
     */
    public function __construct($rootDir, Filesystem $filesystem)
    {
        $this->rootDir = $rootDir;
        $this->filesystem = $filesystem;
    }

    /**
     * Creates a txt file containing all parameters for each IPN.
     *
     * @param  PayboxResponseEvent $event
     */
    public function onPayboxIpnResponse(PayboxResponseEvent $event)
    {
        $path = $this->rootDir . '/../data/' . date('Y\/m\/d\/');
        $this->filesystem->mkdir($path);

        $content = sprintf("Signature verification : %s\n", $event->isVerified() ? 'OK' : 'KO');
        foreach ($event->getData() as $key => $value) {
            $content .= sprintf("%s:%s\n", $key, $value);
        }

        file_put_contents($path . time() . '.txt', $content);
    }
}

要创建自己的监听器,只需使其等待 "paybox.ipn_response" 事件。例如,包的监听器

parameters:
    lexik_paybox.sample_response_listener.class: 'Lexik\Bundle\PayboxBundle\Listener\SampleIpnListener'

services:
    ...
    lexik_paybox.sample_response_listener:
        class: %lexik_paybox.sample_response_listener.class%
        arguments: [ %kernel.root_dir%, @filesystem ]
        tags:
            - { name: kernel.event_listener, event: paybox.ipn_response, method: onPayboxIpnResponse }

资源

所有交易参数在 官方文档 中可用。