Not able to send payment to PayPal using Symfony

2019-09-04 02:31发布

问题:

I am using Symfony and trying to send payment details to PayPal using PayumBundle, I got PayumBundle configured and I got it to work until getting redirected to paypal but no payment details were being sent so I searched on line and came across this question in SO and this is the exact blank information screen I am seeing.

Looking at the documentation that answer is pointing to I came up with the following piece of code but when I try to access to route that is calling the following prepare Action. I run into an error

public function prepareAction(Request $request)
{
    $paymentName = 'paypal';

    $storage = $this->getPayum()->getStorage('ClickTeck\featuresBundle\Entity\Orders');

    /** @var \ClickTeck\featuresBundle\Entity\Orders $paymentDetails */
    $paymentDetails = $storage->create();
    $paymentDetails['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD';
    $paymentDetails['PAYMENTREQUEST_0_AMT'] = 1500;
    $storage->update($paymentDetails);

    $notifyToken = $this->getTokenFactory()->createNotifyToken($paymentName, $paymentDetails);
    $captureToken = $this->getTokenFactory()->createCaptureToken(
        $paymentName,
        $paymentDetails,
        'payment_done'
    );

    $paymentDetails['PAYMENTREQUEST_0_NOTIFYURL'] = $notifyToken->getTargetUrl();
    $paymentDetails['INVNUM'] = $paymentDetails->getId();
    $storage->update($paymentDetails);
    return $this->redirect($captureToken->getTargetUrl());

}

Error: Cannot use object of type ClickTeck\featuresBundle\Entity\Orders as array

I then tried using the following but that leads to another error at paypal end

$paymentDetails->PAYMENTREQUEST_0_CURRENCYCODE = 'USD';
$paymentDetails->PAYMENTREQUEST_0_AMT = 1500;

This transaction cannot be processed. The amount to be charged is zero

This is how my Order Entity looks like

<?php

namespace ClickTeck\featuresBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Payum\Core\Model\Order as BaseOrder;


/**
 * Orders
 */
class Orders extends BaseOrder
{
    /**
     * @var integer
     */
    protected $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

Can someone please help me find out what is this happening? Its been 2 days I am trying to make the PayumBundle work.

I will really appreciate any help in this. How do I send payment details to PayPal?

回答1:

A bit of theory:

You messed to different ways of using Payum. First one is completely custom (other words payment specific format). In this case you can use whatever that implements \ArrayAccess interface. The fields names and their values looks same as they are in Paypal docs.

The other way is a thin layer over first one which adds unification. Here you have to use Order model and fill it using available setters. You can still add some specific fields using setDetails method.

There is a third method: Use your own model (Order\Invoice\Payment you name it) and provide an integration inside Payum.

Now the answer:

How does this \ClickTeck\featuresBundle\Entity\Orders model look like? Do you implement OrderInterface or extend ArrayObject from Payum? I guess the first one. If you want to use completely custom solution extend ArrayObject and use it as array. If you need unified variant (you are going to other payment in future) go with Order



回答2:

As requested, here is a simple example of building a Buy Now button to a twig template:

{# buynow.html.twig #}
<script 
data-tax="0" 
data-shipping="0" 
data-amount="{{ total }}"
data-quantity="1" 
data-name="Player Registration {{ playDate ~ ' ' ~ player.nameLastPlayer ~ ', ' ~ player.nameFirstPlayer }} {{ player.initialPlayer }}"
data-button="buynow" 
data-custom="My custom information"
src="{{  asset('bundles/ceradteam/js/paypal-button.min.js') }}?merchant=MYMERCHANTID" 
></script>

The controller passes in the total amount. You will need to set your merchant id.

The paypal-button.min.js is a java script file generated by the paypal development site. It's been awhile since I made mine but it should be straight forward. And that is all you need for a simple payment.

Note that there is a lot more advanced stuff you can do with paypal but this might point you in the right direction.