mrsool / zatca

An unofficial Ruby library for generating ZATCA e-invoices, QR Codes, and submitting e-invoices to ZATCA's servers.
MIT License
36 stars 12 forks source link

Always fails with invalid OTP #23

Closed davidfrothin closed 7 months ago

davidfrothin commented 7 months ago

I have set valid OTP with the code below and correct VAT numbers etc

require "zatca"

vat_id = "131231231231312312"

# Four digits, each digit acting as a bool. The order is as follows: Standard Invoice, Simplified, future use, future use
invoice_type = "1100"

options = {
  common_name: "1231231123123123",
  organization_identifier: vat_id,
  organization_name: "testing",
  organization_unit: "IT",
  country: "SA",
  invoice_type: invoice_type,
  address: "Riyadh, Al Olaya D, Al Olaya, 111111",
  business_category: "IT",

  # The solution provider name
  egs_solution_name: "zxvasfsfsfadf",

  # The model of the unit the stamp is being generated for
  egs_model: "10001",

  # If you have multiple devices each should have a unique serial number
  egs_serial_number: "100000211"
}

# ENSURE THAT YOU USE YOUR OWN PRIVATE KEY by passing private_key_path and private_key_password.
# Otherwise the SDK will generate a passwordless one and delete it (this is only for testing purposes)
# Modes:
# :production (Via Fatoora Portal)
# :sandbox (Via Developer Portal)
# :simulation (Via Fatoora Portal - Simulation)
# For the sandbox set production_mode to false, otherwise set it to true. It is true by default.
#generator = ZATCA::Signing::CSR.new(csr_options: options, mode: :production)
generator = ZATCA::Signing::CSR.new(csr_options: options, mode: :simulation, private_key_path: '/home/ubuntu/myzats/ec-secp256k1-priv-key.pem')

# This is the CSR as PEM
csr = generator.generate

# Output the CSR
puts "CSR: #{csr}"

# ZATCA's API expects us to encode the PEM to Base64
csr_base64 = Base64.strict_encode64(csr)

# Output the CSR base64
puts "CSR: #{csr_base64}"

# Construct an unauthenticated API client, this is the only endpoint that is unauthenticated
client = ZATCA::Client.new(username: "", password: "")

# Get this OTP from Fatoora portal
otp = "253253"
response = client.issue_csid(csr: csr_base64, otp: otp)

# Output the response
puts "Response: #{response}"

always shows Response: Invalid OTP

obahareth commented 7 months ago

@davidfrothin Make sure the API environment on the client matches the CSR environment.

e.g. client = ZATCA::Client.new(username: "", password: "", environment: :simulation)