Codename::Application::APP_ID - application.rb
headers = { :accept => :json, :authorization => 'Basic ' + user_creds, :content_type => 'application/x-www-form-urlencoded; charset=utf-8' }
bill_id = current_user.email.split("@").first + '-' + SecureRandom.hex(5)
url = "https://w.qiwi.com/api/v2/prv/" + Codename::Application::SHOP_ID + "/bills/" + bill_id
life_time = Time.now.utc + Codename::Application::PAYMENTS_DAYS.day
Codename::Application::PAYMENTS_DAYS.day = 7
phone_number = '+79181234567' amount = 50
data = { user: 'tel:' + phone_number, amount: amount, ccy: 'RUB', comment: 'User email: ' + current_user.email, lifetime: life_time.iso8601, pay_source: 'qw', prv_name: 'Super Mortal Kombat Shop' }
begin result = RestClient.put url, data, headers rescue => e payments_logger.info('Creating bill failed! Email: ' + current_user.email + ' Error: ' + e.message ) flash[:error] = ' . : ' + e.message redirect_to action: :pay return end
class Payment < ActiveRecord::Base STATUSES = [DEFAULT = 0, PAID = 1] belongs_to :user validates :amount, :numericality => { greater_than: 0 } end
class CreatePayments < ActiveRecord::Migration def change create_table :payments do |t| t.integer :user_id, :null => false t.foreign_key :users t.float :amount, :default => 0.0, :null => false t.string :bill_id, :limit => 256, :null => false t.string :phone_number, :limit => 256, :null => false t.integer :status, :limit => 1, :default => 0, :null => false t.timestamps null: false end end end
def create_bill require 'rest-client' require 'base64' require 'securerandom' user_creds = Base64.encode64(Codename::Application::APP_ID + ':' + Codename::Application::APP_PASSWORD) headers = { :accept => :json, :authorization => 'Basic ' + user_creds, :content_type => 'application/x-www-form-urlencoded; charset=utf-8' } # Delete old bill, if current user have it bill = current_user.payments.where(status: Payment::DEFAULT).first if bill # Cancel bill url = "https://w.qiwi.com/api/v2/prv/" + Codename::Application::SHOP_ID + "/bills/" + bill.bill_id data = { status: 'rejected' } begin RestClient.patch url, data, headers bill.delete rescue => e payments_logger.info('Cancelling bill failed! Email: ' + current_user.email + ' Error: ' + e.message ) flash[:error] = ' . . : ' + e.message redirect_to action: :pay return end end bill_id = current_user.email.split("@").first + '-' + SecureRandom.hex(5) payment = current_user.payments.new( amount: params[:amount], bill_id: bill_id, phone_number: params[:user_phone] ) # Validate data if payment.invalid? flash[:error] = ' ' redirect_to action: :pay return end url = "https://w.qiwi.com/api/v2/prv/" + Codename::Application::SHOP_ID + "/bills/" + payment.bill_id life_time = Time.now.utc + Codename::Application::PAYMENTS_DAYS.day data = { user: 'tel:' + payment.phone_number, amount: payment.amount, ccy: 'RUB', comment: 'User email: ' + current_user.email, lifetime: life_time.iso8601, pay_source: 'qw', prv_name: ' Super Mortal Kombat Shop' } # Start transaction begin result = RestClient.put url, data, headers rescue => e payments_logger.info('Creating bill failed! Email: ' + current_user.email + ' Error: ' + e.message ) flash[:error] = ' . : ' + e.message payment.delete redirect_to action: :pay return end result = JSON.parse(result) if result["response"]["result_code"] == 0 payment.save flash[:notice] = ' ' redirect_to action: :pay else payment.delete flash[:error] = ' . : ' + result["response"]["result_code"] redirect_to action: :pay end end
def self.get_payments ... ... end
# Get all payments with default statuses bills = Payment.where(status: Payment::DEFAULT) bills.each do |bill| url = "https://w.qiwi.com/api/v2/prv/" + Codename::Application::SHOP_ID + "/bills/" + bill.bill_id begin bill_status = RestClient.get url, headers rescue => e grabber_logger.info('Check bill failed! Error: ' + e.message) next end bill_status = JSON.parse(bill_status) if bill_status["response"]["bill"]["status"] == 'paid' bill.update(status: Payment::PAID) user = User.find_by_id(bill.user_id) new_amount = user.money_real + bill.amount user.update_attribute(:money_real, new_amount) elsif bill_status["response"]["bill"]["status"] == 'waiting' next elsif bill_status["response"]["bill"]["status"] == 'expired' || bill_status["response"]["bill"]["status"] == 'unpaid' || bill_status["response"]["bill"]["status"] == 'rejected' bill.delete else next end
scheduler.every Codename::Application::GRABBER_INTERVAL do PaymentGrabber.get_payments end
SHOP_ID = '111111' APP_ID = '00000000' APP_PASSWORD = 'XXXXXXXXXXXX' # Count days for payments PAYMENTS_DAYS = 7 # Grabber interval in minutes. How othen sheduler will check payments GRABBER_INTERVAL = '3m'
Source: https://habr.com/ru/post/265391/
All Articles