#saumil
1 messages · Page 1 of 1 (latest)
You may set up the Webhooks and listen to invoice.paid event to get the payment update of the invoices of the subscription every billing cycle: https://stripe.com/docs/billing/subscriptions/webhooks
i am using webhook for intent payment can i use subscription payment and invoice.paid event in this hook, how to use in this code please suggest me code is
public function okt_stripe_event_listener() {
if ( isset( $_GET['webhook-listener'] ) && $_GET['webhook-listener'] == 'stripe' ) {
global $stripe_options;
$payload = @file_get_contents( 'php://input' );
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$endpoint_secret = 'whsec_UJ3nS0qMSkUzimPogcker0Sk34b';
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch ( \UnexpectedValueException $e ) {
// Invalid payload
http_response_code( 400 );
exit();
} catch ( \Stripe\Exception\SignatureVerificationException $e ) {
// Invalid signature
http_response_code( 400 );
exit();
}
if ( $event->type == 'payment_intent.succeeded' ) {
$paymentIntent = $event->data->object;
$this->handleCompletedCheckoutSession( $paymentIntent );
}
http_response_code( 200 );
}
}
public function handleCompletedCheckoutSession( $paymentIntent ) {
$manage_donations = Okt_Manage_Donations::get_instance();
$paymentIntentId= $paymentIntent['id'];
$this->write_log( "Fulfilling order..." );
$manage_donations->update_donation(array('status' => 'completed'));
$this->write_log( $paymentIntent );
}
Your code currently listening to payment_intent.succeeded event only in:
if ( $event->type == 'payment_intent.succeeded' ) {
$paymentIntent = $event->data->object;
$this->handleCompletedCheckoutSession( $paymentIntent );
}
The steps for listening invoice.paid event will be:
- In the Webhook configuration in the Dashboard, you should enable listening to
invoice.paidevent (See screenshot) - In your code, add another
invoice.paidcondition to handle the action you intend to achieve. For example,
if ( $event->type == 'invoice.paid' ) {
$invoice = $event->data->object;
// action to perform on $invoice
}
i am created js code but i am not sure how listen invoice paid and subscription created agains createCardPayment
success: function(response) {
that.prop('disabled', false);
stripe.confirmCardPayment(response, {
payment_method: {
card: card,
billing_details: {
name: 'Jenny Rosen'
}
}
}).then(function(result) {
if (result.error) {
// Show error to your customer (e.g., insufficient funds)
window.location.href = "https://dev.nirmoh.com.au/failed/";
console.log(result.error.message);
} else {
// The payment has been processed!
if (result.paymentIntent.status === 'succeeded') {
console.log("Success");
window.location.href = "https://dev.nirmoh.com.au/success/";
// $('#payment-status').html('Payment succeeded! <a href="https://dev.nirmoh.com.au/success/">Click here</a> to go to the success page.');
// Show a success message to your customer
// There's a risk of the customer closing the window before callback
// execution. Set up a webhook or plugin to listen for the
// payment_intent.succeeded event that handles any business critical
// post-payment actions.
}
// if(response.success){
// window.location.href = "https://dev.nirmoh.com.au/success/";
// }
}
});
},
invoice.paid event is asynchronous using Webhooks and it won't be available in createCardPayment
my response is this so error fire in console
What is the value of your response parameter? It looks like the value is a subscription ID instead of client secret (pi_xxx_secret_xxx)
yes value is subscription id so how to match with subscription id
I'd recommend following this guide for accepting payment for subscription: https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements
In your Subscription Creation, you should expand the payment intent of latest invoice with expand: ['latest_invoice.payment_intent'] and retrieve subscription.latest_invoice.payment_intent.client_secret to pass to your frontend as client secret.