I'm having trouble completing orders after Stripe's API changed

I think that I'm typing the wrong thing in my orders and listing controller. Can you take a look? def create @order = Order.new(order_params) @listing = Listing.find(params[:listing_id]) @seller = @listing.user @order.listing_id = @listing.id @order.buyer_id = current_user.id @order.seller_id = @seller.id Stripe.api_key = ENV["STRIPE_API_KEY"] token = params[:stripeToken] begin charge = Stripe::Charge.create( :amount => (@listing.price * 100).floor, :currency => "usd", :card => token ) rescue Stripe::CardError => e flash[:danger] = e.message end transfer = Stripe::Transfer.create( :amount => (@listing.price * 95).floor, :currency => "usd", :destination => @seller.recipient ) respond_to do |format| if @order.save format.html { redirect_to root_url, notice: "Thanks for ordering!" } format.json { render action: 'show', status: :created, location: @order } else format.html { render action: 'new' } format.json { render json: @order.errors, status: :unprocessable_entity } end end end def create @listing = Listing.new(listing_params) @listing.user_id = current_user.id if current_user.recipient.blank? Stripe.api_key = ENV["STRIPE_API_KEY"] token = params[:stripeToken] recipient = Stripe::Account.create( :managed => false, :country => 'US', :email => current_user.email ) current_user.recipient = recipient.id current_user.save end respond_to do |format| if @listing.save format.html { redirect_to @listing, notice: 'Listing was successfully created.' } format.json { render action: 'show', status: :created, location: @listing } else format.html { render action: 'new' } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end

Posted almost 2 years ago by Alexis Soibelman
Posted almost 2 years ago by Alexis Soibelman

I think that I'm typing the wrong thing in my orders and listing controller. Can you take a look?

Orders_controller.rb
_________________________

Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]

begin
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:currency => "usd",
:card => token
)
rescue Stripe::CardError => e
flash[:danger] = e.message
end

transfer = Stripe::Transfer.create(
:amount => (@listing.price * 95).floor,
:currency => "usd",
:destination => @seller.recipient
)



listings_controller.rb
_________________________



def create
@listing = Listing.new(listing_params)
@listing.user_id = current_user.id

if current_user.recipient.blank?
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]


recipient = Stripe::Account.create(
:managed => false,
:country => 'US',
:email => current_user.email
)


current_user.recipient = recipient.id
current_user.save
end

1
Posted almost 2 years ago by Alex Yang

Hi Alexis,

In the future, please post this in the Discussion thread for the specific Etsydemo course video. This will 1) help me understand how much progress you made before you got stuck and 2) group all troubleshooting requests in the same thread, so it will be easier for you to see if your issue has already been solved.

Can you provide more details of the error you're seeing when you try to complete an order? Are you seeing an error message in your browser or Rails server?

Finally, if it's helpful, you'll be able to see the code for the 'official' Etsydemo app here:
https://github.com/BaseRails/etsydemo

Keep in mind that this code still uses the older Stripe API. However, seeing the details of your error will help me determine what code may need to be fixed.

Best,
Alex

1