google-apps-scriptxero-api

Is there a way of adding an Online Payment option to an invoice via the Xero API?


I'm working on an invoicing automation in Google Apps Script to generate an invoice in Xero based on details that are taken from an active document.

When creating an invoice in the Xero UI, there is an option to add an 'Online Payment' - which allows you to select payment methods such as Stripe. Related Image below.

enter image description here

Is there a way of setting this via the API? My current request is below.

function runInvoiceCreator(data) {
  
  var service = getService();

  if (service.hasAccess()) {
    // Retrieve the tenantId from storage.
    var tenantId = service.getStorage().getValue('tenantId');

    var invoiceData = {
      "Type": "ACCREC",
      "BrandingThemeID": data.branding,
      "Contact": {
        "Name": data.clientName
      },
      "Date": new Date(data.issueDate).toISOString().slice(0, 10),
      "DueDate": new Date(data.dueDate).toISOString().slice(0, 10),
      "Reference": data.clientReference,
      "LineItems": [
        {
          "ItemCode": data.item,
          "Quantity": data.quantity,
          "UnitAmount": data.amount,
        }
      ]
    };

    var apiUrl = 'https://api.xero.com/api.xro/2.0/Invoices';
  
    var options = {
      'method': 'post',
      'headers': {
        'Authorization': 'Bearer ' + accessToken,
        'Content-Type': 'application/json',
        'Xero-tenant-id': tenantId
      },
      'payload': JSON.stringify(invoiceData),
      'muteHttpExceptions': true
    };

    var response = UrlFetchApp.fetch(apiUrl, options);
    Logger.log('Response: ' + response.getContentText());

    
  } else {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s',
        authorizationUrl);
  }
}

Solution

  • Generally payment services need to be set up in Xero unless you are a payment service provider yourself.

    You can then choose the branding theme that includes the particular payment service link you want to send to the customer by including the branding theme id in the invoice creation request.

    Xero Central article : Set up payment services

    Xero Developer Centre: BrandingThemes