cpierce / paypal-wpp
Client library for Paypal Web Payments Pro.
Package info
Type:cakephp-plugin
pkg:composer/cpierce/paypal-wpp
3.1
2021-11-02 14:46 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: 5.5.*
README
This package was written to interact with any PHP application. Below is plain PHP usage first, followed by a CakePHP 5 example showing how to wire it into a form.
Requirements
- PHP 8.3 or later
- ext-curl
Installation
Install via composer:
composer require cpierce/paypal-wpp
Plain PHP Usage
<?php 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'); }
doDirectPayment() throws a \RuntimeException if a required field (name,
amount, card number, expiration date) is missing, and returns false if no
response comes back from PayPal.
CakePHP 5 Example
Configuration
Add your service credentials to your app config (for example
config/app_local.php):
return [ // ... 'PaypalWPP' => [ 'username' => 'username_api1.domain.com', 'password' => '5SWM6YY8YSUY888', 'signature' => 'tlArzO7mr5uXMO6.H2zPIuzAFYn4irhcVyzOPeiUcocJF.H3mGr', ], ];
Making a transaction to PayPal WPP
File: src/Form/SalesForm.php
<?php 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 required.')) ->notBlank('last_name', __('Your last name is required.')) ->creditCard('card_number', [ 'amex', 'visa', 'disc', 'mc', ], __('Please enter a valid credit card number.')) ->notBlank('amount', __('Please enter an amount.')); return $validator; } /** * @param array<string, mixed> $data * @return bool */ protected function process(array $data): bool { $paypal = new PaypalWPP(Configure::read('PaypalWPP')); $payment = $paypal->doDirectPayment($data); if ($payment !== false && $payment['ACK'] === 'Success') { $this->parseData = [ 'transaction_id' => $payment['TRANSACTIONID'], ]; return true; } $this->parseData = [ 'failure_message' => $payment['L_LONGMESSAGE0'] ?? 'No response from gateway.', 'failure_short' => $payment['L_SHORTMESSAGE0'] ?? 'Failure', ]; return false; } /** * @return array<string, string> */ public function getParseData(): array { return $this->parseData; } }
File: src/Controller/SalesController.php
<?php 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; } }
Development
composer install composer test # PHPUnit composer stan # PHPStan (level 8) composer check # both