/** * PayPal Server SDKLib * * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ). */ import { ApiResponse, RequestOptions } from '../core.js'; import { ConfirmOrderRequest, confirmOrderRequestSchema, } from '../models/confirmOrderRequest.js'; import { Order, orderSchema } from '../models/order.js'; import { OrderAuthorizeRequest, orderAuthorizeRequestSchema, } from '../models/orderAuthorizeRequest.js'; import { OrderAuthorizeResponse, orderAuthorizeResponseSchema, } from '../models/orderAuthorizeResponse.js'; import { OrderCaptureRequest, orderCaptureRequestSchema, } from '../models/orderCaptureRequest.js'; import { OrderRequest, orderRequestSchema } from '../models/orderRequest.js'; import { OrderTrackerRequest, orderTrackerRequestSchema, } from '../models/orderTrackerRequest.js'; import { Patch, patchSchema } from '../models/patch.js'; import { array, optional, string } from '../schema.js'; import { BaseController } from './baseController.js'; import { CustomError } from '../errors/customError.js'; export class OrdersController extends BaseController { /** * Creates an order. Merchants and partners can add Level 2 and 3 data to payments to reduce risk and * payment processing costs. For more information about processing payments, see checkout or multiparty * checkout. Note: For error handling and troubleshooting, see Orders v2 errors. * * @param body * @param paypalMockResponse PayPal's REST API uses a request header to invoke * negative testing in the sandbox. This header * configures the sandbox into a negative testing state * for transactions that include the merchant. * @param paypalRequestId The server stores keys for 6 hours. The API callers * can request the times to up to 72 hours by speaking to * their Account Manager. It is mandatory for all single- * step create order calls (E.g. Create Order Request * with payment source information like Card, PayPal. * vault_id, PayPal.billing_agreement_id, etc). * @param paypalPartnerAttributionId * @param paypalClientMetadataId * @param prefer The preferred server response upon successful * completion of the request. Value is: return=minimal. * The server returns a minimal response to optimize * communication between the API caller and the server. A * minimal response includes the id, status and HATEOAS * links. return=representation. The server returns a * complete resource representation, including the * current state of the resource. * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) assertion * that identifies the merchant. For details, see PayPal- * Auth-Assertion. * @return Response from the API call */ async createOrder( { body, paypalMockResponse, paypalRequestId, paypalPartnerAttributionId, paypalClientMetadataId, prefer, paypalAuthAssertion, }: { body: OrderRequest; paypalMockResponse?: string; paypalRequestId?: string; paypalPartnerAttributionId?: string; paypalClientMetadataId?: string; prefer?: string; paypalAuthAssertion?: string; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('POST', '/v2/checkout/orders'); const mapped = req.prepareArgs({ body: [body, orderRequestSchema], paypalMockResponse: [paypalMockResponse, optional(string())], paypalRequestId: [paypalRequestId, optional(string())], paypalPartnerAttributionId: [ paypalPartnerAttributionId, optional(string()), ], paypalClientMetadataId: [paypalClientMetadataId, optional(string())], prefer: [prefer, optional(string())], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], }); req.header('Content-Type', 'application/json'); req.header('PayPal-Mock-Response', mapped.paypalMockResponse); req.header('PayPal-Request-Id', mapped.paypalRequestId); req.header( 'PayPal-Partner-Attribution-Id', mapped.paypalPartnerAttributionId ); req.header('PayPal-Client-Metadata-Id', mapped.paypalClientMetadataId); req.header('Prefer', mapped.prefer); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.json(mapped.body); req.throwOn( 400, CustomError, 'Request is not well-formed, syntactically incorrect, or violates schema.' ); req.throwOn( 401, CustomError, 'Authentication failed due to missing authorization header, or invalid authentication credentials.' ); req.throwOn( 422, CustomError, 'The requested action could not be performed, semantically incorrect, or failed business validation.' ); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.callAsJson(orderSchema, requestOptions); } /** * Shows details for an order, by ID. Note: For error handling and troubleshooting, see Orders v2 * errors. * * @param id The ID of the order for which to show details. * @param paypalMockResponse PayPal's REST API uses a request header to invoke negative testing in the * sandbox. This header configures the sandbox into a negative testing state * for transactions that include the merchant. * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) assertion that identifies the * merchant. For details, see PayPal-Auth-Assertion. * @param fields A comma-separated list of fields that should be returned for the order. * Valid filter field is `payment_source`. * @return Response from the API call */ async getOrder( { id, paypalMockResponse, paypalAuthAssertion, fields, }: { id: string; paypalMockResponse?: string; paypalAuthAssertion?: string; fields?: string; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('GET'); const mapped = req.prepareArgs({ id: [id, string()], paypalMockResponse: [paypalMockResponse, optional(string())], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], fields: [fields, optional(string())], }); req.header('PayPal-Mock-Response', mapped.paypalMockResponse); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.query('fields', mapped.fields); req.appendTemplatePath`/v2/checkout/orders/${mapped.id}`; req.throwOn( 401, CustomError, 'Authentication failed due to missing authorization header, or invalid authentication credentials.' ); req.throwOn(404, CustomError, 'The specified resource does not exist.'); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.callAsJson(orderSchema, requestOptions); } /** * Updates an order with a `CREATED` or `APPROVED` status. You cannot update an order with the * `COMPLETED` status.

To make an update, you must provide a `reference_id`. If you omit this * value with an order that contains only one purchase unit, PayPal sets the value to `default` which * enables you to use the path: \"/purchase_units/@reference_id=='default'/{attribute-or- * object}\". Merchants and partners can add Level 2 and 3 data to payments to reduce risk and * payment processing costs. For more information about processing payments, see checkout or multiparty checkout. *
Note: For error handling and troubleshooting, see Orders v2 errors. *
Patchable attributes or objects: *

AttributeOpNotes
inte * ntreplace
payerreplace, * addUsing replace op for payer will replace the whole payer object * with the value sent in request.
purchase_unitsreplace, * add
purchase_units[].custom_idreplace, add, * remove
purchase_units[].descriptionreplace, add, * remove
purchase_units[].payee. * emailreplace
purchase_units[].shipping. * namereplace, add
purchase_units[].shipping. * email_addressreplace, add
purchase_units[].shipping. * phone_numberreplace, add
purchase_units[].shipping. * optionsreplace, add
purchase_units[].shipping. * addressreplace, add
purchase_units[].shipping. * typereplace, add
purchase_units[]. * soft_descriptorreplace, remove
purchase_units[]. * amountreplace
purchase_units[]. * itemsreplace, add, remove
purchase_units[]. * invoice_idreplace, add, remove
purchase_units[]. * payment_instructionreplace
purchase_units[]. * payment_instruction.disbursement_modereplaceBy default, * disbursement_mode is INSTANT.
purchase_units[]. * payment_instruction.payee_receivable_fx_rate_idreplace, add, * remove
purchase_units[].payment_instruction. * platform_feesreplace, add, remove
purchase_units[]. * supplementary_data.airlinereplace, add, * remove
purchase_units[].supplementary_data. * cardreplace, add, remove
application_context. * client_configurationreplace, add
* * @param id The ID of the order to update. * @param paypalMockResponse PayPal's REST API uses a request header to invoke negative testing in the * sandbox. This header configures the sandbox into a negative testing state * for transactions that include the merchant. * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) assertion that identifies the * merchant. For details, see PayPal-Auth-Assertion. * @param body * @return Response from the API call */ async patchOrder( { id, paypalMockResponse, paypalAuthAssertion, body, }: { id: string; paypalMockResponse?: string; paypalAuthAssertion?: string; body?: Patch[]; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('PATCH'); const mapped = req.prepareArgs({ id: [id, string()], paypalMockResponse: [paypalMockResponse, optional(string())], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], body: [body, optional(array(patchSchema))], }); req.header('Content-Type', 'application/json'); req.header('PayPal-Mock-Response', mapped.paypalMockResponse); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.json(mapped.body); req.appendTemplatePath`/v2/checkout/orders/${mapped.id}`; req.throwOn( 400, CustomError, 'Request is not well-formed, syntactically incorrect, or violates schema.' ); req.throwOn( 401, CustomError, 'Authentication failed due to missing authorization header, or invalid authentication credentials.' ); req.throwOn(404, CustomError, 'The specified resource does not exist.'); req.throwOn( 422, CustomError, 'The requested action could not be performed, semantically incorrect, or failed business validation.' ); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.call(requestOptions); } /** * Payer confirms their intent to pay for the the Order with the given payment source. * * @param id The ID of the order for which the payer confirms * their intent to pay. * @param paypalClientMetadataId * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) * assertion that identifies the merchant. For details, * see PayPal-Auth-Assertion. * @param prefer The preferred server response upon successful * completion of the request. Value is: return=minimal. * The server returns a minimal response to optimize * communication between the API caller and the server. * A minimal response includes the id, status and * HATEOAS links. return=representation. The server * returns a complete resource representation, * including the current state of the resource. * @param body * @return Response from the API call */ async confirmOrder( { id, paypalClientMetadataId, paypalAuthAssertion, prefer, body, }: { id: string; paypalClientMetadataId?: string; paypalAuthAssertion?: string; prefer?: string; body?: ConfirmOrderRequest; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('POST'); const mapped = req.prepareArgs({ id: [id, string()], paypalClientMetadataId: [paypalClientMetadataId, optional(string())], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], prefer: [prefer, optional(string())], body: [body, optional(confirmOrderRequestSchema)], }); req.header('Content-Type', 'application/json'); req.header('PayPal-Client-Metadata-Id', mapped.paypalClientMetadataId); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.header('Prefer', mapped.prefer); req.json(mapped.body); req.appendTemplatePath`/v2/checkout/orders/${mapped.id}/confirm-payment-source`; req.throwOn( 400, CustomError, 'Request is not well-formed, syntactically incorrect, or violates schema.' ); req.throwOn( 403, CustomError, 'Authorization failed due to insufficient permissions.' ); req.throwOn( 422, CustomError, 'The requested action could not be performed, semantically incorrect, or failed business validation.' ); req.throwOn(500, CustomError, 'An internal server error has occurred.'); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.callAsJson(orderSchema, requestOptions); } /** * Authorizes payment for an order. To successfully authorize payment for an order, the buyer must * first approve the order or a valid payment_source must be provided in the request. A buyer can * approve the order upon being redirected to the rel:approve URL that was returned in the HATEOAS * links in the create order response. Note: For error handling and troubleshooting, see Orders v2 * errors. * * @param id The ID of the order for which to authorize. * @param paypalMockResponse PayPal's REST API uses a request header to * invoke negative testing in the sandbox. This * header configures the sandbox into a negative * testing state for transactions that include the * merchant. * @param paypalRequestId The server stores keys for 6 hours. The API * callers can request the times to up to 72 hours * by speaking to their Account Manager. It is * mandatory for all single-step create order calls * (E.g. Create Order Request with payment source * information like Card, PayPal.vault_id, PayPal. * billing_agreement_id, etc). * @param prefer The preferred server response upon successful * completion of the request. Value is: * return=minimal. The server returns a minimal * response to optimize communication between the * API caller and the server. A minimal response * includes the id, status and HATEOAS links. * return=representation. The server returns a * complete resource representation, including the * current state of the resource. * @param paypalClientMetadataId * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) * assertion that identifies the merchant. For * details, see PayPal-Auth-Assertion. * @param body * @return Response from the API call */ async authorizeOrder( { id, paypalMockResponse, paypalRequestId, prefer, paypalClientMetadataId, paypalAuthAssertion, body, }: { id: string; paypalMockResponse?: string; paypalRequestId?: string; prefer?: string; paypalClientMetadataId?: string; paypalAuthAssertion?: string; body?: OrderAuthorizeRequest; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('POST'); const mapped = req.prepareArgs({ id: [id, string()], paypalMockResponse: [paypalMockResponse, optional(string())], paypalRequestId: [paypalRequestId, optional(string())], prefer: [prefer, optional(string())], paypalClientMetadataId: [paypalClientMetadataId, optional(string())], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], body: [body, optional(orderAuthorizeRequestSchema)], }); req.header('Content-Type', 'application/json'); req.header('PayPal-Mock-Response', mapped.paypalMockResponse); req.header('PayPal-Request-Id', mapped.paypalRequestId); req.header('Prefer', mapped.prefer); req.header('PayPal-Client-Metadata-Id', mapped.paypalClientMetadataId); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.json(mapped.body); req.appendTemplatePath`/v2/checkout/orders/${mapped.id}/authorize`; req.throwOn( 400, CustomError, 'Request is not well-formed, syntactically incorrect, or violates schema.' ); req.throwOn( 401, CustomError, 'Authentication failed due to missing authorization header, or invalid authentication credentials.' ); req.throwOn( 403, CustomError, 'The authorized payment failed due to insufficient permissions.' ); req.throwOn(404, CustomError, 'The specified resource does not exist.'); req.throwOn( 422, CustomError, 'The requested action could not be performed, semantically incorrect, or failed business validation.' ); req.throwOn(500, CustomError, 'An internal server error has occurred.'); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.callAsJson(orderAuthorizeResponseSchema, requestOptions); } /** * Captures payment for an order. To successfully capture payment for an order, the buyer must first * approve the order or a valid payment_source must be provided in the request. A buyer can approve the * order upon being redirected to the rel:approve URL that was returned in the HATEOAS links in the * create order response. Note: For error handling and troubleshooting, see Orders v2 errors. * * @param id The ID of the order for which to capture a payment. * @param paypalMockResponse PayPal's REST API uses a request header to invoke * negative testing in the sandbox. This header * configures the sandbox into a negative testing * state for transactions that include the merchant. * @param paypalRequestId The server stores keys for 6 hours. The API * callers can request the times to up to 72 hours by * speaking to their Account Manager. It is mandatory * for all single-step create order calls (E.g. Create * Order Request with payment source information like * Card, PayPal.vault_id, PayPal.billing_agreement_id, * etc). * @param prefer The preferred server response upon successful * completion of the request. Value is: return=minimal. * The server returns a minimal response to optimize * communication between the API caller and the server. * A minimal response includes the id, status and * HATEOAS links. return=representation. The server * returns a complete resource representation, * including the current state of the resource. * @param paypalClientMetadataId * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) * assertion that identifies the merchant. For details, * see PayPal-Auth-Assertion. * @param body * @return Response from the API call */ async captureOrder( { id, paypalMockResponse, paypalRequestId, prefer, paypalClientMetadataId, paypalAuthAssertion, body, }: { id: string; paypalMockResponse?: string; paypalRequestId?: string; prefer?: string; paypalClientMetadataId?: string; paypalAuthAssertion?: string; body?: OrderCaptureRequest; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('POST'); const mapped = req.prepareArgs({ id: [id, string()], paypalMockResponse: [paypalMockResponse, optional(string())], paypalRequestId: [paypalRequestId, optional(string())], prefer: [prefer, optional(string())], paypalClientMetadataId: [paypalClientMetadataId, optional(string())], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], body: [body, optional(orderCaptureRequestSchema)], }); req.header('Content-Type', 'application/json'); req.header('PayPal-Mock-Response', mapped.paypalMockResponse); req.header('PayPal-Request-Id', mapped.paypalRequestId); req.header('Prefer', mapped.prefer); req.header('PayPal-Client-Metadata-Id', mapped.paypalClientMetadataId); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.json(mapped.body); req.appendTemplatePath`/v2/checkout/orders/${mapped.id}/capture`; req.throwOn( 400, CustomError, 'Request is not well-formed, syntactically incorrect, or violates schema.' ); req.throwOn( 401, CustomError, 'Authentication failed due to missing authorization header, or invalid authentication credentials.' ); req.throwOn( 403, CustomError, 'The authorized payment failed due to insufficient permissions.' ); req.throwOn(404, CustomError, 'The specified resource does not exist.'); req.throwOn( 422, CustomError, 'The requested action could not be performed, semantically incorrect, or failed business validation.' ); req.throwOn(500, CustomError, 'An internal server error has occurred.'); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.callAsJson(orderSchema, requestOptions); } /** * Adds tracking information for an Order. * * @param id The ID of the order that the tracking information is * associated with. * @param body * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) assertion * that identifies the merchant. For details, see PayPal- * Auth-Assertion. * @return Response from the API call */ async createOrderTracking( { id, body, paypalAuthAssertion, }: { id: string; body: OrderTrackerRequest; paypalAuthAssertion?: string; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('POST'); const mapped = req.prepareArgs({ id: [id, string()], body: [body, orderTrackerRequestSchema], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], }); req.header('Content-Type', 'application/json'); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.json(mapped.body); req.appendTemplatePath`/v2/checkout/orders/${mapped.id}/track`; req.throwOn( 400, CustomError, 'Request is not well-formed, syntactically incorrect, or violates schema.' ); req.throwOn( 403, CustomError, 'Authorization failed due to insufficient permissions.' ); req.throwOn(404, CustomError, 'The specified resource does not exist.'); req.throwOn( 422, CustomError, 'The requested action could not be performed, semantically incorrect, or failed business validation.' ); req.throwOn(500, CustomError, 'An internal server error has occurred.'); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.callAsJson(orderSchema, requestOptions); } /** * Updates or cancels the tracking information for a PayPal order, by ID. Updatable attributes or * objects: Attribute Op Notes items replace Using replace op for items will replace the entire items * object with the value sent in request. notify_payer replace, add status replace Only patching status * to CANCELLED is currently supported. * * @param id The ID of the order that the tracking information is associated with. * @param trackerId The order tracking ID. * @param paypalAuthAssertion An API-caller-provided JSON Web Token (JWT) assertion that identifies the * merchant. For details, see PayPal-Auth-Assertion. * @param body * @return Response from the API call */ async updateOrderTracking( { id, trackerId, paypalAuthAssertion, body, }: { id: string; trackerId: string; paypalAuthAssertion?: string; body?: Patch[]; }, requestOptions?: RequestOptions ): Promise> { const req = this.createRequest('PATCH'); const mapped = req.prepareArgs({ id: [id, string()], trackerId: [trackerId, string()], paypalAuthAssertion: [paypalAuthAssertion, optional(string())], body: [body, optional(array(patchSchema))], }); req.header('Content-Type', 'application/json'); req.header('PayPal-Auth-Assertion', mapped.paypalAuthAssertion); req.json(mapped.body); req.appendTemplatePath`/v2/checkout/orders/${mapped.id}/trackers/${mapped.trackerId}`; req.throwOn( 400, CustomError, 'Request is not well-formed, syntactically incorrect, or violates schema.' ); req.throwOn( 403, CustomError, 'Authorization failed due to insufficient permissions.' ); req.throwOn(404, CustomError, 'The specified resource does not exist.'); req.throwOn( 422, CustomError, 'The requested action could not be performed, semantically incorrect, or failed business validation.' ); req.throwOn(500, CustomError, 'An internal server error has occurred.'); req.defaultToError(CustomError, 'The error response.'); req.authenticate([{ oauth2: true }]); return req.call(requestOptions); } }