Description
The gform_ppcp_webhook filter can be used to customize the webhook events processed by the Gravity Forms PayPal Checkout Add-On. The following event types are supported by default.
Product and Services:
- PAYMENT.CAPTURE.REFUNDED
- PAYMENT.AUTHORIZATION.VOIDED
- PAYMENT.CAPTURE.COMPLETED
- PAYMENT.CAPTURE.DENIED
Subscriptions:
- PAYMENT.SALE.COMPLETED
- BILLING.SUBSCRIPTION.PAYMENT.FAILED
- BILLING.SUBSCRIPTION.CANCELLED
- BILLING.SUBSCRIPTION.EXPIRED
See the PayPal API Reference for a full list of event types for which webhooks can be sent.
Usage
The hook which would run for all PayPal Checkout feeds can be used like so:
add_filter( 'gform_ppcp_webhook', 'your_function_name', 10, 2 );
Parameters
- $action array
An associative array containing the event details or an empty array for unsupported event types.
array( 'type' => '', 'amount' => '', 'transaction_type' => '', 'transaction_id' => '', 'subscription_id' => '', 'entry_id' => '', 'payment_status' => '', 'note' => '', );
-
$event array
The PayPal Checkout event object for the webhook which was received. See the PayPal API Reference for details about the event properties.
Examples
Add a note
The following shows how you can add a custom note when an authorization is cancelled (voided).
add_filter( 'gform_ppcp_webhook', 'ppcp_webhook_action_note', 10, 2 );
function ppcp_webhook_action_note( $action, $event ) {
if ( rgar( $event, 'event_type' ) === 'PAYMENT.AUTHORIZATION.VOIDED' ) {
$action['note'] = sprintf( 'Authorization has been cancelled (voided). Transaction Id: %s', rgar( $action, 'transaction_id' ) );
}
return $action;
}
Process the PAYMENT.CAPTURE.DECLINED event
add_filter( 'gform_ppcp_webhook', function ( $action, $event ) {
if ( rgar( $event, 'event_type' ) !== 'PAYMENT.CAPTURE.DECLINED' || empty( $action['entry_id'] ) ) {
return $action;
}
$entry = GFAPI::get_entry( $action['entry_id'] );
$payment_status = rgar( $entry, 'payment_status' );
if ( ! in_array( $payment_status, array( 'Authorized', 'Pending' ) ) ) {
return $action;
}
$action['note'] = sprintf( 'Payment declined. Transaction Id: %s', rgar( $action, 'transaction_id' ) );
$action['type'] = 'fail_payment';
$action['amount'] = gf_ppcp()->get_amount_import( rgars( $event, 'resource/amount/value' ), rgar( $entry, 'currency' ) );
return $action;
}, 10, 2 );
Placement
Your code snippet should be placed in the functions.php file of your active theme.
Source Code
$action = apply_filters( 'gform_ppcp_webhook', $action, $event );
This hook is located in GF_PPCP::callback() in class-gf-ppcp.php.