PHP code example of cpierce / paypal-wpp

1. Go to this page and download the library: Download cpierce/paypal-wpp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cpierce / paypal-wpp example snippets




use PaypalWPP\PaypalWPP;

$paypal = new PaypalWPP([
    'username'  => 'username_api1.domain.com',
    'password'  => '5SWM6YY8YSUY888',
    'signature' => 'tlArzO7mr5uXMO6.H2zPIuzAFYn4irhcVyzOPeiUcocJF.H3mGr',
    // Optional — defaults to the live endpoint. Use the sandbox while testing:
    // 'endpoint' => 'https://api-3t.sandbox.paypal.com/nvp',
]);

$payment = $paypal->doDirectPayment([
    'first_name'      => 'Chris',
    'last_name'       => 'Pierce',
    'amount'          => '48.97',
    'card_number'     => '4111-1111-1111-1111',
    'cvv2'            => '389',
    'invoice_number'  => '31337',
    'expiration_date' => [
        'month' => 2,
        'year'  => 2030,
    ],
]);

if ($payment !== false && $payment['ACK'] === 'Success') {
    echo 'Transaction ID: ' . $payment['TRANSACTIONID'];
} else {
    echo 'Payment failed: ' . ($payment['L_LONGMESSAGE0'] ?? 'no response');
}

return [
    // ...
    'PaypalWPP' => [
        'username'  => 'username_api1.domain.com',
        'password'  => '5SWM6YY8YSUY888',
        'signature' => 'tlArzO7mr5uXMO6.H2zPIuzAFYn4irhcVyzOPeiUcocJF.H3mGr',
    ],
];


declare(strict_types=1);

namespace App\Form;

use Cake\Core\Configure;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
use PaypalWPP\PaypalWPP;

/**
 * Sales Form class.
 */
class SalesForm extends Form
{
    /**
     * Parsed transaction result data.
     *
     * @var array<string, string>
     */
    protected array $parseData = [];

    /**
     * @param \Cake\Form\Schema $schema
     * @return \Cake\Form\Schema
     */
    protected function _buildSchema(Schema $schema): Schema
    {
        $schema->addField('first_name', ['type' => 'string', 'length' => 255]);
        $schema->addField('last_name', ['type' => 'string', 'length' => 255]);
        $schema->addField('card_number', ['type' => 'string', 'length' => 20]);
        $schema->addField('cvv2', ['type' => 'string', 'length' => 4]);
        $schema->addField('amount', ['type' => 'string', 'length' => 20]);

        return $schema;
    }

    /**
     * @param \Cake\Validation\Validator $validator
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator): Validator
    {
        $validator
            ->notBlank('first_name', __('Your first name is  }
}


declare(strict_types=1);

namespace App\Controller;

use App\Form\SalesForm;

/**
 * Sales Controller.
 */
class SalesController extends AppController
{
    /**
     * Add Method.
     *
     * @return \Cake\Http\Response|null
     */
    public function add(): ?\Cake\Http\Response
    {
        $sales = new SalesForm();

        if ($this->request->is(['post', 'put'])) {
            if ($sales->execute($this->request->getData())) {
                $transaction = $sales->getParseData();
                $this->Flash->success(
                    __('Payment Completed Successfully: {0}', $transaction['transaction_id'])
                );

                return $this->render('success');
            }

            $transaction = $sales->getParseData();
            $this->Flash->error(
                __('Payment Failed: {0} [{1}]', $transaction['failure_message'], $transaction['failure_short'])
            );
        }

        $this->set(compact('sales'));

        return null;
    }
}