我在我的应用程序中使用 Stripe。我想编写一个集成测试来进行付款,以检查 Stripe 是否已创建付款。我正在使用 Stripe.js。
在我的测试中,我需要一个卡 token 来执行测试收费。通常这个 token 会在客户端使用 stripe.js 生成并在请求中发送以执行收费。由于这是仅服务器端测试,是否有某种方法可以从测试中生成 token ?
作为引用,测试将是这样的(使用 php 但原理是相同的):
/** @test **/
public function it_creates_a_charge()
{
$order = factory(Order::class)->create();
$stripe_token = Stripe::generateToken([
'card' => '4242424242424242'
'exp' => '04/2017',
'cvc' => '123'
]); // does not exist afaik
$response = $this->post('charges/store', [
'stripe_token' => $stripe_token,
'order_id' => $order->id,
//etc
]);
// assertions...
}
本质上我是在询问 Stripe API 中是否有允许服务器端 token 生成的东西。
最佳答案
Stripe provides an API call to create tokens from the server:
\Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");
\Stripe\Token::create(array(
"card" => array(
"number" => "4242424242424242",
"exp_month" => 1,
"exp_year" => 2017,
"cvc" => "314"
)
));
edit: Stripe now provides ready-to-use test tokens like
tok_visa
at https://stripe.com/docs/testing#cards.
关于php - 生成用于测试的 Stripe 测试卡 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619582/