Module: StripeMock::RequestHandlers::Charges
- Included in:
- Instance
- Defined in:
- lib/stripe_mock/request_handlers/charges.rb
Constant Summary collapse
- SEARCH_FIELDS =
[ "amount", "currency", "customer", "payment_method_details.card.brand", "payment_method_details.card.exp_month", "payment_method_details.card.exp_year", "payment_method_details.card.fingerprint", "payment_method_details.card.last4", "status", ].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #capture_charge(route, method_url, params, headers) ⇒ Object
- #get_charge(route, method_url, params, headers) ⇒ Object
- #get_charges(route, method_url, params, headers) ⇒ Object
- #new_charge(route, method_url, params, headers = {}) ⇒ Object
- #refund_charge(route, method_url, params, headers) ⇒ Object
- #search_charges(route, method_url, params, headers) ⇒ Object
- #update_charge(route, method_url, params, headers) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 5 def Charges.included(klass) klass.add_handler 'post /v1/charges', :new_charge klass.add_handler 'get /v1/charges', :get_charges klass.add_handler 'get /v1/charges/search', :search_charges klass.add_handler 'get /v1/charges/((?!search).*)', :get_charge klass.add_handler 'post /v1/charges/(.*)/capture', :capture_charge klass.add_handler 'post /v1/charges/(.*)/refund', :refund_charge klass.add_handler 'post /v1/charges/(.*)/refunds', :refund_charge klass.add_handler 'post /v1/charges/(.*)', :update_charge end |
Instance Method Details
#capture_charge(route, method_url, params, headers) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 126 def capture_charge(route, method_url, params, headers) route =~ method_url charge = assert_existence :charge, $1, charges[$1] if params[:amount] refund = Data.mock_refund( :balance_transaction => new_balance_transaction('txn'), :id => new_id('re'), :amount => charge[:amount] - params[:amount] ) add_refund_to_charge(refund, charge) end if params[:application_fee] charge[:application_fee] = params[:application_fee] end charge[:captured] = true charge end |
#get_charge(route, method_url, params, headers) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 112 def get_charge(route, method_url, params, headers) route =~ method_url charge_id = $1 || params[:charge] charge = assert_existence :charge, charge_id, charges[charge_id] charge = charge.clone if params[:expand] == ['balance_transaction'] balance_transaction = balance_transactions[charge[:balance_transaction]] charge[:balance_transaction] = balance_transaction end charge end |
#get_charges(route, method_url, params, headers) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 81 def get_charges(route, method_url, params, headers) params[:offset] ||= 0 params[:limit] ||= 10 clone = charges.clone if params[:customer] clone.delete_if { |k,v| v[:customer] != params[:customer] } end Data.mock_list_object(clone.values, params) end |
#new_charge(route, method_url, params, headers = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 16 def new_charge(route, method_url, params, headers = {}) stripe_account = headers && headers[:stripe_account] || Stripe.api_key if headers && headers[:idempotency_key] params[:idempotency_key] = headers[:idempotency_key] if charges.any? original_charge = charges.values.find { |c| c[:idempotency_key] == headers[:idempotency_key]} return charges[original_charge[:id]] if original_charge end end id = new_id('ch') if params[:source] if params[:source].is_a?(String) # if a customer is provided, the card parameter is assumed to be the actual # card id, not a token. in this case we'll find the card in the customer # object and return that. if params[:customer] params[:source] = get_card(customers[stripe_account][params[:customer]], params[:source]) else params[:source] = get_card_or_bank_by_token(params[:source]) end elsif params[:source][:id] raise Stripe::InvalidRequestError.new("Invalid token id: #{params[:source]}", 'card', http_status: 400) end elsif params[:customer] customer = customers[stripe_account][params[:customer]] if customer && customer[:default_source] params[:source] = get_card(customer, customer[:default_source]) end end ensure_required_params(params) bal_trans_params = { amount: params[:amount], source: id, application_fee: params[:application_fee] } balance_transaction_id = new_balance_transaction('txn', bal_trans_params) charges[id] = Data.mock_charge( params.merge :id => id, :balance_transaction => balance_transaction_id) charge = charges[id].clone if params[:expand] == ['balance_transaction'] charge[:balance_transaction] = balance_transactions[balance_transaction_id] end charge end |
#refund_charge(route, method_url, params, headers) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 147 def refund_charge(route, method_url, params, headers) charge = get_charge(route, method_url, params, headers) new_refund( route, method_url, params.merge(:charge => charge[:id]), headers ) end |
#search_charges(route, method_url, params, headers) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 105 def search_charges(route, method_url, params, headers) require_param(:query) unless params[:query] results = search_results(charges.values, params[:query], fields: SEARCH_FIELDS, resource_name: "charges") Data.mock_list_object(results, params) end |
#update_charge(route, method_url, params, headers) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/stripe_mock/request_handlers/charges.rb', line 67 def update_charge(route, method_url, params, headers) route =~ method_url id = $1 charge = assert_existence :charge, id, charges[id] allowed = allowed_params(params) disallowed = params.keys - allowed if disallowed.count > 0 raise Stripe::InvalidRequestError.new("Received unknown parameters: #{disallowed.join(', ')}" , '', http_status: 400) end charges[id] = Util.rmerge(charge, params) end |