PaddleHQ / paddle-node-sdk

Node.js SDK for working with the Paddle API in server-side apps.
https://developer.paddle.com/
Apache License 2.0
28 stars 5 forks source link

[Bug]: getPaymentMethodChangeTransaction throws error because unitPriceOverrides is undefined #18

Closed xgedev closed 4 months ago

xgedev commented 4 months ago

What happened?

Hi! When fetching a Price from the API that does not have unit_price_overrides set, it returns an error and the current operation fails. This happened to me when trying to use paddle.subscriptions.getPaymentMethodChangeTransaction(subId), it returns an error instead of the transaction but when I put a ? before price.unit_price_overrides in entities/price/price.js, it works just fine.

Steps to reproduce

  1. Create a basic product with a price that does not have any overwrites.
  2. Create a subscription.
  3. Init Paddle using new Paddle(apiKey).
  4. Run paddle.subscriptions.getPaymentMethodChangeTransaction(subId).
  5. Observe the error.

What did you expect to happen?

The error should not be thrown. Instead, in /dist/entities/price/price.js price.unit_price_overrides being undefined should be handled properly, for example using ?:

/* ... */
class Price {
    constructor(price) {
        /* ... */
        this.unitPrice = new index_1.Money(price.unit_price);
        this.unitPriceOverrides = price.unit_price_overrides?.map((unit_price_override) => new index_1.UnitPriceOverride(unit_price_override));
        this.quantity = new index_1.PriceQuantity(price.quantity);
        /* ... */
    }
}
exports.Price = Price;

Logs

[Paddle] [LOG] [Request] GET https://sandbox-api.paddle.com/subscriptions/sub_01hs8tbydenhgakmxxxxxxxxxx/update-payment-method-transaction Transaction ID: 6762xxxx-xxxx-xxxx-xxxx-a8e914acxxxx
[Paddle] [LOG] [Response] GET https://sandbox-api.paddle.com/subscriptions/sub_01hs8tbydenhgakmxxxxxxxxxx/update-payment-method-transaction 200 Transaction ID: 6762xxxx-xxxx-xxxx-xxxx-a8e914acxxxx Request ID: 4ba7xxxx-xxxx-xxxx-xxxx-xxxx796ba203
Cannot read properties of undefined (reading 'map')
TypeError: Cannot read properties of undefined (reading 'map')
    at new Price (/home/user/project/node_modules/@paddle/paddle-node-sdk/dist/entities/price/price.js:16:62)
    at new TransactionItem (/home/user/project/node_modules/@paddle/paddle-node-sdk/dist/entities/transaction/transaction-item.js:7:46)
    at /home/user/project/node_modules/@paddle/paddle-node-sdk/dist/entities/transaction/transaction.js:23:54
    at Array.map (<anonymous>)
    at new Transaction (/home/user/project/node_modules/@paddle/paddle-node-sdk/dist/entities/transaction/transaction.js:23:40)
    at SubscriptionsResource.<anonymous> (/home/user/project/node_modules/@paddle/paddle-node-sdk/dist/resources/subscriptions/index.js:146:20)
    at Generator.next (<anonymous>)
    at fulfilled (/home/user/project/node_modules/@paddle/paddle-node-sdk/dist/resources/subscriptions/index.js:19:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
vijayasingam-paddle commented 4 months ago

Hi @xgedev, Thank you for taking the time to raise this issue. It appears to be an inconsistency in our API. I have updated the code so that it doesn't throw an error while using this function while i work with the team to get the API fixed.

Thank you.

xgedev commented 4 months ago

Great, thank you so much for implementing a fix this quickly.