I'm currently learning programming, and this keeps what I want to make a note of for myself.
(It's a continuation of what I posted last time, or something like another route.) [Rails] Error resolution when generating tokens with PAYJP
After entering the test card information and pressing the purchase button,
Was displayed.
I will omit it because it is described in the previous content,
When I wrote console.log () in the JavaScript description of token generation and checked status, it was 200, and it was confirmed that token generation was successful.
(No tokens ?? That shouldn't be the case !!!)
I used binding.pry to check the contents of params to find the cause of the error.
pry(#<OrdersController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"uRxJ+Ho4c2vi4Pc8MrK/s7UhNbujnVBDt7qjJ11pFpeHWDMltGl3eu/ls94DaALSPNfIpoMtUd4aeOcs4Z8Y4w==", "item_order"=>{"postal_code"=>"555-0000", "prefecture_id"=>"9", "city"=>"Municipality", "block_number"=>"address", "building_name"=>"Building name", "phone_number"=>"09012345678"}, "token"=>"tok_3b5890d13fb07a96a6cf2fa832e0", "controller"=>"orders", "action"=>"create", "id"=>"3"} permitted: false>
As you can see from the second half,
"token"=>"tok_3b5890d13fb07a96a6cf2fa832e0"
The token was firmly in the params! !! !!
The tokens are well generated!
Next, let's take a look at the current state of the controller.
****_controller.rb (partial excerpt)
def order_params
params.require(:item_order).permit(:token, :postal_code, :prefecture_id, :city, :block_number, :building_name, :phone_number).merge(item_id: @item[:id], user_id: current_user.id)
end
def set_item
@item = Item.find(params[:id])
end
def pay_item
Payjp.api_key = ENV["PAYJP_SECRET_KEY"]
Payjp::Charge.create(
amount: @item.selling_price,
card: order_params[:token],
currency:'jpy'
)
end
As for the current description,
It receives : token in: item_order in params as ʻorder_params [: token]`.
It means that.
Now, let's check again by typing ʻorder_params in binding.pry`.
pry(#<OrdersController>)> order_params
=> <ActionController::Parameters {"postal_code"=>"555-0000", "prefecture_id"=>"9", "city"=>"Municipality", "block_number"=>"address", "building_name"=>"Building name", "phone_number"=>"09012345678", "item_id"=>3, "user_id"=>3} permitted: true>"building_name"=>"Building name", "phone_number"=>"09012345678"} permitted: false>
There is no token! !! !! !! !! !! !!
params.require(:item_order).permit(:token, ~~~~)
Why are you writing with require and permit like this? ?? ??
When I checked with bindng.pry, I was able to confirm that token was generated, so there was a problem with how to move the generated token.
The location where token is currently generated is outside ʻitem_order and directly under params, so The location of the token` was different in the first place.
In binding.pry, try typingparams [: token],
pry(#<OrdersController>)> params[:token]
=> "tok_14078502197e031107d18bb7e428"
Will come out.
So it was necessary to write it like this.
****_controller.rb
def order_params
params.require(:item_order).permit(:postal_code, :prefecture_id, :city, :block_number, :building_name, :phone_number).merge(item_id: @item[:id], user_id: current_user.id, token: params[:token])
end
It's a bit long, but I'm writing token inside the merge method.
It is a description to attract to ʻorder_params instead of ʻitem_order.
By doing this, I was able to attract to ʻorder_params, so after changing the description, check with binding.pry`,
pry(#<OrdersController>)> order_params
=> <ActionController::Parameters {"postal_code"=>"333-0000", "prefecture_id"=>"13", "city"=>"Municipality", "block_number"=>"address", "building_name"=>"Building name", "phone_number"=>"09012345678", "item_id"=>3, "user_id"=>3, "token"=>"tok_14078502197e031107d18bb7e428"} permitted: true>
This is perfect ☆ You can now receive it at ʻorder_params [: token]`, and the payment was successful! !! !!
Understand the whereabouts of token!
Use the merge method to merge!