BWH Payments

BWH Payments

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

PayPal

Take PayPal wallet payments through PayPal Checkout (Orders v2). The shopper approves the payment on PayPal's own page. BWH Payments then captures it, and the capture is where the money is taken.

PayPal works differently from a card gateway in two ways:

  • Approval is not payment. An approved order holds no money until BWH Payments captures it.
  • A completed order is not always paid. PayPal completes an order as soon as it is captured, even while the capture waits for an eCheck or a review. BWH Payments reads the capture, not the order.

Before you start

  • A PayPal Business account
  • A REST app in the PayPal Developer Dashboard, with its Client ID and Secret. Sandbox and Live apps have separate credentials.
  • A currency that PayPal settles in: AUD, CAD, CHF, CZK, DKK, EUR, GBP, HKD, HUF, ILS, JPY, MXN, MYR, NOK, NZD, PHP, PLN, SEK, SGD, THB, TWD or USD. BWH Payments refuses any other currency before it calls PayPal. SAR, AED, KWD, BHD, QAR and INR are not on the list.

Set up PayPal

1. Add the webhook in PayPal. In your REST app, go to Webhooks → Add Webhook:

  • URL: https://your.site/api/v2/method/bwh_payments.bwh_payments.webhook.handle?gateway=PayPal
  • Events: CHECKOUT.ORDER.APPROVED, PAYMENT.CAPTURE.COMPLETED and PAYMENT.CAPTURE.DENIED, and for refunds PAYMENT.CAPTURE.REFUNDED, PAYMENT.REFUND.PENDING and PAYMENT.REFUND.FAILED

Copy the webhook's Webhook ID.

2. Fill in PayPal Gateway Settings

Field Value
Mode Sandbox or Live. It selects the PayPal API that the credentials go to. Sandbox credentials don't work in Live mode.
Client ID Your REST app's client id
Client Secret Your REST app's secret
Currency Used when a payment has no currency
Webhook ID The Webhook ID from step 1. It isn't a secret, but every delivery is verified with it.
Brand Name Optional. The name the shopper sees on PayPal's approval page.
Success URL Your confirmation page. ?reference_id=… is added to it. Required, unless every payment sets redirect_to.
Cancelled URL Your payment-failed page, for a shopper who leaves the approval page. ?reference_id=… is added to it. Required, unless every payment sets redirect_to.
Failure URL PayPal has no failure redirect. BWH Payments stores this URL on the request, so your app has a page to send the shopper to.

3. Create a Payment Gateway Profile named PayPal, set Gateway Settings to PayPal Gateway Settings, and tick Enabled.

How checkout works

  1. create_payment creates a PayPal order with the intent CAPTURE. The order id is the request's order_ref, and PayPal's approval link is its order_url. The request name goes to PayPal as custom_id, so a PayPal statement reconciles to the request.
  2. You redirect the shopper to order_url. They sign in to PayPal and approve the payment.
  3. BWH Payments captures the approved order the next time it checks the status. Two things start that check:
    • the shopper's return, through return_from_gateway (with redirect_to) or your own call to sync_status()
    • the CHECKOUT.ORDER.APPROVED webhook, which queues a status sync for the request

PayPal sends CHECKOUT.ORDER.APPROVED. BWH Payments asks PayPal to verify the delivery, queues a status sync, captures the approved order, reads the capture as COMPLETED, and marks the request Paid.

The capture runs only for a Pending request that BWH Payments created. A webhook for an order that isn't yours captures nothing, and a request that has left Pending never asks PayPal again.

The return and the webhook can try to capture at the same moment. PayPal answers the second capture with ORDER_ALREADY_CAPTURED, and BWH Payments reads that as the capture already done. Each capture also sends the order id as its PayPal-Request-Id, so a retried capture is never taken twice.

How it behaves

PayPal says Payment status
A capture is COMPLETED, PARTIALLY_REFUNDED or REFUNDED Paid
The capture is DECLINED or FAILED Not Paid
The capture is PENDING (an eCheck, or a PayPal review) Pending
The order is APPROVED Captured, then read again
The order is VOIDED Cancelled
CREATED, SAVED, PAYER_ACTION_REQUIRED, anything else Pending
  • Amounts are sent as a decimal string in major units, for example "49.99". PayPal takes HUF, JPY and TWD in whole units only. An amount with decimals in those currencies is refused, not rounded.
  • Shopper details: BWH Payments sends no payer email, because PayPal would lock the wallet to that email.
  • Webhooks are signed with a certificate. BWH Payments sends each delivery back to PayPal's verify-webhook-signature endpoint with your Webhook ID, and applies it only when PayPal answers SUCCESS. So every delivery costs one PayPal API call.
  • A capture that fails in the webhook job marks the event's Integration Request Failed. If PayPal couldn't answer (a 5xx, a 429 or a dropped connection), Frappe retries the job. A real decline waits for retry_webhook.
  • Refunds go against the payment's capture, which is stored as gateway_transaction_ref. Partial refunds are supported. PayPal's COMPLETED is Processed. A PENDING refund settles from a refund webhook, or from sync_refunds.
  • Refunds raised on the PayPal dashboard are recorded by the refund webhook. PayPal can't list a payment's refunds, so sync_refunds can't find one that has no webhook.
  • Cancelling an open checkout isn't supported. release_if_unpaid() returns False. The approval link stays payable until the order is captured or expires, so you can send the shopper back to order_url.

Good to know

  • Gateway settings are a single document, so a site can hold one PayPal account.
  • An approved order that was never captured can't be refunded here. Capture or void it in the PayPal dashboard.
  • The access token is cached per mode and client id. New credentials never create orders in the old account.

Test mode

Set Mode to Sandbox, and use the sandbox app's credentials. Pay with a personal sandbox account from Testing Tools → Sandbox Accounts in the Developer Dashboard.

A webhook only reaches a public URL. To test on your own machine, expose your site through a tunnel, for example cloudflared tunnel --url http://127.0.0.1:8000 --http-host-header your.site, and use the tunnel's URL in step 1.

Next steps

Last updated 1 month ago
Was this helpful?
Thanks!