fix(paypal): handle octet-stream NVP responses#20641
Open
StaberindeZA wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
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 decodingresult.bodyas UTF-8 when it’s aBuffer. - 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
rawuses nullish coalescing (result.text ?? ...). Ifresult.textis an empty string (which is plausible in the reported failure mode), this will select''and skip decodingresult.bodyeven when it contains the response bytes, leading to parsing an empty payload. Consider treating empty/whitespaceresult.textas absent (e.g., checkresult.text?.length > 0) before falling back toresult.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), |
david1alvarez
approved these changes
May 22, 2026
Contributor
david1alvarez
left a comment
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Because
This pull request
Issue that this pull request solves
Closes: PAY-3709
Checklist
Put an
xin the boxes that applyHow to review (Optional)
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.