Last updated 15 hours ago

Stripe integration

Corrily saves which price was displayed to a given user for a given product, therefore the Stripe integration consists in retrieving this price, and sending it to Stripe to bill the customer in their local currency.

Retrieve the price

Retrieving the price of an item can be done with the same call as previously. You can check the API doc for examples of call in major languages. The API will remember which price you displayed for a given user to make sure that there is no confusion for them.

Examples below will assume that the following was ran, and that the user is purchasing item "a".

import requests

item_prices = {"monthly_plan": 12., "annual_plan": 20.} # Assuming this is a dict of your items with prices in $
ip_user = "83.205.82.236"

api_url = "https://app.corrily.com/v1/prices/get"
payload = {
    "ip": "185.35.50.4",
    "products": [{"product_id": item_id} for item_id, _ in item_prices.items()]
}

headers = {
  "Content-Type": "application/json",
  "X-API-KEY": "YOUR_API_KEY_HERE"
}
response = requests.post(url, json=payload, headers=headers)

data = response.json()
product_prices = data["products"]

Charge the customer

Integrations can vary with Stripe, but here is a few examples of how an integration can be done with different flows.

Although we mention local_price below, the same logic applies for handling optimal prices with localization turned off.

With PaymentIntent

intent = stripe.PaymentIntent.create(
  amount=product_prices["monthly_plan"]["local_price"],
  currency=product_prices["monthly_plan"]["local_currency"].lower(),
)

With Stripe Subscription

This will create a subscription with a custom price. The custom price will not impact any saved prices you might have for the product id.

subscription = stripe.Subscription.create(
            customer=data['customerId'],
            items=[
                {
                    'price': {
                      'unit_amount': product_prices["monthly_plan"]["local_price"],
                      'currency': product_prices["monthly_plan"]["local_currency"].lower(),
                      'product': "monthly_plan", # or whatever the matching stripe product ID is
                      'recurring': {
                        'interval': "month"
                      }
                    }
                }
            ],
            expand=['latest_invoice.payment_intent'],
        )

With Stripe Checkout

checkout_session = stripe.checkout.Session.create(
            payment_method_types=['card'],
            line_items=[
                {
                    'price_data': {
                        'currency': product_prices["monthly_plan"]["local_currency"].lower(),
                        'unit_amount': product_prices["monthly_plan "]["local_price"],
                        'product_data': {
                            'name': 'Stubborn Attachments',
                        },
                        'recurring': {
                            'interval': "month"
                        }
                    },
                    'quantity': 1,
                },
            ],
            mode='payment',
            success_url=YOUR_DOMAIN + '/success.html',
            cancel_url=YOUR_DOMAIN + '/cancel.html',
        )

Get insights by letting Corrily know a transaction was successful

The last part to integrating with Corrily is to send a call to our setSuccess API. This allows us to provide you with data and insights regarding pricing.

To do so, send us a call on transaction success as follow.

import requests

url = "https://app.corrily.com/v1/prices/setSuccess"

payload = {
  "user_id": "user_100321",
  "products": {
    [
      "product_id": "monthly_subscription",
      "amount": 50
    ]
  }
}

headers = {
  "Content-Type": "application/json",
  "X-API-KEY": "YOUR_API_KEY_HERE"
}

response = requests.post(url, json=payload, headers=headers)

We can help!

If you have any issue with the integration, you can contact us directly at stripe@corrily.com. We can help you get set up easily and send you code snippets.

Copyright © Corrily 2020. All right reserved.