hakito / cakephp-paypal-rest-plugin

CakePHP的REST API PayPal插件

安装: 422

依赖: 0

建议者: 0

安全: 0

星星: 7

关注者: 2

分支: 5

公开问题: 3

类型:cakephp-plugin

v4.0.0 2022-07-23 18:28 UTC

This package is auto-updated.

Last update: 2024-09-05 10:35:23 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

CakePHP-PayPalRest-Plugin

这是一个简单的CakePHP 4.x PayPal插件,使用REST API (v1)

安装

如果你使用composer,只需添加以下命令:

composer require hakito/cakephp-paypal-rest-plugin

加载插件

在你的bootstrap文件中加载插件

public function bootstrap()
{
    // Call parent to load bootstrap from files.
    parent::bootstrap();

    $this->addPlugin(\PayPal\Plugin::class, ['routes' => true]);
}

创建表格

使用以下命令创建数据库中的pay_pal_payments表:

bin/cake migrations migrate -p PayPal

配置

你可以在tests/config/PayPal.php中找到一个示例配置。只需在你的自己的bootstrap.php中覆盖设置。

使用

以下是最小化的一组设置以启动支付请求:

class OrdersController extends AppController {

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('PayPal.PayPal', []);
    }

    public yourPaymentAction($order) {
        foreach ($order['OrderedItem'] as $orderedItem)
        {
            $quantity = $orderedItem['quantity'];
            $price = $orderedItem['price'];
            $itemName = $orderedItem['name'];
            $itemId = $orderedItem['id'];

            // money values are always integer values in cents
            $this->PayPal->AddArticle($itemName, $quantity, $price, $itemId);
        }

        // optional shipping fee
        $this->PayPal->Shipping = 123; // money values are always integer values in cents

        // Url the client is redirected to when PayPal payment is performed successfully
        // NOTE: This does not mean that the payment is COMPLETE.
        $okUrl = Router::url('/paymentOk', true);

        // Url the client is redirected to whe PayPal payment fails or was cancelled
        $nOkUrl = Router::url('/paymentFailed', true);

        return $this->PayPal->PaymentRedirect($order['id'], $okUrl, $nOkUrl);
    }
}

为了在你的应用中接收支付通知,插件期望有3个事件处理器

$payPalPayments = TableRegistry::getTableLocator()->get('PayPal.PayPalPayments');
$eventManager = $payPalPayments->getEventManager();
$eventManager->setEventList(new EventList());

// Will be called just after PayPal redirects the customer
// back to your site. (You could start a transaction here)
$eventManager->on('PayPal.BeforePaymentExecution',
function($event, $remittanceIdentifier)
{
    // Handled is expected to be set to TRUE, otherwise the plugin
    // will throw an exception
    return ['handled' => true];
});

// Will be called when the REST api call fails or
// the saleState != 'completed' or paymentState != 'approved'
// (You could rollback a transaction here)
$eventManager->on('PayPal.CancelPaymentExecution',
function($event, $remittanceIdentifier) {});

// Will be called after the REST api call
// and only if the saleState == 'completed' and paymentState == 'approved'
// (You could commit a transaction here)
$eventManager->on('PayPal.AfterPaymentExecution',
function($event, $remittanceIdentifier) {});

要全额退款,查找支付并发出退款。

$payPalPayment = $payPalPayments->findByRemittanceIdentifier($remittanceIdentifier);
$payPalPayments->refundPayment($payPalPayment);

备注

当前实现不支持自动处理待处理状态的支付。