phwoolcon/payment

Phwoolcon 的支付模块

dev-master / 1.0.x-dev 2017-07-30 07:32 UTC

This package is auto-updated.

Last update: 2024-08-29 04:36:56 UTC


README

Build Status Code Coverage License

Phwoolcon 的支付模块

此模块默认包含支付宝功能。

1. 安装

使用 composer 将此库添加到您的项目中

composer require "phwoolcon/payment":"dev-master"

2. 配置

请创建一个新的配置文件 app/config/production/payment.php
用真实的支付宝配置覆盖默认设置

<?php
return [
    'gateways' => [
        'alipay' => [
            'partner' => 'YOUR_PARTNER_ID_APPLIED_FROM_ALIPAY',
            'seller_id' => 'YOUR_SELLER_EMAIL_APPLIED_FROM_ALIPAY',
            'private_key' => '-----BEGIN RSA PRIVATE KEY-----
YOUR_PRIVATE_KEY_PROVIDED_TO_ALIPAY
-----END RSA PRIVATE KEY-----',
            'ali_public_key' => '-----BEGIN PUBLIC KEY-----
THE_PUBLIC_KEY_APPLIED_FROM_ALIPAY
-----END PUBLIC KEY-----',
        ],
    ],
];

3. 使用

Phwoolcon Payment 实现了一个抽象的支付处理器,所有
支付流程都通过 Processor::run() 调用

3.1. 开始支付宝支付请求(移动网页支付)

<?php
use Phalcon\Di;
use Phwoolcon\Payment\Processor;

$di = Di::getDefault();
Processor::register($di);

$tradeId = md5(microtime());
$payload = Processor::run(Payload::create([
    'gateway' => 'alipay',
    'method' => 'mobile_web',
    'action' => 'payRequest',
    'data' => [
        'trade_id' => $tradeId,
        'product_name' => 'Test product',
        'client_id' => 'test_client',
        'user_identifier' => 'Test User',
        'amount' => 1,
    ],
]));
echo get_class($payload);       // prints Phwoolcon\Payment\Process\Payload

$result = $payload->getResult();
echo get_class($result);        // prints Phwoolcon\Payment\Process\Result

$order = $result->getOrder();
echo get_class($order);         // prints Phwoolcon\Payment\Model\Order

echo $order->getStatus();       // prints pending

$redirectUrl = $order->getPaymentGatewayUrl();
echo $redirectUrl;              // prints url like this:
                                // https://mapi.alipay.com/gateway.do?service=alipay.wap.create.direct.pay.by.user&partner=...
                                // You can send 302 response to make browser
                                // redirecting to this url to complete a pay request

$returnUrl = $order->getOrderData('alipay_request.return_url');
echo $returnUrl;                // prints url like this:
                                // http://yoursite.com/api/alipay/return
                                // Alipay will redirect the user back to this url
                                // once the payment is complete or closed

$notifyUrl = $order->getOrderData('alipay_request.notify_url');
echo $notifyUrl;                // prints url like this:
                                // http://yoursite.com/api/alipay/callback
                                // Alipay will post callback data to this url
                                // once the payment is complete or closed

3.2. 处理支付宝回调

<?php
use Phalcon\Di;
use Phwoolcon\Payment\Processor;

$di = Di::getDefault();
Processor::register($di);
$payload = Processor::run(Payload::create([
    'gateway' => 'alipay',
    'method' => 'mobile_web',
    'action' => 'callback',
    'data' => $_POST,
]));
$result = $payload->getResult();
echo $result->getResponse();    // prints success

4. 如何创建自定义支付方法

支付方法定义在配置文件 payment.php 中,您可以在需要时
添加任何支付网关/方法。

4.1. 创建支付网关/方法配置结构

编辑 app/config/payment.php

<?php
return [
    'gateways' => [

        .
        .
        .

        'your_gateway' => [
            'label' => 'Payment Gateway Name',
            'order_prefix' => 'SOME_PREFIX',
            'methods' => [
                'payment_method_1' => [
                    'class' => 'Fully\Qualified\Class\Name',
                    'label' => 'Payment Method Name',
                ],
                'payment_method_2' => [
                    'class' => 'Fully\Qualified\Class\Name',
                    'label' => 'Payment Method Name',
                ],
            ],
            'required_callback_parameters' => [
                'order_id',
                'amount',
                'status',
                'sign',
            ],
            'any_options' => 'value',
            'another_option' => 'value',
        ],
    ],
];

4.2. 填写真实世界的配置

不要 在前面的文件中填写真实世界的配置,如果您将其
添加到版本控制软件(如 git, svn),可能会泄露您的
支付网关账户信息给公众。

相反,请在 app/config/production/payment.php 文件中
填写这些配置,并将其添加到您的 VCS 忽略列表中。

4.3. 创建支付方法类

支付方法类必须实现 Phwoolcon\Payment\MethodInterface

Phwoolcon\Payment\MethodTrait 中抽象了一些常见功能,您可以在
您的类中使用它。

请快速查看 Phwoolcon\Payment\Tests\Helper\TestPaymentMethod

支付方法应该实现至少两个操作
payRequestcallback

您可以为支付方法添加任何操作,通过将操作名称传递给 Processor::run()
有效负载来调用它们

任何操作都应该返回一个 Phwoolcon\Payment\Process\Result,其中
应该包含一个 Order 或错误。

中文