Created
March 6, 2018 04:13
-
-
Save arinhouck/5b6de92ee56de0bd81a80e5692f97bae to your computer and use it in GitHub Desktop.
Basic Stellar Payment Processor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "#{Rails.root}/lib/sdk/stellar" | |
| class PaymentProcessor | |
| def self.perform | |
| # Create or find a setting record by `cursor` key | |
| cursor_setting = Setting.find_or_create_by(key: 'cursor') | |
| # Extract the value of the `cursor` if it exists in DB | |
| cursor = cursor_setting&.value | |
| # Instantiate the Stellar SDK by merchant account public key and cursor | |
| sdk = ::Stellar.new(ENV.fetch('ACCOUNT_ID', 'default_value_account_id_or_set_in_env'), cursor) | |
| # Instantiate list of memos | |
| memos = [] | |
| # Extract transactions | |
| transactions = sdk.transactions | |
| # Loop until no transactions left (empty) | |
| until transactions.empty? | |
| # iterate each transaction and extract memo | |
| transactions.each do |tr| | |
| memos << tr.memo | |
| # Set cursor to call API again with new position | |
| sdk.cursor = tr.paging_token | |
| end | |
| # When you call transactions again it will | |
| # extract transactions after the newly defined cursor | |
| transactions = sdk.transactions | |
| end | |
| # Mark all purchases as completed by memo | |
| Purchase.where(memo: memos.compact).update_all(completed: true) | |
| # Update latest cursor in database | |
| # so next time payment processor starts it | |
| # will load from latest cursor | |
| if sdk.cursor != cursor | |
| cursor_setting.update_column(:value, sdk.cursor) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment