How to use a saved payment source to purchase in ActiveMerchant?

I use ActiveMerchant with Braintree as a payment processor. I want to use the functionality of the Braintree customer repository to store credit card information.

Storage is going well, but I can’t determine the correct way to pay by credit card customer_vault_id. It looks very straight forward according to the Braintree documentation , but I'm not sure how to pass this through ActiveMerchant. I get validation errors from Braintree if I send a credit card with empty data and errors from ActiveMerchant if I try nil. The only thing that seems obvious is to send customer_vault_idto the hash purchase_options, for example:

GATEWAY.purchase(self.price_in_cents, self.credit_card, 
                 :ip => self.ip_address,
                 :customer_vault_id => 12345)

Is this the right way to use client storage?

If so, what is the correct second argument for this line if I want to use the saved customer store as a payment method?

Thanks.

+3
source share
2 answers

I found that you can substitute customer_vault_idas a string for ActiveMerchant::Billing::CreditCardin the purchase method. The docs really have no indication of this :(

+2
source

After calling #store on the gateway, you will receive a response in which you need to save authorization.

According to docs found on wiki: https://github.com/activemerchant/active_merchant/wiki/Patterns-&-Standards

, , . Response #.

/: ,

,

gateway = ActiveMerchant::Billing::BraintreeGateway.new(login: login, password: password)
credit_card = ActiveMerchant::Billing::CreditCard.new(
                :first_name         => 'Bob',
                :last_name          => 'Bobsen',
                :number             => '4242424242424242',
                :month              => '8',
                :year               => Time.now.year+1,
                :verification_value => '000')
response = gateway.store(credit_card)
=> #<ActiveMerchant::Billing::Response:0x00007f8efb3df1a8
 @authorization="1508682289#1508160804#cim_store",
 @avs_result={"code"=>nil, "message"=>nil, "street_match"=>nil, "postal_match"=>nil},
 @cvv_result={"code"=>nil, "message"=>nil},
 @emv_authorization=nil,
 @error_code=nil,
 @fraud_review=false,
 @message="Successful",
 @params=
  {"message_code"=>"1",
   "message_text"=>"Successful",
   "result_code"=>"Ok",
   "test_request"=>nil,
   "customer_profile_id"=>"1508682289",
   "customer_payment_profile_id"=>"1508160804",
   "direct_response"=>nil},
 @success=true,
 @test=true>

response.authorization
=> "1508682289#1508160804#cim_store"
gateway.purchase(100, response.authorization)
=> #<ActiveMerchant::Billing::Response:0x00007f8ede0027c8
 @authorization="40036062888#XXXX4242#cim_purchase",
 @avs_result=
  {"code"=>"Y", "message"=>"Street address and 5-digit postal code match.", "street_match"=>"Y", "postal_match"=>"Y"},
 @cvv_result={"code"=>"P", "message"=>"CVV not processed"},
 @emv_authorization=nil,
 @error_code=nil,
 @fraud_review=false,
 @message="This transaction has been approved.",
 @params=
  {"message_code"=>"1",
   "message_text"=>"Successful",
   "result_code"=>"Ok",
   "test_request"=>nil,
   "customer_profile_id"=>nil,
   "customer_payment_profile_id"=>nil,
   "direct_response"=>
    "1,1,1,This transaction has been approved.,T91GL2,Y,40036062888,,,1.00,CC,auth_capture,2852040810cf440a4a13,Bob,Bobsen,,,,n&#47;a,,,,,,,,,,,,,,,,,,,,P,2,,,,,,,,,,,XXXX4242,Visa,,,,,,,,,,,,,,,,,",
   "response_code"=>1,
   "response_subcode"=>"1",
   "response_reason_code"=>"1",
   "response_reason_text"=>"This transaction has been approved.",
   "approval_code"=>"T91GL2",
   "avs_result_code"=>"Y",
   "transaction_id"=>"40036062888",
   "invoice_number"=>"",
   "order_description"=>"",
   "amount"=>"1.00",
   "method"=>"CC",
   "transaction_type"=>"auth_capture",
   "customer_id"=>"2852040810cf440a4a13",
   "first_name"=>"Bob",
   "last_name"=>"Bobsen",
   "company"=>"",
   "address"=>"",
   "city"=>"",
   "state"=>"n&#47;a",
   "zip_code"=>"",
   "country"=>"",
   "phone"=>"",
   "fax"=>"",
   "email_address"=>"",
   "ship_to_first_name"=>"",
   "ship_to_last_name"=>"",
   "ship_to_company"=>"",
   "ship_to_address"=>"",
   "ship_to_city"=>"",
   "ship_to_state"=>"",
   "ship_to_zip_code"=>"",
   "ship_to_country"=>"",
   "tax"=>"",
   "duty"=>"",
   "freight"=>"",
   "tax_exempt"=>"",
   "purchase_order_number"=>"",
   "md5_hash"=>"",
   "card_code"=>"P",
   "cardholder_authentication_verification_response"=>"2",
   "account_number"=>"XXXX4242",
   "card_type"=>"Visa",
   "split_tender_id"=>"",
   "requested_amount"=>"",
   "balance_on_card"=>""},
 @success=true,
 @test=true>
0

Source: https://habr.com/ru/post/1711495/


All Articles