Perl 使用 USAePay billing module 时如何切换到 Sandbox server?
支付阿杰
结论
在提交交易前调用 test_transaction(1),让 Business::OnlinePayment::USAePay 连接测试环境:
$tx->test_transaction(1);
$tx->submit();
出现 Specified source key not found,通常是因为 Sandbox 中创建的 source key 被发送到了 production endpoint。USAePay 的测试环境和生产环境使用不同的 source key,两者不能混用。
修改后的代码
use strict;
use warnings;
use Business::OnlinePayment;
use constant {
LOGIN => 'sandbox source key',
PASSWORD => '12345',
};
my $tx = Business::OnlinePayment->new('USAePay');
$tx->content(
login => LOGIN,
password => PASSWORD,
type => 'CC',
action => 'Recurring Authorization',
description => 'Business::OnlinePayment test',
amount => '49.95',
invoice_number => '100100',
name => 'Tofu Beast',
card_number => '4000100011112224',
expiration => '09/19',
address => '1234 Bean Curd Lane',
city => 'San Francisco',
state => 'CA',
zip => '94102',
);
# 必须在 submit() 之前启用测试模式
$tx->test_transaction(1);
$tx->submit();
if ($tx->is_success()) {
print "Card processed successfully: " . $tx->authorization . "\n";
} else {
print "Card was rejected: " . $tx->error_message . "\n";
}
为什么这样配置
Business::OnlinePayment 提供了统一的测试交易开关。在支持该开关的 Business::OnlinePayment::USAePay 版本中,调用:
$tx->test_transaction(1);
USAePay 驱动就会选择测试服务器,而不是默认的生产服务器。这项设置必须放在 $tx->submit() 之前。
还需要确认以下配置:
login使用 USAePay Sandbox 中生成的 source key。password使用与该 Sandbox source key 对应的 PIN。- 不要混用 production source key 和 Sandbox PIN。
- Sandbox 账户中的 source key 已启用,并且没有额外的 IP 地址或权限限制。
版本兼容性
如果调用 test_transaction(1) 后,当前安装的 Business::OnlinePayment::USAePay 仍然访问 production server,可能是该版本没有正确实现测试环境切换。先检查已安装模块的文档或源码,确认它如何处理 test_transaction,必要时升级模块。
不建议直接在业务代码中改写网关 URL,否则部署时容易误连生产环境。如果旧版本确实不支持测试服务器,应先确认该版本公开的配置接口,再指定 Sandbox endpoint,不要猜测构造参数名称。
示例中的有效期 09/19 已经过期。即使服务器切换正确,也应将它替换为 USAePay Sandbox 当前允许的未来有效期,否则可能收到另一条拒绝信息。
备注:内容仅供参考。
