Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoSkorJjj committed Apr 22, 2024
1 parent 8724e0d commit 3f42cc6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 80 deletions.
26 changes: 11 additions & 15 deletions backend/complex/process-chunks/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,19 @@ def initialize_api_key_cycle(self):
def initialize_rabbitmq(self):
"""Initializes the RabbitMQ connection and channels."""

environment = os.getenv('ENVIRONMENT', 'development')

if environment == 'production':
ssl_context = None
if os.getenv("ENVIRONMENT") == "production":
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
ssl_context.set_ciphers('ECDHE+AESGCM:!ECDSA')
url = f"amqps://{self.RABBITMQ_USERNAME}:{self.RABBITMQ_PASSWORD}@{self.RABBITMQ_SERVER}:5671"
parameters = pika.URLParameters(url)
parameters.ssl_options = pika.SSLOptions(context=ssl_context)
self.connection = pika.BlockingConnection(parameters)
else:
credentials = pika.PlainCredentials(self.RABBITMQ_USERNAME, self.RABBITMQ_PASSWORD)
connection_parameters = pika.ConnectionParameters(
host=self.RABBITMQ_SERVER,
credentials=credentials,
)
self.connection = pika.BlockingConnection(connection_parameters)


credentials = pika.PlainCredentials(self.RABBITMQ_USERNAME, self.RABBITMQ_PASSWORD)
connection_parameters = pika.ConnectionParameters(
host=self.RABBITMQ_SERVER,
credentials=credentials,
ssl_options=pika.SSLOptions(context=ssl_context) if ssl_context else None
)

self.connection = pika.BlockingConnection(connection_parameters)
self.channel = self.connection.channel()
self.channel.queue_declare(queue=self.QUEUE_NAME_1, durable=True)
self.channel.queue_declare(queue=self.QUEUE_NAME_2, durable=True)
Expand Down
40 changes: 0 additions & 40 deletions backend/kong-gateway/rawstring-adapter/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,3 @@ end

return RawStringAdapterHandler

-- local cjson = require "cjson.safe"

-- local RawStringAdapterHandler = {
-- PRIORITY = 1000,
-- VERSION = "1.0",
-- }

-- function RawStringAdapterHandler:access(config)
-- -- Retrieve the raw body directly without transformation
-- local raw_body, err = kong.request.get_raw_body()
-- if err or not raw_body then
-- kong.log.err("Error reading request body or body is empty: ", err)
-- return kong.response.exit(400, { message = "Bad request" })
-- end

-- -- Attempt to decode and re-encode to ensure it's valid JSON
-- local decoded_json, decode_err = cjson.decode(raw_body)
-- if decode_err then
-- kong.log.err("Failed to decode JSON: ", decode_err)
-- return kong.response.exit(400, { message = "Invalid JSON" })
-- end

-- local encoded_json, encode_err = cjson.encode(decoded_json)
-- if encode_err then
-- kong.log.err("Failed to re-encode JSON: ", encode_err)
-- return kong.response.exit(500, { message = "Internal Server Error" })
-- end

-- -- Forward the validated and re-encoded JSON
-- kong.service.request.set_raw_body(encoded_json)

-- -- Optionally, forward headers as is
-- local headers = kong.request.get_headers()
-- for k, v in pairs(headers) do
-- kong.service.request.set_header(k, v)
-- end
-- end

-- return RawStringAdapterHandler

91 changes: 66 additions & 25 deletions backend/simple/payment/src/proto-services/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,73 @@ def cancel_subscription(cancel_request, _call)
raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, e.message)
end

def webhook(webhook_request, _call)
payload = webhook_request.raw
stripe_signature = _call.metadata["stripe-signature"]
event = nil

begin
event = configured_stripe::Webhook.construct_event(
payload, stripe_signature, ENV['STRIPE_WEBHOOK_SECRET']
)
rescue JSON::ParserError => e
raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, e.message)
rescue configured_stripe::SignatureVerificationError => e
raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, e.message)
end

if event.type != 'checkout.session.completed'
raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, "Received unsupported event of type: #{event.type}")
end

session = event.data.object
customer_id = session.customer
subscription_id = session.subscription
# def webhook(webhook_request, _call)
# payload = webhook_request.raw
# stripe_signature = _call.metadata["stripe-signature"]
# event = nil

# begin
# event = configured_stripe::Webhook.construct_event(
# payload, stripe_signature, ENV['STRIPE_WEBHOOK_SECRET']
# )
# rescue JSON::ParserError => e
# raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, e.message)
# rescue configured_stripe::SignatureVerificationError => e
# raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, e.message)
# end

# if event.type != 'checkout.session.completed'
# raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, "Received unsupported event of type: #{event.type}")
# end

# session = event.data.object
# customer_id = session.customer
# subscription_id = session.subscription

customer = configured_stripe::Customer.retrieve(customer_id)
customer_email = customer.email
# customer = configured_stripe::Customer.retrieve(customer_id)
# customer_email = customer.email

Payment::WebhookResponse.new(email: customer_email, subscription_id: subscription_id)
# Payment::WebhookResponse.new(email: customer_email, subscription_id: subscription_id)
# end
def webhook(webhook_request, _call)
# Setup logger
logger = Logger.new($stdout)
logger.level = Logger::DEBUG

payload = webhook_request.raw
stripe_signature = _call.metadata["stripe-signature"]

# Log the raw payload and its data type
logger.debug("Received payload: #{payload}")
logger.debug("Payload data type: #{payload.class}")

# Log the Stripe signature
logger.debug("Received Stripe signature: #{stripe_signature}")

begin
event = configured_stripe::Webhook.construct_event(
payload, stripe_signature, ENV['STRIPE_WEBHOOK_SECRET']
)
rescue JSON::ParserError => e
logger.error("JSON parsing error: #{e.message}")
raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, e.message)
rescue configured_stripe::SignatureVerificationError => e
logger.error("Signature verification error: #{e.message}")
raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, e.message)
end

if event.type != 'checkout.session.completed'
logger.error("Unsupported event type: #{event.type}")
raise GRPC::BadStatus.new_status_exception(GRPC::Core::StatusCodes::INVALID_ARGUMENT, "Received unsupported event of type: #{event.type}")
end

session = event.data.object
customer_id = session.customer
subscription_id = session.subscription

customer = configured_stripe::Customer.retrieve(customer_id)
customer_email = customer.email

Payment::WebhookResponse.new(email: customer_email, subscription_id: subscription_id)
end
end

0 comments on commit 3f42cc6

Please sign in to comment.