square / square-nodejs-sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

paymentsApi::createPayment thrown exception

ThinhVu opened this issue · comments

paymentsApi::createPayment returned response body as a plain JS object.
But callAsJson expect a JSON string. That's why this bug occured.

At paymentsApi.ts:

  async createPayment(
    body: CreatePaymentRequest,
    requestOptions?: RequestOptions
  ): Promise<ApiResponse<CreatePaymentResponse>> {
    const req = this.createRequest('POST', '/v2/payments');
    const mapped = req.prepareArgs({
      body: [body, createPaymentRequestSchema],
    });
    req.json(mapped.body);
    return req.callAsJson(createPaymentResponseSchema, requestOptions);
  }

@apimatic/core/lib/http/requestBuilder.js

DefaultRequestBuilder.prototype.callAsJson = function (schema, requestOptions) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var result, parsed, mappingResult;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        ...
                    case 1:
                        result = _a.sent();
                        if (result.body === '') {
                            throw new Error('Could not parse body as JSON. The response body is empty.');
                        }
                        console.log('body result', typeof result.body, result.body) // my injected log
                        if (typeof result.body !== 'string') {
                            throw new Error('Could not parse body as JSON. The response body is not a string.');
                        }

Output:

body result object {
  payment: {
    id: 'F4d3Ft65d5mTlu1kFj4lB6eEb1fZY',
    ...
  }
}

Error: Could not parse body as JSON. The response body is not a string.
    at DefaultRequestBuilder.<anonymous> (/project/node_modules/@apimatic/core/lib/http/requestBuilder.js:293:35)
    at step (/project/node_modules/tslib/tslib.js:143:27)
    at Object.next (/project/node_modules/tslib/tslib.js:124:57)
    at fulfilled (/project/node_modules/tslib/tslib.js:114:62)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

hi @ThinhVu,

The latest version of our SDK (25.2.0) seems to have this issue fixed

In the future we suggest posting questions to our Developer Forums as these forums are monitored by Square Employees, and your questions will have a response within 48 hours.

Cheers!