Skip to content

fix(paypal): handle octet-stream NVP responses#20641

Open
StaberindeZA wants to merge 1 commit into
mainfrom
pay-3709
Open

fix(paypal): handle octet-stream NVP responses#20641
StaberindeZA wants to merge 1 commit into
mainfrom
pay-3709

Conversation

@StaberindeZA
Copy link
Copy Markdown
Contributor

Because

  • PayPal's NVP endpoint intermittently returns application/octet-stream instead of text/plain. Superagent does not buffer unknown content types by default, leaving result.text and result.body empty and silently breaking every downstream NVP parse.

This pull request

  • Adds .buffer(true) so superagent always reads the response body.
  • Reads from result.text when populated, falling back to decoding result.body as utf-8 when it arrives as a Buffer.

Issue that this pull request solves

Closes: PAY-3709

Checklist

Put an x in the boxes that apply

  • My commit is GPG signed.
  • If applicable, I have modified or added tests which pass locally.
  • I have added necessary documentation (if appropriate).
  • I have verified that my changes render correctly in RTL (if appropriate).
  • I have manually reviewed all AI generated code.

How to review (Optional)

  • Key files/areas to focus on:
  • Suggested review order:
  • Risky or complex parts:

Screenshots (Optional)

Please attach the screenshots of the changes made in case of change in user interface.

Other information (Optional)

Any other information that is important to this pull request.

Copilot AI review requested due to automatic review settings May 22, 2026 18:11
@StaberindeZA StaberindeZA requested a review from a team as a code owner May 22, 2026 18:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the PayPal NVP client against intermittent application/octet-stream responses by forcing Superagent to buffer responses and by decoding the raw response from either result.text or a UTF-8 stringified Buffer.

Changes:

  • Force Superagent to always buffer the response body via .buffer(true).
  • Parse NVP responses from result.text, falling back to decoding result.body as UTF-8 when it’s a Buffer.
  • Reuse the computed raw payload string for downstream error construction.
Comments suppressed due to low confidence (1)

libs/payments/paypal/src/lib/paypal.client.ts:130

  • raw uses nullish coalescing (result.text ?? ...). If result.text is an empty string (which is plausible in the reported failure mode), this will select '' and skip decoding result.body even when it contains the response bytes, leading to parsing an empty payload. Consider treating empty/whitespace result.text as absent (e.g., check result.text?.length > 0) before falling back to result.body.
    const raw =
      result.text ??
      (Buffer.isBuffer(result.body) ? result.body.toString('utf8') : '');

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +109 to 114
// PayPal's NVP endpoint sometimes returns application/octet-stream
// instead of text/plain. .buffer(true) ensures superagent reads
// the body regardless of Content-Type (as a Buffer on result.body
// for non-text types).
.buffer(true)
.send(payload),
Copy link
Copy Markdown
Contributor

@david1alvarez david1alvarez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+wc, copilot's suggestion to add a unit test would be great to implement

Because:

* PayPal's NVP endpoint intermittently returns application/octet-stream
  instead of text/plain. Superagent does not buffer unknown content
  types by default, leaving result.text and result.body empty and
  silently breaking every downstream NVP parse.

This commit:

* Adds .buffer(true) so superagent always reads the response body.
* Reads from result.text when populated, falling back to decoding
  result.body as utf-8 when it arrives as a Buffer.

Closes #PAY-3709
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants