SQL Server Requires Chilkat v11.0.0+
SQL Server
PayPal: Build a worklist of overdue invoices
See more PayPal Examples
Find open invoices due in the past 90 days, including partial payments. Walk all result pages and display the invoice number, due date, and remaining balance for the collections team.
Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @sTmp2 nvarchar(4000)
DECLARE @sTmp3 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- See Unlock Chilkat before making requests.
-- For the merchant's internal backoffice application. Load the sandbox token
-- from the merchant's credential store. Keep it out of publicly distributed apps.
-- See Get a PayPal access token.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'AuthToken', 'SANDBOX_ACCESS_TOKEN'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'status[0]', 'SENT'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'status[1]', 'PARTIALLY_PAID'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'status[2]', 'UNPAID'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'status[3]', 'SHARED'
-- Yesterday is the last overdue date. Include invoices due in the past 90 days.
DECLARE @day int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @day OUT
EXEC sp_OAMethod @day, 'SetFromCurrentSystemTime', @success OUT
EXEC sp_OAMethod @day, 'AddDays', @success OUT, -1
EXEC sp_OAMethod @day, 'GetAsIso8601', @sTmp0 OUT, 'YYYY-MM-DD', 0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'due_date_range.end', @sTmp0
EXEC sp_OAMethod @day, 'AddDays', @success OUT, -89
EXEC sp_OAMethod @day, 'GetAsIso8601', @sTmp0 OUT, 'YYYY-MM-DD', 0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'due_date_range.start', @sTmp0
DECLARE @page int
SELECT @page = 1
DECLARE @lastPage int
SELECT @lastPage = 1
WHILE (@page <= @lastPage)
BEGIN
DECLARE @url int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT
EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page='
EXEC sp_OAMethod @url, 'AppendInt', @success OUT, @page
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/json', @resp
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT 'No HTTP response: ' + @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @day
EXEC @hr = sp_OADestroy @url
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
IF (@iTmp0 < 200) or (@iTmp1 >= 300)
BEGIN
DECLARE @errorInfo int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @errorInfo OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @errorInfo, 'Load', @success OUT, @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAMethod @errorInfo, 'StringOf', @sTmp0 OUT, 'name'
EXEC sp_OAMethod @errorInfo, 'StringOf', @sTmp1 OUT, 'details[0].issue'
PRINT 'HTTP ' + @iTmp0 + ' ' + @sTmp0 + ' ' + @sTmp1
EXEC sp_OAMethod @errorInfo, 'StringOf', @sTmp0 OUT, 'debug_id'
PRINT 'PayPal debug ID: ' + @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @day
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @errorInfo
RETURN
END
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
DECLARE @parsedResult int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @parsedResult OUT, @sTmp0
IF @parsedResult <> 1
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Expected a JSON response; HTTP ' + @iTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @day
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @errorInfo
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @i int
SELECT @i = 0
DECLARE @count int
EXEC sp_OAMethod @result, 'SizeOfArray', @count OUT, 'items'
WHILE (@i < @count)
BEGIN
EXEC sp_OASetProperty @result, 'I', @i
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'items[i].detail.invoice_number'
EXEC sp_OAMethod @result, 'StringOf', @sTmp1 OUT, 'items[i].detail.payment_term.due_date'
EXEC sp_OAMethod @result, 'StringOf', @sTmp2 OUT, 'items[i].due_amount.value'
EXEC sp_OAMethod @result, 'StringOf', @sTmp3 OUT, 'items[i].due_amount.currency_code'
PRINT @sTmp0 + ' due ' + @sTmp1 + ' ' + @sTmp2 + ' ' + @sTmp3
SELECT @i = @i + 1
END
EXEC sp_OAMethod @result, 'IntOf', @lastPage OUT, 'total_pages'
SELECT @page = @page + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @day
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @errorInfo
EXEC @hr = sp_OADestroy @result
END
GO