SQL Server Requires Chilkat v11.0.0+
SQL Server
PayPal: Look up a subscription and its recent payments
See more PayPal Examples
Start with the PayPal subscription ID saved with the local customer. Display its status and next billing time, then list transactions for the previous 30 days with each gross amount and currency.
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 @sTmp4 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
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'id', 'SUBSCRIPTION_ID'
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}', @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
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 @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 @errorInfo
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'status'
PRINT 'Status: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'billing_info.next_billing_time'
PRINT 'Next billing: ' + @sTmp0
DECLARE @now int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @now OUT
EXEC sp_OAMethod @now, 'SetFromCurrentSystemTime', @success OUT
DECLARE @query int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @query OUT
EXEC sp_OAMethod @now, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'end_time', @sTmp0
EXEC sp_OAMethod @now, 'AddDays', @success OUT, -30
EXEC sp_OAMethod @now, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'start_time', @sTmp0
EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions', @query, @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 @errorInfo
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @query
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
IF (@iTmp0 < 200) or (@iTmp1 >= 300)
BEGIN
DECLARE @historyError int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @historyError OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @historyError, 'Load', @success OUT, @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAMethod @historyError, 'StringOf', @sTmp0 OUT, 'name'
EXEC sp_OAMethod @historyError, 'StringOf', @sTmp1 OUT, 'details[0].issue'
PRINT 'HTTP ' + @iTmp0 + ' ' + @sTmp0 + ' ' + @sTmp1
EXEC sp_OAMethod @historyError, 'StringOf', @sTmp0 OUT, 'debug_id'
PRINT 'PayPal debug ID: ' + @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @errorInfo
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @historyError
RETURN
END
DECLARE @history int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @history OUT
DECLARE @parsedHistory int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @history, 'Load', @parsedHistory OUT, @sTmp0
IF @parsedHistory <> 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 @errorInfo
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @historyError
EXEC @hr = sp_OADestroy @history
RETURN
END
DECLARE @i int
SELECT @i = 0
EXEC sp_OAMethod @history, 'SizeOfArray', @iTmp0 OUT, 'transactions'
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OASetProperty @history, 'I', @i
EXEC sp_OAMethod @history, 'StringOf', @sTmp0 OUT, 'transactions[i].time'
EXEC sp_OAMethod @history, 'StringOf', @sTmp1 OUT, 'transactions[i].id'
EXEC sp_OAMethod @history, 'StringOf', @sTmp2 OUT, 'transactions[i].status'
EXEC sp_OAMethod @history, 'StringOf', @sTmp3 OUT, 'transactions[i].amount_with_breakdown.gross_amount.value'
EXEC sp_OAMethod @history, 'StringOf', @sTmp4 OUT, 'transactions[i].amount_with_breakdown.gross_amount.currency_code'
PRINT @sTmp0 + ' ' + @sTmp1 + ' ' + @sTmp2 + ' ' + @sTmp3 + ' ' + @sTmp4
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @errorInfo
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @now
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @historyError
EXEC @hr = sp_OADestroy @history
END
GO