duncanjbrown / simple-stripe
此软件包已被 放弃 并不再维护。未建议替代软件包。
WordPress 的 Stripe 接管工具
0.1.0
2016-01-18 23:23 UTC
Requires
- duncanjbrown/plain-route: ^0.1
- stripe/stripe-php: ^3.5
- vlucas/phpdotenv: ^2.1
This package is not auto-updated.
Last update: 2018-06-19 06:12:37 UTC
README
此插件为您的网站提供一个端点,使用 Stripe 令牌进行收费。
它不提供任何前端代码,永远不会。
它只支持美元,但可以进行扩展。
安装
针对 Bedrock 项目
composer require duncanjbrown/simple-stripe
这将安装插件及其依赖项。
您应该在项目的 .env
中添加 S_STRIPE_PUBLISHABLE_KEY
和 S_STRIPE_SECRET_KEY
。
在纯 WP 中
安装插件,但不要激活它。
打开终端并访问其文件夹,然后运行
composer install
您应该在插件文件夹中的 .env
文件中添加 S_STRIPE_PUBLISHABLE_KEY
和 S_STRIPE_SECRET_KEY
。
激活它。
使用方法
获取 Stripe 令牌。您可以使用 Stripe 检查站 来完成此操作。
从 Stripe 检查站获取令牌后,以以下格式将令牌和金额(以分为单位)通过 POST 发送到 /_stripe/charge
{
's-stripe': {
'token': token,
'amount': 100
}
}
如果收费成功,将调用钩子 simple_stripe_charge_succeeded
。
如果收费失败,将调用钩子 simple_stripe_charge_failed
。
这两个钩子都接收一个 SimpleStripeOneOffPayment
对象。
SimpleStripeOneOffPayment 对象
此对象具有以下属性
$currency // the currency, eg "usd"
$amount_in_cents // the amount charged, in cents
$charge // Stripe response, if the charge succeeded
$errors // a WP_Error containing error messages, if the charge failed
示例
此代码显示处理收费时的成功或失败消息。
add_action( 'simple_stripe_charge_failed', function( $payment ) {
$errors = $payment->errors->get_error_messages();
$message = sprintf( '<ul><li>%s</li></ul>', implode( '</li><li>', $errors ) );
wp_die( '<h2>Something went wrong</h2>' . $message . '<p>Your card has not been charged.</p>',
'Something went wrong', [ 'response' => 400 ] );
});
add_action( 'simple_stripe_charge_succeeded', function( $payment ) {
$amount = sprintf( "$%01.2f", $payment->amount_in_cents / 100 );
wp_die( "<h2>✓ Your payment has been processed</h2>
<p>Thank you for your donation of ${amount}.</p>", 'Thank you', [ 'response' => 201 ] );
});