﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.8
@EndOfDesignText@
Sub Class_Globals
	Private fx As JFX
	
	Public access_token As String
	Public terminal_id As String
	
	Private gottoken As Boolean=False
	
	Public usedemo As Boolean = True
	
	Private usingurl As String = "https://demo-api.vivapayments.com/ecr/v1/"
	Private usingurlaccount As String = "https://demo-accounts.vivapayments.com/"
	
	Private state As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
	gottoken=False
End Sub

private Sub DemoOrProduction
	If usedemo=True Then
		usingurl = "https://demo-api.vivapayments.com/ecr/v1/"
		usingurlaccount = "https://demo-accounts.vivapayments.com/"
	Else
		usingurl = "https://api.vivapayments.com/ecr/v1/"
		usingurlaccount = "https://accounts.vivapayments.com/"
	End If
End Sub

public Sub GetAccessToken(Client_ID As String, Client_Secret_Key As String) As ResumableSub
	DemoOrProduction
	Dim amap As Map
	Dim su As StringUtils
	Dim creds As String=Client_ID & ":" & Client_Secret_Key
	access_token=""
	gottoken=False
	
	Private j As HttpJob
	j.Initialize("viva",Me)
	
	j.postString(usingurlaccount & "connect/token","grant_type=client_credentials")
	j.GetRequest.SetHeader("Authorization", "Basic " & su.EncodeBase64(creds.GetBytes("UTF8")))
	j.GetRequest.Setheader("Accept", "application/json")
	J.GetRequest.SetContentType("application/x-www-form-urlencoded")
	
	Wait For (j) JobDone(j As HttpJob)
	
	'Log(j.GetString)
	
	If j.Success=True Then
	If j.GetString.Contains("access_token")=True Then
			Dim gotjson As JSON=j.GetString
			amap.initialize
			amap=gotjson.ToMap
			access_token=amap.Get("access_token") 'Get token to string
			gottoken=True
			Log ("OK, I've got access_token, ready to use it...")
			'Log(access_token)
		j.Release
		Return True
		
	Else
		
		j.Release
		Return False
			
	End If
	Else
		j.Release
		Return False
	End If

	


End Sub

Public Sub SearchLivePOSatSource(SourceID As Int) As ResumableSub
'Returns List...
	
	Dim list1 As List
	list1.Initialize
	
	If gottoken=False Then 
		Log($"Sorry, not having "access_token" to continue at SearchLivePOSatSource, so the list i will return will have 0 size..."$)
		Return list1
	End If
	
	DemoOrProduction
	
	Dim amap As Map
	
	Private j As HttpJob
	j.Initialize("viva",Me)

	Dim m As Map = CreateMap("statusId": 1,"sourceCode": SourceID.As(String))
	Dim json As String = m.As(JSON).ToString
	
	j.postString(usingurl & "devices:search",json) 'sourcecode = select the shop-place
	j.GetRequest.SetHeader("Authorization", "Bearer " & access_token)
	j.GetRequest.Setheader("Accept", "application/json")
	J.GetRequest.SetContentType("application/json")
	Wait For (j) JobDone(j As HttpJob)

	If j.Success=True Then

		Dim gotjson As JSON=j.GetString
		Dim listsources As List=gotjson.ToList
		For k=0 To listsources.Size-1
			amap=listsources.Get(k).As(Map)
			terminal_id=amap.Get("terminalId") 'You can set all values to a list or array.. combo
			list1.Add(terminal_id)
		Next
		
		j.Release
		
		Log ("I will auto set the last list item as default terminal_id")
		Return list1
	
	Else
	
		Log ("No terminals for this SourceID.")
		j.Release
		Return list1
	
	End If
End Sub

public Sub RequestSalesPayment(sessionId As String, terminal_id1 As String, cashregister_id As String, amount As Int, currencyCode As Int, merchantnotes As String, customernotes As String, preauthbool As Boolean,maxInstalments As Int, tipamount As Int) As ResumableSub
	
	If gottoken=False Then
		Log($"Sorry, not having "access_token" to continue at RequestSalesPayment..."$)
		Return False
	End If
	
	Private j As HttpJob
	j.Initialize("viva",Me)
	
	Dim m As Map = CreateMap("sessionId": sessionId, "terminalId": terminal_id1, "cashRegisterId": cashregister_id,"amount": amount,"currencyCode": currencyCode.as(String),"merchantReference": merchantnotes,"customerTrns": customernotes,"preauth": preauthbool,"maxInstalments": maxInstalments,"tipAmount": tipamount)
	Dim json As String = m.As(JSON).ToString

	j.postString(usingurl & "transactions:sale",json) 
	j.GetRequest.SetHeader("Authorization", "Bearer " & access_token)
	J.GetRequest.SetContentType("application/json")
	Wait For (j) JobDone(j As HttpJob)

	If j.Success=True Then
	If j.GetString.Contains("detail")=True Then
		j.release
		Log("This SessionID already used, use other and try again.")
		Return False
		Else
		j.release
		Log("Now you will probably check your POS after a while (because of cloud) - asking for credit/debit card.")
		Return True
	End If
	Else
		j.Release
		Return False
	End If

End Sub



public Sub RequestRefund(sessionId1 As String, refundsessionId As String, terminal_id1 As String, cashregister_id As String, amount As Int, currencyCode As Int, merchantnotes As String, customernotes As String) As ResumableSub

	If gottoken=False Then
		Log($"Sorry, not having "access_token" to continue at RequestRefund..."$)
		Return False
	End If
	
	Private j As HttpJob
	j.Initialize("viva",Me)
	
	Dim m As Map = CreateMap("sessionId": refundsessionId,"terminalId": terminal_id1, "cashRegisterId": cashregister_id,"parentSessionId": sessionId1,"amount": amount,"currencyCode": currencyCode.As(String),"merchantReference": merchantnotes,"customerTrns": customernotes)
	Dim json As String = m.As(JSON).ToString


	j.postString(usingurl & "transactions:refund",json) 
	j.GetRequest.SetHeader("Authorization", "Bearer " & access_token)
	j.GetRequest.Setheader("Accept", "application/json")
	J.GetRequest.SetContentType("application/json")
	Wait For (j) JobDone(j As HttpJob)

	If j.Success=True Then
		If j.GetString.Contains("detail")=True Then
			j.release
			Log("This SessionID already used, use other and try again.")
			Return False
		Else
			j.release
			Log("It seems that VIVA got request for refund, if refunded - don't know, you must check it.")
			Return True
		End If
	Else
		j.Release
		Return False
	End If
	
End Sub

public Sub RetrieveSessionbyId(sessionID1 As String) As ResumableSub
	

	If gottoken=False Then
		Log($"Sorry, not having "access_token" to continue at RetrieveSessionbyId, so JSON will be empty..."$)
		Return Null
	End If
	
	Private j As HttpJob
	j.Initialize("viva",Me)
	
	j.download(usingurl & "sessions/" & sessionID1)
	j.GetRequest.SetHeader("Authorization", "Bearer " & access_token)

	Wait For (j) JobDone(j As HttpJob)

	If j.Success=True Then
		
		Dim jsonstring As String=j.GetString
		
		j.release
	
		Return jsonstring
	Else
		
		j.Release
		
		Return Null
	End If
	
End Sub

public Sub RetrieveSessionbyDate(date1 As String) As ResumableSub
	
	
	If gottoken=False Then
		Log($"Sorry, not having "access_token" to continue at RetrieveSessionbyDate, so list will return with 0 size..."$)
		Return Null
	End If
	
	Private j As HttpJob
	j.Initialize("viva",Me)
	
	j.download(usingurl & "sessions?date="&date1)
	j.GetRequest.SetHeader("Authorization", "Bearer " & access_token)

	Wait For (j) JobDone(j As HttpJob)

	If j.Success=True Then
		
		Dim jsonstring As String=j.GetString

		j.release
		
		Return jsonstring
	Else
		j.Release
		
		Return Null
	End If
	
End Sub

Sub AbortSession(sessionid1 As String, cashRegisterId As String) As ResumableSub
	
	If gottoken=False Then
		Log($"Sorry, not having "access_token" to continue at AbortSession..."$)
		Return False
	End If
	
	Private j As HttpJob
	j.Initialize("viva",Me)
	
	j.download(usingurl & "sessions:abort/" & sessionid1 & "?cashRegisterId=" & cashRegisterId)
	j.GetRequest.SetHeader("Authorization", "Bearer " & access_token)

	Wait For (j) JobDone(j As HttpJob)

	If j.Success=True Then
		Log("OK, Seems that aborted.")
		j.release
		Return True
	Else
		j.Release
		Return False
	End If

End Sub


