# QuataPay in Ruby Plain HTTP, no QuataPay package; works with test and live keys. Secrets come from `QUATAPAY_API_KEY` and `QUATAPAY_WEBHOOK_SECRET`. ## Create a payment ```ruby require "json" require "net/http" BASE = "https://api.quatapay.com/api/v1" def quatapay_create_payment(order_id:, amount_xaf:, idempotency_key:) key = ENV.fetch("QUATAPAY_API_KEY") path = key.start_with?("qpay_test_") ? "/gateway/sandbox/payments" : "/gateway/payments" uri = URI(BASE + path) req = Net::HTTP::Post.new(uri) req["Authorization"] = "Bearer " + key req["Content-Type"] = "application/json" req["Idempotency-Key"] = idempotency_key # one per checkout attempt, reused on retries req.body = JSON.generate( amount: amount_xaf, # integer XAF currency: "XAF", customer_reference: order_id.to_s, return_url: "https://your-site.example/pay/return?order=" + order_id.to_s, cancel_url: "https://your-site.example/pay/cancel?order=" + order_id.to_s ) res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 5, read_timeout: 30) { |h| h.request(req) } body = JSON.parse(res.body) raise "QuataPay #{res.code} #{body['code']}: #{body['error']}" unless res.is_a?(Net::HTTPSuccess) [body.dig("data", "checkout_url"), body.dig("data", "payment", "id")] end ``` ## Verify a webhook ```ruby require "openssl" def quatapay_secure_equal?(a, b) return false unless a.bytesize == b.bytesize diff = 0 a.bytes.zip(b.bytes) { |x, y| diff |= x ^ y } diff.zero? end def quatapay_valid_signature?(secret, raw_body, timestamp, signature) return false unless timestamp.to_s.match?(/\A\d+\z/) && signature return false if (Time.now.to_i - timestamp.to_i).abs > 300 expected = "sha256=" + OpenSSL::HMAC.hexdigest("SHA256", secret, "#{timestamp}.#{raw_body}") quatapay_secure_equal?(expected, signature) end # Rails: raw_body = request.raw_post, headers request.headers["X-QuataPay-Timestamp"] / ["X-QuataPay-Signature"], # skip_forgery_protection on the webhook action. Dedupe on (data["id"], event) with a unique index. ```