Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for partial refunds Gravity Forms #119

Closed
remcotolsma opened this issue Jun 7, 2021 · 3 comments
Closed

Support for partial refunds Gravity Forms #119

remcotolsma opened this issue Jun 7, 2021 · 3 comments
Assignees
Labels
enhancement plugin: gravityforms Label for Gravity Forms related content. psp: buckaroo Label for Buckaroo related content.
Milestone

Comments

@remcotolsma
Copy link
Member

After pronamic/wp-pay-core@7af53e7 it is possible to keep track of the refunded amount per payment.

Gravity Forms can also keep track of refunds by adding transaction info in a gf_addon_payment_transaction table.

The gf_addon_payment_transaction table has no unique constrains, so addons have to make sure transactions are not added twice. Within the PayPal add-on, I think they assume that the PayPal only calls the IPN URL once.

My colleague @rvdsteege had the idea to also keep track of the 'refunded amount' in the Gravity Forms entry. The Gravity Forms refund_payment function should then only be called if the Gravity Forms entry 'refunded amount' is bigger than the Pronamic Payment 'refunded amount'.

Downside of this approach is that we can't include the payment provider his transactie ID in the refund_payment call.
https://github.com/wp-premium/gravityforms/blob/2.4.20/includes/addon/class-gf-payment-addon.php#L1908-L1964

@remcotolsma remcotolsma added this to the 6.8 milestone Jun 7, 2021
@remcotolsma remcotolsma self-assigned this Jun 7, 2021
@remcotolsma
Copy link
Member Author

Gravity Forms stores the payment_amount in the wp_gf_entry table. We can also store extra data for entries in the wp_gf_entry_meta table.

We can store the refunded amount in, for example, the meta field refunded_amount. Then we can check for that in the pronamic_payment_status_update_{$slug} action:

$entry_id = $payment->get_source_id();

$entry = \GFAPI::get_entry( $entry_id );

$refunded_amount = $payment->get_refunded_amount()->get_value();

$entry_refunded_amount = \gform_get_meta( $entry_id, 'pronamic_pay_refunded_amount' );

if ( $entry_refunded_amount < $refunded_amount ) {
	$result = $this->addon->refund_payment(
		$entry,
		array(
			// The Gravity Forms payment add-on callback feature uses the action ID to prevent processing an action twice.
			'id'             => '',
			'type'           => 'refund_payment',
			// Unfortunately we don't have a specific transaction ID for this refund at this point.
			'transaction_id' => '',
			'entry_id'       => $entry_id,
			'amount'         => ( $refunded_amount - $entry_refunded_amount ),
		)
	);

	if ( true === $result ) {
		\gform_update_meta( $entry_id, 'pronamic_pay_refunded_amount', $refunded_amount );
	}
}

The gf_addon_payment_transaction table has no unique constrains, so addons have to make sure transactions are not added twice. Within the PayPal add-on, I think they assume that the PayPal only calls the IPN URL once.

This assumption does not seem correct, Gravity Forms can track the callbacks (eg IPN requests) in the gf_addon_payment_callback table. There is a is_duplicate_callback function to make sure callbacks are not processed twice.

https://github.com/wp-premium/gravityforms/blob/a9c8f2de051e016e096069210ecd5fb8f5a19801/includes/addon/class-gf-payment-addon.php#L1630-L1632

I don't think we can use this because we don't work with the Gravity Forms payment callback functionality.

@remcotolsma
Copy link
Member Author

remcotolsma commented Jun 9, 2021

Work in progress: https://youtu.be/E2vHO-ZuHvc, also see:

@remcotolsma remcotolsma added psp: buckaroo Label for Buckaroo related content. plugin: gravityforms Label for Gravity Forms related content. labels Jun 9, 2021
@remcotolsma
Copy link
Member Author

This is done, it requires some testing, but for now good to go. We have two other Buckaroo issues open: #120 and #140.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement plugin: gravityforms Label for Gravity Forms related content. psp: buckaroo Label for Buckaroo related content.
Projects
None yet
Development

No branches or pull requests

1 participant