Payum支付集成入门指南:从零开始构建支付系统

Payum支付集成入门指南:从零开始构建支付系统

Payum PHP Payment processing library. It offers everything you need to work with payments: Credit card & offsite purchasing, subscriptions, payouts etc. Payum 项目地址: https://gitcode.com/gh_mirrors/pa/Payum

什么是Payum

Payum是一个强大的PHP支付处理库,它提供了统一的API来集成多种支付网关。无论是PayPal、Stripe还是线下支付,Payum都能让你用相似的代码结构来处理不同的支付方式,大大简化了支付系统的开发流程。

环境准备

在开始之前,确保你的开发环境满足以下要求:

  1. PHP 7.2或更高版本
  2. Composer包管理工具
  3. 基本的PHP开发环境

安装Payum核心组件

Payum采用模块化设计,你可以根据需要安装特定的支付网关模块。以下是安装基础组件和离线支付模块的命令:

php composer.phar require payum/offline php-http/guzzle7-adapter

这里我们选择了离线支付模块(payum/offline)作为示例,实际项目中你可以替换为其他支付网关如payum/paypal-express-checkout-nvp或payum/stripe。

Payum工作流程解析

Payum遵循标准化的支付处理流程,主要包括以下几个步骤:

  1. 配置阶段:设置支付网关和存储
  2. 准备阶段:创建支付订单
  3. 捕获阶段:处理支付请求
  4. 完成阶段:处理支付结果

Payum工作流程图

详细配置指南

1. 基础配置(config.php)

首先创建配置文件,这里我们设置一个离线支付网关作为示例:

<?php
// config.php

use Payum\Core\PayumBuilder;
use Payum\Core\Payum;
use Payum\Core\Model\Payment;

$paymentClass = Payment::class;

$payum = (new PayumBuilder())
    ->addGateway('aGateway', [
        'factory' => 'offline',
    ])
    ->getPayum();

在实际项目中,你可能需要考虑使用更可靠的存储方案如数据库存储,而非示例中的文件系统存储。

2. 准备支付订单(prepare.php)

准备脚本负责创建支付订单并设置必要信息:

<?php
include __DIR__.'/config.php';

$gatewayName = 'aGateway';
$storage = $payum->getStorage($paymentClass);

$payment = $storage->create();
$payment->setNumber(uniqid());
$payment->setCurrencyCode('EUR');
$payment->setTotalAmount(123); // 1.23 EUR
$payment->setDescription('订单描述');
$payment->setClientId('客户ID');
$payment->setClientEmail('客户邮箱');

// 设置支付网关需要的额外参数
$payment->setDetails([
    // 支付网关特定参数
]);

$storage->update($payment);

// 创建捕获令牌并重定向
$captureToken = $payum->getTokenFactory()->createCaptureToken(
    $gatewayName, 
    $payment, 
    'done.php'
);

header("Location: ".$captureToken->getTargetUrl());

3. 捕获支付请求(capture.php)

捕获脚本处理实际的支付请求,这段代码是通用的,适用于所有支付网关:

<?php
include __DIR__.'/config.php';

$token = $payum->getHttpRequestVerifier()->verify($_REQUEST);
$gateway = $payum->getGateway($token->getGatewayName());

if ($reply = $gateway->execute(new Capture($token), true)) {
    if ($reply instanceof HttpRedirect) {
        header("Location: ".$reply->getUrl());
        die();
    } elseif ($reply instanceof HttpPostRedirect) {
        echo $reply->getContent();
        die();
    }
    throw new \LogicException('不支持的回复类型');
}

$payum->getHttpRequestVerifier()->invalidate($token);
header("Location: ".$token->getAfterUrl());

4. 处理支付结果(done.php)

完成脚本处理支付结果,检查状态并返回响应:

<?php
include __DIR__.'/config.php';

$token = $payum->getHttpRequestVerifier()->verify($_REQUEST);
$gateway = $payum->getGateway($token->getGatewayName());

$gateway->execute($status = new GetHumanStatus($token));
$payment = $status->getFirstModel();

header('Content-Type: application/json');
echo json_encode([
    'status' => $status->getValue(),
    'order' => [
        'total_amount' => $payment->getTotalAmount(),
        'currency_code' => $payment->getCurrencyCode(),
        'details' => $payment->getDetails(),
    ],
]);

最佳实践建议

  1. 生产环境注意事项

    • 避免使用文件系统存储支付数据
    • 实现适当的错误处理和日志记录
    • 考虑添加CSRF保护
  2. 安全建议

    • 定期使旧令牌失效
    • 验证所有输入数据
    • 实现适当的访问控制
  3. 性能优化

    • 考虑缓存支付网关配置
    • 优化数据库查询
    • 实现异步通知处理

扩展与定制

Payum的强大之处在于它的可扩展性。你可以:

  1. 创建自定义支付网关
  2. 添加支付前后的事件处理
  3. 集成自定义存储后端
  4. 开发特定业务逻辑的扩展

通过掌握Payum的基础流程,你已经具备了集成各种支付网关的能力。无论是简单的信用卡支付还是复杂的订阅系统,Payum都能提供稳定可靠的解决方案。

Payum PHP Payment processing library. It offers everything you need to work with payments: Credit card & offsite purchasing, subscriptions, payouts etc. Payum 项目地址: https://gitcode.com/gh_mirrors/pa/Payum

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱焰菲Wesley

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值