cpierce/paypal-wpp

Paypal Web Payments Pro 的客户端库。

安装: 368

依赖: 0

建议者: 0

安全性: 0

星星: 2

关注者: 7

分支: 1

开放问题: 0

类型:cakephp-plugin

3.1 2021-11-02 14:46 UTC

This package is auto-updated.

Last update: 2024-08-29 03:42:07 UTC


README

Build Status

此包是为了与任何PHP应用程序交互而编写的,但我会使用 CakePHP 3.x 来演示如何连接到它。

Composer 设置

首先,我们希望在 composer.json 文件中添加库。需要将 "cpierce/paypal-wpp": "3.*" 添加到您的require中,如下所示

文件: composer.json

{
    "name": "cakephp/app",
    "description": "CakePHP skeleton app",
    "homepage": "https://cakephp.com.cn",
    "type": "project",
    "license": "MIT",
    "require": {
        "php": ">=5.5.9",
        "cakephp/cakephp": "~3.3",
        "mobiledetect/mobiledetectlib": "2.*",
        "cakephp/migrations": "~1.0",
        "cakephp/plugin-installer": "*",
        "cpierce/paypal-wpp": "3.*"
    },
    "require-dev": {
        "psy/psysh": "@stable",
        "cakephp/debug_kit": "~3.2",
        "cakephp/bake": "~1.1"
    },
    "suggest": {
        "phpunit/phpunit": "Allows automated tests to be run without system-wide install.",
        "cakephp/cakephp-codesniffer": "Allows to check the code against the coding standards used in CakePHP."
    },
    "autoload": {
        "psr-4": {
            "App\\": "src"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Test\\": "tests",
            "Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
        }
    },
    "scripts": {
        "post-install-cmd": "App\\Console\\Installer::postInstall",
        "post-autoload-dump": "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump"
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

保存此更改后,运行 composer update 并让包下载到 vendor 应用中。

通用配置

开始之前,您需要配置服务用户名、密码和签名。配置应类似于以下内容

文件: config/bootstrap.php

/**
 * Paypal WPP Payload
 */
Configure::write('PaypalWPP.username', 'username_api1.domain.com');
Configure::write('PaypalWPP.password', '5SWM6YY8YSUY888');
Configure::write('PaypalWPP.signature', 'tlArzO7mr5uXMO6.H2zPIuzAFYn4irhcVyzOPeiUcocJF.H3mGr');

向 PayPal WPP 发送交易

配置设置完成后,您将使用库连接到 PayPal WPP。

文件: src/Form/SalesForm.php

<?php

namespace App\Form;

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

/**
 * Sales Form class.
 *
 * @extends Cake\Form\Form
 */
class SalesForm extends Form
{
    /**
     * Parse Data Array.
     *
     * @var array
     */
    protected $parseData = [];

    /**
     * Build Schema Method.
     *
     * @param Schema $schema
     *
     * @return Schema $schema
     */
    protected function _buildSchema(Schema $schema)
    {
        return $schema->addField('first_name', 'string')
            ->addField('last_email', 'string')
            ->addField('card_number', 'string')
            ->addField('amount', 'string');
    }

    /**
     * Build Validator Method.
     *
     * @param Validator $validator
     *
     * @return Validator $validator
     */
    protected function _buildValidator(Validator $validator)
    {
        return $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.'));
    }

    /**
     * Execute Method.
     *
     * @param array $data
     *
     * @return bool
     */
    protected function _execute(array $data)
    {
        $paypal = new PaypalWPP(Configure::read('PaypalWPP'));

        $payment = $paypal->doDirectPayment($data);

        if ($payment['ACK'] == 'Success') {
            $this->parseData = [
                'transaction_id' => $payment['TRANSACTIONID'],
            ];
            return true;
        } else {
            $this->parseData = [
                'failure_message' => $payment['L_LONGMESSAGE0'],
                'failure_short'   => $payment['L_SHORTMESSAGE0'],
            ];
        }

        return false;
    }

    /**
     * Get Parse Data Method.
     *
     * @return string
     */
    public function getParseData()
    {
        return $this->parseData;
    }
}

文件: src/Controller/SalesController.php

<?php

namespace App\Controller;

use App\Form\SalesForm;

/**
 * Sales Controller.
 */
class SalesController extends AppController
{
    /**
     * Add Method.
     */
    public function add()
    {
        $sales = new SalesForm();

        if ($this->request->is(['post', 'put'])) {
            $transaction_execute = $sales->execute($this->request->data);
            $transaction = $sales->getParseData();
            if ($transaction_execute) {
                $this->Flash->success(
                    __('Payment Completed Successfully: '.$transaction['transaction_id'])
                );
                $this->render('success');
            } else {
                $this->Flash->error(
                    __('Payment Failed: '.$transaction['failure_message'].'['.$transaction['failure_short'].']')
                );
            }
        }

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