Payum - 捕获时调度的事件?

时间:2016-02-05 15:16:37

标签: symfony events payum

我想知道Payum(或PayumBundle)在付款时是否会发送活动? 目的是向客户发送确认电子邮件。

我发现文档没有任何内容,我在代码源中只找到了三个事件:

  • payum.gateway.pre_execute
  • payum.gateway.execute
  • payum.gateway.post_execute

但我猜它与付款本身没有关系。顺便说一句,似乎从未发过Stripe付款。

感谢。

3 个答案:

答案 0 :(得分:0)

默认情况下,Payum不会发送任何事件。它旨在通过扩展进行扩展。例如,如果您想在实例推送通知(Paypal IPN)上执行某些操作,则可以添加如下所述的扩展名:http://payum.org/doc/1.0/Core/instant-payment-notification

还有一个dispatch events的扩展名。您必须手动创建它并添加到payum。

答案 1 :(得分:0)

我遇到了同样的问题:在某些付款操作上调度自定义事件。在我的情况下,我想处理授权捕获操作。

我注意到 Payum请求可能会多次处理1次付款操作,这就是为什么我需要将我的付款标记为已处理,以便仅在我的自定义事件后发送(目的为{{1属性)。

这是我的付款实体:

Payment::$customPaymentEventDispatched

然后注册Payum eventDispatcherExtension和我的自定义监听器

<?php

namespace MyApp\PaymentBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Payum\Core\Model\Payment as BasePayment;

class Payment extends BasePayment
{
    // ...

    // Not persisted
    protected $payable;
    protected $customPaymentEventDispatched = false;

    // ...

    public function setPayable($payable)
    {
        $this->payable = $payable;

        return $this;
    }

    public function getPayable()
    {
        return $this->payable;
    }

    public function markAsCustomPaymentEventDispatched()
    {
        $this->customPaymentEventDispatched = true;

        return $this;
    }

    public function isCustomPaymentEventDispatched()
    {
        return $this->customPaymentEventDispatched;
    }
}

听众的代码:

services:        

    payum.extension.event_dispatcher:
        class: Payum\Core\Bridge\Symfony\Extension\EventDispatcherExtension
        arguments: ["@event_dispatcher"]
        tags:
            - { name: payum.extension, all: true, prepend: false }

    my_app_payment.payum.listener.payment_listener:
        class: MyApp\PaymentBundle\EventListener\PaymentListener
        arguments:
            - "@monolog.logger.payment"
        tags:
            - { name: kernel.event_listener, event: payum.gateway.post_execute }

我对此实现并不完全满意,但我没有找到更好的方法来实现这一目标。我将非常感谢您的任何反馈!

答案 2 :(得分:0)

您可以检查是否已捕获付款:

use Payum\Core\Action\CapturePaymentAction;
use Payum\Core\Bridge\Symfony\Event\ExecuteEvent;
use Payum\Core\Request\Capture;
use Payum\Core\Request\GetHumanStatus;

class SuccessPaymentListener
{
    public function onPostExecute(ExecuteEvent $event) {
        $context = $event->getContext();
        $action = $context->getAction();
        $request = $context->getRequest();
        if ($action instanceof CapturePaymentAction && $request instanceof Capture) {
            $status = new GetHumanStatus($request->getToken());
            $gateway = $context->getGateway();
            $gateway->execute($status);
            if ($status->isCaptured()) {
                $payment = $status->getFirstModel();
                //here is your stuff
            }
        }
    }
}

services.yml:

app.payum.extension.event_dispatcher:
    class: Payum\Core\Bridge\Symfony\Extension\EventDispatcherExtension
    arguments: ["@event_dispatcher"]
    tags:
        - { name: payum.extension, all: true, prepend: false }

app.success_payment_listener:
    class: AppBundle\Listener\SuccessPaymentListener
    tags:
        - { name: kernel.event_listener, event: payum.gateway.post_execute, method: onPostExecute }