<?xml version="1.0" encoding="utf-8"?>
<!--
	OFX REST connection manager definitions.
	Copyright © 2006-2020 COZYROC LLC. All rights reserved.
-->
<ConnectionManager Name="OFX" RuntimeVersion="2.0" Help="http://www.cozyroc.com/ssis/ofx-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://sandbox.api.ofx.com/v1" />

	<Authentication Name="OAuth 2">
		<Documentation>https://payments.developer.ofx.com/authentication/introduction</Documentation>

		<User>
			<Parameter Name="Scope" Default="payments" />
		</User>

		<Test Url="/payments/beneficiaries?pageSize=1" />

		<Token Url="{{=OAuth2.sign_in_url}}" Result="{{=response.access_token}}">
			<Documentation>https://payments.developer.ofx.com/authentication/authorize</Documentation>

			<Parameters>
				<Parameter Name="response_type" Value="{{=OAuth2.sign_in_response_type}}" />
				<Parameter Name="client_id" Value="{{=OAuth2.client_id}}" />
				<Parameter Name="redirect_uri" Value="{{=OAuth2.redirect_uri}}" />
				<Parameter Name="scope" Value="{{=token.Scope}}" />
			</Parameters>

			<Authorized Id="{{=OAuth2.redirect_uri}}" Result="{{=
				connection.execute({
					url: OAuth2.authorization_url,
					method: OAuth2.authorization_method,
					parameters: {
						code: response.code,
						grant_type: OAuth2.grant_type,
						client_id: OAuth2.client_id,
						client_secret: OAuth2.client_secret,
						redirect_uri: OAuth2.redirect_uri,
						scope: token.Scope
			} })}}">

				<Documentation>https://payments.developer.ofx.com/authentication/accesstoken</Documentation>
			</Authorized>

			<Refresh Url="{{=OAuth2.refresh_token_url}}" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Parameters>
					<Parameter Name="client_id" Value="{{=OAuth2.client_id}}" />
					<Parameter Name="client_secret" Value="{{=OAuth2.client_secret}}" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="scope" Value="{{=token.Scope}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Resources>
		<Template>
			<Field Name="ShortText" DataType="DT_WSTR" Length="255" />
			<Field Name="LongText" DataType="DT_WSTR" Length="1000" />
			<Field Name="DateTime" DataType="DT_DBTIMESTAMP" />
			<Field Name="Date" DataType="DT_DBDATE" />
			<Field Name="Address">
				<Component Name="addressLine" Template="ShortText" />
				<Component Name="citySuburb" Template="ShortText" />
				<Component Name="stateProvince" Template="ShortText" />
				<Component Name="postCode" Template="ShortText" />
				<Component Name="country" Template="ShortText" />
			</Field>

			<Resource Name="Base">
				<Read Result="{{=response}}">
					<Parameters>
						<Parameter Name="page" Value="{{=parameters.iterator}}" />
						<Parameter Name="pageSize" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 0) + 1}}" />
					</Iterator>
				</Read>

				<Create Method="POST">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Create>

				<Update Method="PUT">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Update>

				<Delete Method="DELETE" />
			</Resource>
		</Template>

		<Resource Name="Quote" Template="Base">
			<Fields>
				<Field Name="id" DataType="DT_GUID" IsKey="true" />
				<Field Name="buyCurrency" Template="ShortText" />
				<Field Name="buyAmount" DataType="DT_R8" />
				<Field Name="sellCurrency" Template="ShortText" />
				<Field Name="sellAmount" DataType="DT_R8" />
				<Field Name="customerRate" DataType="DT_R8" />
				<Field Name="inverseCustomerRate" DataType="DT_R8" />
				<Field Name="fee" DataType="DT_R8" />
				<Field Name="feeCurrency" Template="ShortText" />
				<Field Name="freebies">
					<Component Name="total" DataType="DT_I4" />
					<Component Name="claimed" DataType="DT_I4" />
					<Component Name="remaining" DataType="DT_I4" />
				</Field>
				<Field Name="beneficiaries" DataType="DT_I4" />
			</Fields>

			<Create Url="/payments/quotes">
				<Documentation>https://payments.developer.ofx.com/specs/quotes/create-quote</Documentation>
			</Create>

			<Update Url="/payments/quotes/{{=item.id}}">
				<Documentation>https://payments.developer.ofx.com/specs/quotes/update-quote</Documentation>
			</Update>
		</Resource>

		<Resource Name="Beneficiary" Template="Base">
			<Fields>
				<Field Name="beneficiaryId" Template="ShortText" IsKey="true" />
				<Field Name="name" Template="ShortText" />
				<Field Name="currency" Template="ShortText" />
				<Field Name="alias" Template="ShortText" />
				<Field Name="email" Template="ShortText" />
				<Field Name="classification" Template="ShortText" />
				<Field Name="paymentReason" Template="ShortText" />
				<Field Name="beneficiaryBankDetails">
					<Component Name="bankName" Template="ShortText" />
					<Component Name="bankBranchName" Template="ShortText" />
					<Component Name="bankCode" Template="ShortText" />
					<Component Name="branchCode" Template="ShortText" />
					<Component Name="swift" Template="ShortText" />
					<Component Name="account" Template="ShortText" />
					<Component Name="accountSuffix" Template="ShortText" />
					<Component Name="reference" Template="ShortText" />
					<Component Name="bankCorrespondent" Template="ShortText" />
					<Component Name="bankAddress" Template="Address" />
				</Field>
				<Field Name="beneficiaryAddress" Template="Address" />
				<Field Name="benBizNumber" Template="ShortText" />
			</Fields>

			<Read Url="/payments/beneficiaries" Result="{{=response.beneficiaries}}">
				<Documentation>https://payments.developer.ofx.com/specs/beneficiaries/get-beneficiaries</Documentation>
			</Read>

			<Create Url="/payments/beneficiaries">
				<Documentation>https://payments.developer.ofx.com/specs/beneficiaries/create-beneficiary</Documentation>
			</Create>

			<Update Url="/payments/beneficiaries/{{=item.beneficiaryId}}">
				<Documentation>https://payments.developer.ofx.com/specs/beneficiaries/edit-beneficiary</Documentation>
			</Update>

			<Delete Url="/payments/beneficiaries/{{=item.beneficiaryId}}">
				<Documentation>https://payments.developer.ofx.com/specs/beneficiaries/delete-beneficiary</Documentation>
			</Delete>
		</Resource>

		<Resource Name="Deal" Template="Base">
			<Fields>
				<Field Name="dealId" Template="ShortText" IsKey="true" />
				<Field Name="dealType" Template="ShortText" />
				<Field Name="buyCurrency" Template="ShortText" />
				<Field Name="buyAmount" DataType="DT_R8" />
				<Field Name="sellCurrency" Template="ShortText" />
				<Field Name="sellAmount" DataType="DT_R8" />
				<Field Name="rate" DataType="DT_R8" />
				<Field Name="status" Template="ShortText" />
				<Field Name="reference" Template="ShortText" />
				<Field Name="enteredDate" Template="DateTime" />
				<Field Name="maturityDate" Template="DateTime" />
				<Field Name="fee" DataType="DT_R8" />
				<Field Name="feeCurrency" Template="ShortText" />
				<Field Name="BPayNumber" DataType="DT_I4" />
				<Field Name="stateInfo">
					<Component Name="dealState" Template="ShortText" />
					<Component Name="isSettled" DataType="DT_BOOL" />
					<Component Name="isAwaitingFunds" DataType="DT_BOOL" />
					<Component Name="dealStateDisplay" Template="ShortText" />
				</Field>
				<Field Name="dealBeneficiaries" Template="LongText" Composite="true" />
				<Field Name="quoteId" Template="ShortText" />
				<Field Name="fundingSource" Template="ShortText" />
				<Field Name="customer">
					<Component Name="title" Template="ShortText" />
					<Component Name="firstName" Template="ShortText" />
					<Component Name="middleName" Template="ShortText" />
					<Component Name="lastName" Template="ShortText" />
					<Component Name="companyName" Template="ShortText" />
					<Component Name="branchName" Template="ShortText" />
					<Component Name="accountNumber" Template="ShortText" />
					<Component Name="address1" Template="ShortText" />
					<Component Name="city" Template="ShortText" />
					<Component Name="state" Template="ShortText" />
					<Component Name="postCode" Template="ShortText" />
					<Component Name="countryISOCode" Template="ShortText" />
				</Field>
				<Field Name="beneficiaries">
					<Get Url="/payments/deals/{{=item.dealId}}/beneficiaries" Value="{{=response.dealBeneficiaries}}">
						<Documentation>https://payments.developer.ofx.com/specs/deals/get-deal-beneficiaries</Documentation>
					</Get>

					<Component IsArray="true">
						<Component Name="beneficiaryId" Template="ShortText" />
						<Component Name="amount" DataType="DT_R8" />
						<Component Name="payReason" Template="ShortText" />
						<Component Name="payReference" Template="ShortText" />
					</Component>
				</Field>
			</Fields>

			<Read Url="/payments/deals" Result="{{=response.deals}}">
				<Documentation>https://payments.developer.ofx.com/specs/deals/gets-a-list-of-deals-filtered-by-dealstatus-and-dealtype-and-pagination</Documentation>
			</Read>

			<Create Url="/payments/deals">
				<Documentation>https://payments.developer.ofx.com/specs/deals/create-a-deal</Documentation>
			</Create>
		</Resource>

		<Resource Name="User" Template="Base">
			<Fields>
				<Field Name="userId" DataType="DT_GUID" IsKey="true" />
				<Field Name="hasBeneficiary" DataType="DT_BOOL" />
				<Field Name="status" Template="ShortText" />
			</Fields>

			<Read Url="/users">
				<Documentation>https://payments.developer.ofx.com/usersspecs/users/get-a-list-of-all-users</Documentation>
			</Read>
		</Resource>
	</Resources>

	<Script>
		<Module Name="Main">
			<![CDATA[
require("underscore");

		]]>
		</Module>
		<Module Name="OAuth 2">
			<![CDATA[
// See RFC 6749 (https://tools.ietf.org/html/rfc6749) for familiarizing with OAuth2 authorization flows-related terms and concepts 
var OAuth2 = {
	client_id: "n4sWgXDA3B7eJISYC9rgG6jlLGNAVd4d", // required. The Client ID.
	client_secret: "Z3eTtGRKaqht9Qh9", // required. The Client Secret.
	sign_in_url: "/oauth/authorize", // required. The starting endpoint in the authorization server. 
	sign_in_response_type: "code", // optional. The value for the "response_type" parameter in the initiating call. Usually "code", but can also be "token", "id_token".
	authorization_url: "/oauth/token", // required. The endpoint for exchanging authorization code for an access token.
	authorization_method: "POST", // required. Usually the authorization URL is accessed via a POST request.
	redirect_uri: "https://www.cozyroc.com/oauth_callback", // required. The callback URL to be called after autherization. If the default value doesn't meet requirements, need to setup "Token.Authorized.LocalListenUrl".
	grant_type: "authorization_code", // optional. Can also be "client_credentials" or "password" (see https://auth0.com/docs/applications/reference/grant-types-available )
	refresh_token_url: "/oauth/token" // required, if the authentication needs to support renewing access tokens
};
		]]>
		</Module>
	</Script>
</ConnectionManager>