You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过WP Form在WordPress中集成CyberSource支付网关(无需WooCommerce)

WP Forms集成CyberSource支付网关(无需WooCommerce)

完全可以不依赖WooCommerce实现WP Forms和CyberSource的集成,核心思路是通过WP Forms的钩子拦截表单提交事件,对接CyberSource的支付API触发支付流程。以下是具体实现方案:

一、自定义代码集成(最灵活的方案)

这是最直接的方式,通过WP Forms的内置钩子和CyberSource API完成对接:

  1. 配置WP Forms表单

    • 创建包含金额输入字段(数字类型)、用户邮箱/联系方式的表单,确保开启表单提交验证。
  2. 用钩子拦截表单提交
    使用WP Forms的wpforms_process_complete钩子,在表单数据验证通过后触发支付流程。示例代码可添加到主题的functions.php或自定义插件中:

    add_action( 'wpforms_process_complete', 'cybersource_wpforms_init_payment', 10, 4 );
    function cybersource_wpforms_init_payment( $fields, $entry, $form_data, $entry_id ) {
        // 仅处理目标表单,替换为你的表单ID
        if ( $form_data['id'] !== 123 ) return;
    
        // 从表单提取关键数据
        $payment_amount = $fields['field_2']['value']; // 替换为你的金额字段ID
        $customer_email = $fields['field_email']['value'];
        $order_id = 'WPFORM-' . $entry_id; // 用表单提交ID生成唯一订单号
    
        // 初始化CyberSource客户端(需先安装CyberSource PHP SDK)
        require_once plugin_dir_path( __FILE__ ) . 'cybersource-sdk/autoload.php';
        $config = new \CyberSource\Configuration();
        $config->setMerchantId( get_option('cybersource_merchant_id') );
        $config->setApiKeyId( get_option('cybersource_api_key_id') );
        $config->setApiSecret( get_option('cybersource_api_secret') );
        $config->setEnvironment( get_option('cybersource_environment') ); // sandbox/production
    
        $apiClient = new \CyberSource\ApiClient( $config );
        $paymentApi = new \CyberSource\Api\PaymentsApi( $apiClient );
    
        // 构建支付请求参数
        $request = new \CyberSource\Model\CreatePaymentRequest();
        $request->setClientReferenceInformation(
            (new \CyberSource\Model\Ptsv2paymentsClientReferenceInformation())
                ->setCode( $order_id )
        );
        $request->setOrderInformation(
            (new \CyberSource\Model\Ptsv2paymentsOrderInformation())
                ->setAmountDetails(
                    (new \CyberSource\Model\Ptsv2paymentsOrderInformationAmountDetails())
                        ->setTotalAmount( $payment_amount )
                        ->setCurrency( 'USD' ) // 替换为你的结算货币
                )
        );
    
        try {
            // 创建支付会话
            $response = $paymentApi->createPayment( $request );
            // 跳转至CyberSource支付页面
            if ( isset( $response['redirectUrl'] ) ) {
                wp_redirect( $response['redirectUrl'] );
                exit;
            }
        } catch ( \CyberSource\ApiException $e ) {
            // 捕获错误并返回表单提示
            wpforms()->process->errors[ $form_data['id'] ]['submit'] = '支付初始化失败:' . $e->getMessage();
        }
    }
    
  3. 处理支付结果

    • 在CyberSource后台配置webhook,接收支付成功/失败的通知。
    • 编写webhook处理函数,根据通知更新表单提交状态(如标记为已支付),并给用户发送邮件通知。
  4. 注意事项

    • 不要硬编码CyberSource密钥,用WordPress的update_option和get_option安全存储。
    • 确保服务器支持PHP curl,且开启HTTPS。
    • 先在CyberSource沙箱环境测试,再切换到生产环境。

二、第三方插件辅助(简化开发)

如果不想写自定义代码,可以寻找支持CyberSource+WP Forms集成的第三方插件:

  • 部分通用支付网关插件支持通过短代码或钩子关联WP Forms,允许动态传递表单中的金额字段值。
  • 这类插件通常提供可视化配置界面,减少代码编写量,但可能需要付费授权。

三、为什么你之前的表单提交没触发支付?

WP Forms默认仅负责收集和保存表单数据,没有内置CyberSource支付触发逻辑。之前通过WooCommerce能完成支付,是因为WooCommerce本身有完整的订单-支付流程闭环,而WP Forms需要额外的代码/插件来衔接表单提交和支付网关。

内容的提问来源于stack exchange,提问作者DHEERAJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 02:55:36