Skip to content

Instantly share code, notes, and snippets.

@chrisjsimpson
Created November 24, 2020 22:23
Show Gist options
  • Save chrisjsimpson/f83536d38113f2bd732ad33c835cca0f to your computer and use it in GitHub Desktop.
Save chrisjsimpson/f83536d38113f2bd732ad33c835cca0f to your computer and use it in GitHub Desktop.
Stripe connect create subscription and trigger trial, finalise invoice and confirm payment
# Create a customer
CONNECT_ACCOUNT_ID=acct_abc123
CUSTOMER_ID=`stripe customers create --stripe-account=$CONNECT_ACCOUNT_ID | jq -r .id`
# Create a payment method
PAYMENT_METHOD=`stripe payment_methods create --stripe-account=$CONNECT_ACCOUNT_ID \
--type=card \
-d "card[number]"=4242424242424242 \
-d "card[exp_month]"=11 \
-d "card[exp_year]"=2021 \
-d "card[cvc]"=314 | jq -r .id`
# Attach payment method to customer
stripe payment_methods attach $PAYMENT_METHOD \
--stripe-account=$CONNECT_ACCOUNT_ID \
--customer=$CUSTOMER_ID
# Set default payment method for customer (invoice settings)
stripe customers update $CUSTOMER_ID \
--stripe-account=$CONNECT_ACCOUNT_ID \
-d "invoice_settings[default_payment_method]"=$PAYMENT_METHOD
# Create a product
PRODUCT_ID=`stripe products create --name="Gold Special" \
--stripe-account=$CONNECT_ACCOUNT_ID | jq -r .id`
# Create the subscription, with trial starting x secconds in the future:
trial_end=$(expr 30 + `date +%s`)
SUBSCRIPTION_ID=`stripe subscriptions create \
--stripe-account=$CONNECT_ACCOUNT_ID \
--customer $CUSTOMER_ID \
-d "trial_end"=$trial_end \
-d "items[0][price_data][recurring][interval]"=month \
-d "items[0][price_data][currency]"=GBP \
-d "items[0][price_data][product]"=$PRODUCT_ID \
-d "items[0][price_data][unit_amount]"=599 \
-d "metadata[person_uuid]"=1 \
-d "metadata[plan_uuid]"=840500cb-c663-43e6-a632-d8521bb14c42 \
-d "metadata[subscribie_checkout_session_id]"=abc123 | jq -r .id`
# Force finalize the invoice to generate payment_intent.succeeded event
# ("but that's not really testing the actual flow (just be aware of that)"- src @turbotime.
sleep 120;
INVOICE_ID=`stripe subscriptions retrieve \
--stripe-account=$CONNECT_ACCOUNT_ID \
$SUBSCRIPTION_ID | jq -r .latest_invoice`
# Get payment intent
PAYMENT_INTENT=`stripe invoices finalize_invoice $INVOICE_ID --stripe-account=$CONNECT_ACCOUNT_ID | jq -r .payment_intent`
# Confirm payment
stripe payment_intents confirm \
--stripe-account=$CONNECT_ACCOUNT_ID \
$PAYMENT_INTENT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment