カテゴリー:
QA
閲覧数:359 配信日:2019-04-27 07:46
Q37-2
How do I redirect to the success screen after receiving the “payment_intent.succeeded” event via webhook?
・2019/4/27
・How can I display the success screen after making a payment with PaymentIntent?
・Checkout new allows you to specify 'success_url' as shown below, but how to specify 'success_url' if you do not use Checkout?
Code tried (PHP)
・ I can receive "payment_intent.succeeded" event by webhook
・ I do not redirect to the success screen
▼index.php
・Checkout new allows you to specify 'success_url' as shown below, but how to specify 'success_url' if you do not use Checkout?
$checkoutSession = \Stripe\Checkout\Session::create([
'customer_email' => 'customer@example.com',
'success_url' => 'https://xxxx/thanks.php',
Code tried (PHP)
・ I can receive "payment_intent.succeeded" event by webhook
・ I do not redirect to the success screen
▼index.php
<?php
\Stripe\Stripe::setApiKey("sk_test_xxxx");
$paymentintent = \Stripe\PaymentIntent::create([
"amount" => 1099,
"currency" => "jpy",
]);
?>
<input id="cardholder-name" type="text">
<div id="card-element"></div>
<button id="card-button" data-secret="<?php echo $paymentintent->client_secret; ?>">
Submit Payment
</button>
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('pk_test_xxxx');
const elements = stripe.elements();
const cardElement = elements.create('card');
cardElement.mount('#card-element');
const cardholderName = document.getElementById('cardholder-name');
const cardButton = document.getElementById('card-button');
const clientSecret = cardButton.dataset.secret;
cardButton.addEventListener('click', async (ev) => {
const {paymentIntent, error} = await stripe.handleCardPayment(
clientSecret, cardElement, {
payment_method_data: {
billing_details: {name: cardholderName.value}
}
}
);
if (error) {
// Display error.message in your UI.
console.log(error)
} else {
// The payment has succeeded. Display a success message.
console.log("ok")
}
});
</script>
・How do I redirect to the success screen after receiving the "payment_intent.succeeded" event via webhook?
質問後に気が付いたこと
JavaScriptでリダイレクトさせるもエラー
・JavaScriptでwebhook.phpへリダイレクトさせるも「HTTP ERROR 400」発生
・そもそもwebhook.phpで「$_SERVER['HTTP_STRIPE_SIGNATURE']」を受け取るよう設定しているため、それ以外の方法によるアクセスは不可
\Stripe\Checkout\Session::create()メソッドの'success_url'パラメータ処理を行っているコード内容を参考にすればよいのでは?
・探してみるも良く分からない
・'success_url'に関する記述は下記のみ
▼stripe/vendor/stripe/stripe-php/lib/Checkout/Session.php
<?php
namespace Stripe\Checkout;
/**
* Class Session
*
* @property string $id
* @property string $object
* @property string $cancel_url
* @property string $client_reference_id
* @property string $customer
* @property string $customer_email
* @property mixed $display_items
* @property bool $livemode
* @property string $payment_intent
* @property string[] $payment_method_types
* @property string $subscription
* @property string $success_url
*
* @package Stripe
*/
class Session extends \Stripe\ApiResource
{
const OBJECT_NAME = "checkout.session";
use \Stripe\ApiOperations\Create;
use \Stripe\ApiOperations\Retrieve;
}
▼stripe/stripe-php/lib/ApiOperations/Create.php
<?php
namespace Stripe\ApiOperations;
/**
* Trait for creatable resources. Adds a `create()` static method to the class.
*
* This trait should only be applied to classes that derive from StripeObject.
*/
trait Create
{
/**
* @param array|null $params
* @param array|string|null $options
*
* @return \Stripe\ApiResource The created resource.
*/
public static function create($params = null, $options = null)
{
self::_validateParams($params);
$url = static::classUrl();
list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}
}
・Stripe Q39。I want to check the processing for the 'success_url' parameter of the 'Checkout \ Session :: create () method' in the Git-Hub code