SQL Server Stripe: Create a Coupon

Back to Index

Creates a coupon object.

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

CURL Command

curl https://api.stripe.com/v1/coupons \
   -u STRIPE_SECRET_KEY: \
   -d percent_off=25 \
   -d duration=repeating \
   -d duration_in_months=3 \
   -d id=25OFF \
   -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/coupons
    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, 'percent_off', '25'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'duration', 'repeating'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'duration_in_months', '3'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'id', '25OFF'

    DECLARE @strResponseBody nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestFormUrlEncoded', @strResponseBody OUT, 'POST', '/v1/coupons'
    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_off int

    DECLARE @created int

    DECLARE @currency int

    DECLARE @duration nvarchar(4000)

    DECLARE @duration_in_months int

    DECLARE @livemode int

    DECLARE @max_redemptions int

    DECLARE @percent_off int

    DECLARE @redeem_by int

    DECLARE @times_redeemed int

    DECLARE @valid int

    EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @object OUT, 'object'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @amount_off OUT, 'amount_off'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @created OUT, 'created'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @currency OUT, 'currency'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @duration OUT, 'duration'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @duration_in_months OUT, 'duration_in_months'
    EXEC sp_OAMethod @jsonResponse, 'BoolOf', @livemode OUT, 'livemode'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @max_redemptions OUT, 'max_redemptions'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @percent_off OUT, 'percent_off'
    EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @redeem_by OUT, 'redeem_by'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @times_redeemed OUT, 'times_redeemed'
    EXEC sp_OAMethod @jsonResponse, 'BoolOf', @valid OUT, 'valid'

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


END
GO

Sample JSON Response Body

{
  "id": "25OFF",
  "object": "coupon",
  "amount_off": null,
  "created": 1516662783,
  "currency": null,
  "duration": "repeating",
  "duration_in_months": 3,
  "livemode": false,
  "max_redemptions": null,
  "metadata": {},
  "percent_off": 25,
  "redeem_by": null,
  "times_redeemed": 0,
  "valid": true
}