BWH Payments

BWH Payments

Open in ChatGPT
Ask ChatGPT about this page
Open in Claude
Ask Claude about this page

Move from legacy

This page lists what to change when you move an app from the legacy version of BWH Payments to the current one. The legacy version is the code before the service clients refactor of October 2026. Its docs are at BWH Payments (legacy).

You do not need a data patch. The Gateway Payment Request keeps its fields, each Gateway Settings keeps its fields, and the webhook URL stays the same. Old payments stay readable and refundable.

1. Update the app

cd apps/bwh_payments && git pull && cd ../..
bench setup requirements
bench --site <your-site> migrate

The current version needs these Python packages. bench setup requirements installs them:

Package Legacy Current
stripe >=11,<16 >=12.5,<16
pydantic not used >=2,<3
braintree not used >=4.20,<5

migrate adds the Gateway Refund DocType and the settings for the new gateways.

2. Run the background workers

The legacy version applied a webhook in the webhook request. The current version records the delivery, answers the gateway, and applies the event in a background job on the short queue. If no worker runs, the Integration Request stays Queued and the payment stays Pending.

Make sure the site runs its workers (bench start in development, Supervisor or systemd in production). A failed job is kept as a Failed Integration Request. A System Manager can run it again with retry_webhook. See Webhooks and security.

3. Stop calling refund methods from the browser

These methods of the Gateway Payment Request are no longer whitelisted:

Method Legacy Current
refund whitelisted server only
release_if_unpaid whitelisted server only
get_remaining_refundable_amount whitelisted server only

Frappe's run_doc_method checks only read permission, so any reader could refund a payment. If your desk or portal calls them, add your own whitelisted method that checks permission, then call the server API:

import frappe
from bwh_payments.api import refund


@frappe.whitelist(methods=["POST"])
def refund_order(order: str, amount: float | None = None):
	frappe.get_doc("Sales Order", order).check_permission("write")
	payment_request = frappe.db.get_value("Gateway Payment Request", {"ref_docname": order}, "name")
	return refund(payment_request, amount, reference=order).as_dict()

sync_status and the new sync_refunds stay whitelisted.

4. Expect refunds to be Pending

In the legacy version, refund() added the amount to refund_amount as soon as the gateway accepted the call. Now each refund is a Gateway Refund with the status Pending, Processed or Failed. Only a Processed refund adds to refund_amount and moves the request to Partially Refunded or Refunded. A Pending refund still counts against the amount left to refund.

  • payment_request.refund() still returns {"refund_amount", "status", "refund_id"}, but a Pending refund is not in refund_amount yet.
  • Use bwh_payments.api.refund(payment_request, amount=None, reference=None). It returns the Gateway Refund.
  • To act when a refund settles, add on_refund_update(self, refund) to the referenced document. It runs on every status change of a Gateway Refund. refund.reference is the id you passed.
  • Subscribe each gateway to its refund events, so Pending refunds settle. The gateway pages list the events. For a refund whose webhook never arrives, call sync_refunds.

5. Handle on_payment_authorized once

When a request turns Paid, BWH Payments now calls on_payment_authorized("Completed") on the referenced document, once. This is the frappe/payments convention. The legacy version did not call it.

If your app already settles the order after sync_status() or from a scheduled job, do it in one place only. Move the work into on_payment_authorized, or make it safe to run twice.

6. Do not change a request after insert

These fields are now set once. To charge a different amount, currency or gateway, create a new Gateway Payment Request.

  • amount, currency_code, gateway, order_ref
  • status is read only. A new checkout must start as Pending.

To create a request, you can now call bwh_payments.api.create_payment(ref_doctype, ref_docname, amount, currency, gateway, **fields). fields accepts only company, redirect_to and customer_*.

7. Simplify the return page (optional)

The legacy docs had you write a guest endpoint that calls sync_status() from a POST. That still works. You can instead set redirect_to to a path on your site. Every return URL then goes through checkout.return_from_gateway, which confirms the payment with the gateway and redirects the shopper to redirect_to. See Return pages.

sync_status() now asks the gateway only while the request is Pending, because asking can capture money.

8. Update your tests

The legacy test fakes bwh_payments/tests/fake_stripe.py and fake_razorpay.py are gone. Patching make_post_request on a built-in Gateway Settings no longer has an effect: under tests every gateway uses its in-memory stub.

In an app that takes payments, use bwh_payments.testing. It needs no provider:

from bwh_payments import testing as payment_testing

gateway = payment_testing.configure_test_gateway()
payment_testing.pay(payment_request.name)
payment_testing.set_next_refund_status("Pending")

Refunds and webhook logs commit, so enter payment_testing.without_commits() in setUpClass and leave it in tearDownClass. Call payment_testing.reset() in setUp. Test with stubs has a full example.

9. Update your own gateway

Skip this step if you only use the built-in gateways.

A legacy gateway keeps working. BWH Payments reads every result through its model, so a gateway that returns dicts is still accepted, and a handle_webhook that returns {} is read as "ignore". Change these things when you next work on it:

Area Legacy Current
Return types dicts CheckoutSession, RefundResult and WebhookEvent (or None) from bwh_payments.base_class
Ignore a webhook return {} return None
create_session no return_url optional return_url, passed only when set
Refund status ignored succeeded / processed, pending, failed / canceled / cancelled. An empty status means Processed. Any other value stays Pending.
New optional methods none get_transaction_ref, get_refund_status, list_refunds, supported_currencies
Webhook payment event session_id, status, event_id also transaction_ref, and sync_status for a gateway that captures in get_payment_status
Refund webhook event none refund_id, refund_amount, transaction_ref
Class attributes none logo, logo_dark, description, docs_url, reusable_checkout
Transport and tests patch the transport a base / live / stub service client

Map your gateway's refund statuses to the values above. A status such as COMPLETED that is not in the list keeps the refund Pending until a webhook or get_refund_status settles it.

See Gateway contract and Service clients.

Checklist

  • App updated, requirements installed, site migrated
  • Background workers run on the short queue
  • No browser call to refund, release_if_unpaid or get_remaining_refundable_amount
  • Refund code handles Pending, and on_refund_update is in place
  • Refund events subscribed, after legacy refunds have settled
  • Order settles once, in on_payment_authorized
  • No code changes amount, currency_code, gateway or status after insert
  • Tests use bwh_payments.testing
Last updated 1 month ago
Was this helpful?
Thanks!