OneTrial with Rails
- Snippet on the signup page:
html<script src="https://onetrial.dev/v1.js" data-key="YOUR_PUBLIC_KEY" async></script>
- Send the token with the form:
js// Signup form (browser): include the token in your POSTconst token = await OneTrial.getToken(); // window.OneTrial is set by the snippetOneTrial.markSubmit(); // records time-to-submitformData.append('visitorToken', token);
- Decide in SignupsController#create:
ruby# app/controllers/signups_controller.rbdef createuser = User.create!(email: params[:email])visitor_token = params[:visitorToken]res = Net::HTTP.post(URI("https://onetrial.dev/api/v1/decisions"),{ visitorToken: visitor_token, email: email, ip: request.remote_ip, userId: user.id }.to_json,"Authorization" => "Bearer #{ENV.fetch('ONETRIAL_API_KEY')}", "Content-Type" => "application/json")d = JSON.parse(res.body)return render_paid_plan if d["decision"] == "deny"return redirect_to d["challengeUrl"], allow_other_host: true if d["decision"] == "challenge"create_trial(user)end
- Env:
ONETRIAL_API_KEY=ot_test_...(server only). Test keys hit the same engine; switch toot_live_when you go live. - Verify:
GET /api/v1/integration/statusreturns a checklist. Shadow mode is on until you flip it.