Skip to content

Instantly share code, notes, and snippets.

@makasim
Last active December 12, 2015 09:39
Show Gist options
  • Save makasim/4753506 to your computer and use it in GitHub Desktop.
Save makasim/4753506 to your computer and use it in GitHub Desktop.

Configure paypal express checkout payment

Step 1. Download paypal payum lib

Add the following lines in your composer.json file:

{
    "require": {
        "payum/paypal-express-checkout-nvp": "dev-master"
    }
}

Note: You may want to adapt this line to use a specific version.

Now, run composer.phar to download the bundle:

$ php composer.phar install

Note: You can immediately start using it. The autoloading files have been generated by composer and already included to the app autoload file.

Step 2: Basic configuration.

1. Configure payment context

#app/config/config.yml

payum:
    contexts:
        your_context_name:
            paypal_express_checkout_nvp_payment:
                api:
                    options:
                        username:  'get this from gateway side'
                        password:  'get this from gateway side'
                        signature: 'get this from gateway side'
                        sandbox: true

Warning:

You have to changed this name your_context_name to something related to your domain, for example post_a_job_with_paypal

2-a. Configure doctrine storage

Extend payment instruction class with added id property:

<?php
//src/Acme/DemoBundle/Entity

namespace AcmeDemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Entity\PaymentInstruction;

/**
 * @ORM\Entity
 */
class PaypalPaymentInstruction extends PaymentInstruction
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;
}

and configure storage to use this model:

#app/config/config.yml

payum:
    contexts:
        your_context_name:
            doctrine_storage:
                driver: orm
                model_class: AcmeDemoBundle\Entity\PaypalPaymentInstruction

doctrine:
    orm:
        entity_managers:
            default:
                mappings: 
                    payum_paypal_express_checkout_nvp:                          
                        is_bundle: false
                        type: xml 
                        dir: %kernel.root_dir%/../vendor/payum/paypal-express-checkout-nvp/src/Payum/Paypal/ExpressCheckout/Nvp/Bridge/Doctrine/Resources/mapping
                        prefix: Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Entity

2-b. Configure filesystem storage

Extend payment instruction class with added id property:

<?php
//src/Acme/DemoBundle/Model

namespace AcmeDemoBundle\Model;

use Payum\Paypal\ExpressCheckout\Nvp\PaymentInstruction;

class PaypalPaymentInstruction extends PaymentInstruction
{
    protected $id;
    
    public function getId()
    {
        return $this->id;
    }
}

and configure storage to use this model:

#app/config/config.yml

payum:
    contexts:
        your_name_here:
            filesystem_storage:
                model_class: Acme\DemoBundle\Model\PaypalPaymentInstruction
                storage_dir: %kernel.root_dir%\Resources\payments
                id_property: id

Step 3. Create simple instruction:

Note : Here we assume you choose choose doctrine storage

<?php
//src/Acme/DemoBundle/Controller
namespace AcmeDemoBundle\Controller;

use Acme\DemoBundle\Entity\PaypalPaymentInstruction;

class PaymentController extends Controller 
{
    public function captureWithPaypalExpressCheckoutAction()
    {
        $contextName = 'your_context_name';
    
        $paymentContext = $this->get('payum')->getContext($contextName);
    
        /** @var PaypalPaymentInstruction */
        $instruction = $paymentContext->getStorage()->createModel();
        
        $returnUrl = $this->get('router')->generate('payum_payment_capture', array(
            'contextName' => 'your_context',
            'modelId' => $simpleSell->getId(), // you have create this method
        ), $absolute = true);
        $instruction->setReturnurl($returnUrl);
        $instruction->setCancelurl($returnUrl);
        
        $instruction->setInvnum();
        $instruction->setPaymentrequestCurrencycode(0, 'USD');
        $instruction->setPaymentrequestAmt(0,  1.23));
        
        $paymentContext->getStorage()->updateModel($instruction);
        $instruction->setInvnum($instruction->getId());
        
        return $this->forward('PayumBundle:Capture:do', array(
            'contextName' => $contextName,
            'modelId' => $instruction->getId()
        ));
    }
}

Warning:

The example assume you imported payum routings. So this payum_payment_capture is exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment