Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

PayPal: Void an unused authorization

See more PayPal Examples

Release an unused hold with the authorization ID. A fully captured authorization cannot be voided; refund its capture instead. A successful void can have an empty response body.

PayPal documentation

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 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

    --  Use the original authorization ID to release the hold; do not send an amount.

    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'id', 'AUTHORIZATION_ID'
    --  Create and persist this key once per business operation. On a timeout,
    --  retry the same request with its saved key, not a newly generated one.
    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    DECLARE @requestId nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'GenerateUuid', @requestId OUT
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'PayPal-Request-Id', @requestId

    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'POST', 'https://api-m.sandbox.paypal.com/v2/payments/authorizations/{$id}/void', @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 @crypt
        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 @crypt
        EXEC @hr = sp_OADestroy @errorInfo
        RETURN
      END


    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'Authorization void accepted; HTTP ' + @iTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @errorInfo


END
GO