wikp/payment-mtgox-bundle

此包已被放弃且不再维护。未建议替代包。

基于MtGox.com(比特币加密货币)创建支付系统的捆绑包

安装: 125

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v0.1.0 2013-02-24 16:01 UTC

This package is not auto-updated.

Last update: 2020-04-03 18:07:32 UTC


README

此捆绑包利用MtGox的IPN(即时支付通知)功能,可以创建例如捐赠系统或简单的商店。您唯一需要做的就是创建实现OrderInterfaceOrder类及其doctrine仓库(实现OrderRepositoryInterface)并将用户重定向到MtGox支付页面(获取重定向地址的实现也包括在此捆绑包中)。

完整文档和大规模重构即将推出,因此仅用于测试目的(我在生产环境中使用这个包,一切运行良好,但我的重构意味着您也需要重构;)。

文档

安装

PaymentMtgoxBundle 安装方式与其他symfony捆绑包类似(使用 composer require,更改 app/AppKernel.php

配置

您首先需要配置 JMSPaymentCoreBundle(因此只需将 payments_secret 放入 `app/config/parameters.yml')。然后获取您的API密钥和密钥,并将它们放入其中。例如:

mtgox_api_key:    your-mtgox-api-key
mtgox_api_secret: "+your-mtgox-api-secret=="

您还需要将 IpnController 添加到您的 app/config/routing.yml

wikp_payment_mtgox:
    resource: "@WikpPaymentMtgoxBundle/Resources/config/routing.yml"
    prefix:   /mtgox

接下来,编写您的订单实体及其仓库

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Payment\CoreBundle\Model\PaymentInstructionInterface;
use Wikp\PaymentMtgoxBundle\Plugin\OrderInterface;

/**
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\OrderRepository")
 * @ORM\Table(name="item_order")
 */
class Order implements OrderInterface
{
    const STATUS_NEW = 0;
    const STATUS_FINISHED = 1;
    const STATUS_CANCELLED = 2;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="JMS\Payment\CoreBundle\Entity\PaymentInstruction")
     * @ORM\JoinColumn(name="payment_instruction_id", referencedColumnName="id")
     */
    private $paymentInstruction;

    /**
     * @ORM\Column(type="smallint")
     */
    private $status;

    public function __construct()
    {
        $this->status = self::STATUS_NEW;
    }

    public function getId()
    {
        return $this->id;
    }

    /**
     * @return \JMS\Payment\CoreBundle\Model\PaymentInstructionInterface
     */
    public function getPaymentInstruction()
    {
        return $this->paymentInstruction;
    }

    public function setPaymentInstruction(PaymentInstructionInterface $paymentInstruction)
    {
        $this->paymentInstruction = $paymentInstruction;
    }

    public function cancel()
    {
        $this->status = self::STATUS_CANCELLED;
    }

    public function approve()
    {
        $this->status = self::STATUS_FINISHED;
    }
}
<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Wikp\PaymentMtgoxBundle\Plugin\OrderRepositoryInterface;

class OrderRepository extends EntityRepository implements OrderRepositoryInterface
{
    public function getOrderById($id)
    {
        return $this->find($id);
    }
}

获取支付URL

您可以按照这种方式获取支付URL,然后重定向用户。

<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Wikp\PaymentMtgoxBundle\Plugin\MtgoxPaymentPlugin;
use Wikp\PaymentMtgoxBundle\Mtgox\RequestType\MtgoxTransactionUrlRequest;
use JMS\Payment\CoreBundle\Entity\PaymentInstruction;
use JMS\Payment\CoreBundle\Model\FinancialTransactionInterface;
use Acme\DemoBundle\Entity\Order;


class DemoController extends Controller
{
    /**
     * @Route("/pay/{amount}", name="_demo_pay")
     * @Template()
     */
    public function helloAction($amount)
    {
        // form validation, etc.

        $currency = 'USD'; //change it, store in Order on config, whatever

        /** @var $ppc \JMS\Payment\CoreBundle\PluginController\PluginController */
        $ppc = $this->get('payment.plugin_controller');
        $ppc->addPlugin($this->get('wikp_payment_mtgox.plugin'));

        $ppc->createPaymentInstruction(
            $instruction = new PaymentInstruction(
                $amount,
                $currency,
                MtgoxPaymentPlugin::SYSTEM_NAME
            )
        );

        $em = $this->get('doctrine.orm.entity_manager');
        $order = new Order();
        $order->setPaymentInstruction($instruction);
        $em->persist($order);
        $em->flush();

        if (FinancialTransactionInterface::STATE_PENDING == $instruction->getState()) {
            $urlRequest = new MtgoxTransactionUrlRequest();
            $urlRequest->setAmount($amount);
            $urlRequest->setIpnUrl($this->generateUrl('wikp_payment_mtgox_ipn', array(), true));
            $urlRequest->setDescription( //info for the user visible on the payment page
                sprintf('You are about to pay for order id=%d', $order->getId())
            );
            $urlRequest->setAdditionalData($order->getId()); //could be useful for debugging
            $urlRequest->setCurrency($currency);
            $urlRequest->setReturnSuccess(
                $this->generateUrl('_demo_payment_successful', array(), true)
            );
            $urlRequest->setReturnFailure(
                $this->generateUrl('_demo_payment_canceled', array(), true)
            );

            return $this->redirect(
                $this->get('wikp_payment_mtgox.plugin')->getMtgoxTransactionUrl($urlRequest)
            );
        }
    }

    /**
     * @Route("/pay-success", name="_demo_payment_successful")
     */
    public function paymentSuccessAction()
    {
        return 'User paid for order but his money could not arrive to your wallet already';
    }

    /**
     * @Route("/pay-cancel", name="_demo_payment_canceled")
     */
    public function paymentCancelAction()
    {
        return 'User has clicked Cancel on mtgox.com';
    }
}

IpnController

当您配置捆绑包并执行支付时,当所有资金都转移到您的MtGox账户时,IpnController应将其标记为完成。

请记住,您无法在本地机器上调试此功能,因为MtGox可能无法访问它。