SQL Server Stripe: Attach a Source

Back to Index

Attaches a Source object to a Customer. The source must be in a chargeable state.

Documentation: https://stripe.com/docs/api/curl#attach_source

CURL Command

curl https://api.stripe.com/v1/customers/cus_CBbg9PmQ9sLbmo/sources \
   -u STRIPE_SECRET_KEY: \
   -d source=src_1BnETLGswQrCoh0X5KOorXEP \
   -X POST

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --  URL: https://api.stripe.com/v1/customers/cus_CBbg9PmQ9sLbmo/sources
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'api.stripe.com', STR(@port), STR(@bTls), STR(@bAutoReconnect)
    IF STR(@success) <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'SetAuthBasic', @success OUT, 'STRIPE_SECRET_KEY', ''

    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'source', 'src_1BnETLGswQrCoh0X5KOorXEP'

    DECLARE @strResponseBody nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestFormUrlEncoded', @strResponseBody OUT, 'POST', '/v1/customers/cus_CBbg9PmQ9sLbmo/sources'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    DECLARE @jsonResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonResponse OUT

    EXEC sp_OAMethod @jsonResponse, 'Load', @success OUT, @strResponseBody

    DECLARE @id nvarchar(4000)

    DECLARE @object nvarchar(4000)

    DECLARE @amount int

    DECLARE @client_secret nvarchar(4000)

    DECLARE @created int

    DECLARE @currency nvarchar(4000)

    DECLARE @flow nvarchar(4000)

    DECLARE @livemode int

    DECLARE @ownerAddress int

    DECLARE @ownerEmail nvarchar(4000)

    DECLARE @ownerName int

    DECLARE @ownerPhone int

    DECLARE @ownerVerified_address int

    DECLARE @ownerVerified_email int

    DECLARE @ownerVerified_name int

    DECLARE @ownerVerified_phone int

    DECLARE @receiverAddress nvarchar(4000)

    DECLARE @receiverAmount_charged int

    DECLARE @receiverAmount_received int

    DECLARE @receiverAmount_returned int

    DECLARE @receiverRefund_attributes_method nvarchar(4000)

    DECLARE @receiverRefund_attributes_status nvarchar(4000)

    DECLARE @statement_descriptor int

    DECLARE @status nvarchar(4000)

    DECLARE @type nvarchar(4000)

    DECLARE @usage nvarchar(4000)

    DECLARE @bitcoinAddress nvarchar(4000)

    DECLARE @bitcoinAmount int

    DECLARE @bitcoinAmount_charged int

    DECLARE @bitcoinAmount_received int

    DECLARE @bitcoinAmount_returned int

    DECLARE @bitcoinUri nvarchar(4000)

    DECLARE @customer nvarchar(4000)

    EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @object OUT, 'object'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @amount OUT, 'amount'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @client_secret OUT, 'client_secret'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @created OUT, 'created'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @currency OUT, 'currency'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @flow OUT, 'flow'
    EXEC sp_OAMethod @jsonResponse, 'BoolOf', @livemode OUT, 'livemode'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @ownerAddress OUT, 'owner.address'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @ownerEmail OUT, 'owner.email'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @ownerName OUT, 'owner.name'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @ownerPhone OUT, 'owner.phone'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @ownerVerified_address OUT, 'owner.verified_address'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @ownerVerified_email OUT, 'owner.verified_email'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @ownerVerified_name OUT, 'owner.verified_name'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @ownerVerified_phone OUT, 'owner.verified_phone'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @receiverAddress OUT, 'receiver.address'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @receiverAmount_charged OUT, 'receiver.amount_charged'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @receiverAmount_received OUT, 'receiver.amount_received'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @receiverAmount_returned OUT, 'receiver.amount_returned'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @receiverRefund_attributes_method OUT, 'receiver.refund_attributes_method'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @receiverRefund_attributes_status OUT, 'receiver.refund_attributes_status'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @statement_descriptor OUT, 'statement_descriptor'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @status OUT, 'status'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'type'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @usage OUT, 'usage'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @bitcoinAddress OUT, 'bitcoin.address'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @bitcoinAmount OUT, 'bitcoin.amount'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @bitcoinAmount_charged OUT, 'bitcoin.amount_charged'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @bitcoinAmount_received OUT, 'bitcoin.amount_received'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @bitcoinAmount_returned OUT, 'bitcoin.amount_returned'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @bitcoinUri OUT, 'bitcoin.uri'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @customer OUT, 'customer'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO

Sample JSON Response Body

{
  "id": "src_1BnETLGswQrCoh0X5KOorXEP",
  "object": "source",
  "amount": 1000,
  "client_secret": "src_client_secret_CBbgdvc1AflN5jN0wQevA2jA",
  "created": 1516662783,
  "currency": "usd",
  "flow": "receiver",
  "livemode": false,
  "metadata": {},
  "owner": {
    "address": null,
    "email": "jenny.rosen@example.com",
    "name": null,
    "phone": null,
    "verified_address": null,
    "verified_email": null,
    "verified_name": null,
    "verified_phone": null
  },
  "receiver": {
    "address": "test_1MBhWS3uv4ynCfQXF3xQjJkzFPukr4K56N",
    "amount_charged": 0,
    "amount_received": 0,
    "amount_returned": 0,
    "refund_attributes_method": "email",
    "refund_attributes_status": "missing"
  },
  "statement_descriptor": null,
  "status": "chargeable",
  "type": "bitcoin",
  "usage": "single_use",
  "bitcoin": {
    "address": "test_1MBhWS3uv4ynCfQXF3xQjJkzFPukr4K56N",
    "amount": 2371000,
    "amount_charged": 0,
    "amount_received": 0,
    "amount_returned": 0,
    "uri": "bitcoin:test_1MBhWS3uv4ynCfQXF3xQjJkzFPukr4K56N?amount=0.02371000"
  },
  "customer": "cus_CBbg9PmQ9sLbmo"
}