SQL Server Requires Chilkat v11.0.0+
SQL Server
PayPal: Pay with a previously saved card
See more PayPal Examples
Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.
When a returning buyer selects their saved card, look up its vault ID under their authenticated local account. Send the order price and currency from your own cart. Creating the order can capture immediately; inspect each capture before fulfillment. Follow payer-action if further authentication is required.
Chilkat SQL Server Downloads
-- 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 @sTmp2 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
-- Resolve the saved token under this signed-in buyer. A vault ID from a different
-- customer must never be accepted from the browser.
-- 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, 'SetRequestHeader', NULL, 'Prefer', 'return=representation'
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'intent', 'CAPTURE'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'purchase_units[0].amount.currency_code', 'USD'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'purchase_units[0].amount.value', '30.00'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'payment_source.card.vault_id', 'SAVED_CARD_TOKEN'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'payment_source.card.stored_credential.payment_initiator', 'CUSTOMER'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'payment_source.card.stored_credential.payment_type', 'ONE_TIME'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'payment_source.card.stored_credential.usage', 'SUBSEQUENT'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'payment_source.card.experience_context.return_url', 'https://merchant.example/paypal/return'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'payment_source.card.experience_context.cancel_url', 'https://merchant.example/paypal/cancel'
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api-m.sandbox.paypal.com/v2/checkout/orders', @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 @crypt
EXEC @hr = sp_OADestroy @body
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 @body
EXEC @hr = sp_OADestroy @errorInfo
RETURN
END
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
DECLARE @parsed int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @parsed OUT, @sTmp0
IF @parsed <> 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 @crypt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @errorInfo
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'id'
PRINT 'ID: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'status'
PRINT 'Status: ' + @sTmp0
-- Link order is not fixed. Send the buyer to the matching URL in a browser.
DECLARE @linkIndex int
SELECT @linkIndex = 0
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'links'
WHILE (@linkIndex < @iTmp0)
BEGIN
EXEC sp_OASetProperty @result, 'I', @linkIndex
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'links[i].rel', 'payer-action', 1
IF @iTmp0
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'links[i].href'
PRINT 'Approval URL: ' + @sTmp0
END
SELECT @linkIndex = @linkIndex + 1
END
-- A purchase unit may have no captures yet, or more than one.
DECLARE @i int
SELECT @i = 0
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'purchase_units'
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OASetProperty @result, 'I', @i
DECLARE @j int
SELECT @j = 0
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'purchase_units[i].payments.captures'
WHILE (@j < @iTmp0)
BEGIN
EXEC sp_OASetProperty @result, 'J', @j
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'purchase_units[i].reference_id'
EXEC sp_OAMethod @result, 'StringOf', @sTmp1 OUT, 'purchase_units[i].payments.captures[j].id'
PRINT 'Unit: ' + @sTmp0 + ' capture: ' + @sTmp1
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'purchase_units[i].payments.captures[j].status'
EXEC sp_OAMethod @result, 'StringOf', @sTmp1 OUT, 'purchase_units[i].payments.captures[j].amount.value'
EXEC sp_OAMethod @result, 'StringOf', @sTmp2 OUT, 'purchase_units[i].payments.captures[j].amount.currency_code'
PRINT ' ' + @sTmp0 + ' ' + @sTmp1 + ' ' + @sTmp2
SELECT @j = @j + 1
END
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @errorInfo
EXEC @hr = sp_OADestroy @result
END
GO