how will be the body of the recurring order for paypal checkout api - ruby-on-rails

I have a rails application and now im trying to integrate with the Paypal for subscription.
I did a successful checkout with a single non recuring product using paypal checkout api.
My body for the order was
body = {
intent: 'CAPTURE',
application_context: {
return_url: "#{Rails.application.secrets.app_host}#{return_path}",
cancel_url: "#{Rails.application.secrets.app_host}#{cancel_path}"
},
purchase_units: [
{
amount: {
currency_code: 'USD',
value: '220.00'
}
}
]
}
I have my required plan saved in db:
#<Plan id: 6, name: "Daimond", fee: 45, created_at: "2019-08-19 08:40:13", updated_at: "2019-10-11 03:42:26", recuring: true, period: "Month", cycles: 12>
How will be the body for this kind of recurring orders?

Once you have a Plan, subscriptions need to be approved by the payer. The best way to get that approval is discussed in Step 4 here: https://developer.paypal.com/docs/subscriptions/integrate/#4-create-a-subscription

Related

How can I create new book and assign two authors with it using Rails miniTest?

Doing TDD
I facing a problem creating a book and assigning two authors with it. There is a failure in the creation. It says that there is an AssociationTypeMismatch: Author(#15100) expected, got nil which is an instance of NilClass(#40. I suppose that the format "authors: []" is not have been read well by something.
This is the test:
def test_has_many_and_belongs_to_mapping
apress = Publisher.find_by(name: 'Apress')
assert_equal 2, apress.books.size
book = Book.new(
title: 'Rails E-Commerce 3nd Edition',
authors: [
Author.find_by(first_name: 'Christian', last_name: 'Hellsten'),
Author.find_by(first_name: 'Jarkko', last_name: 'Laine')
],
published_at: Time.now,
isbn: '123-123-123-x',
blurb: 'E-Commerce on Rails',
page_count: 300,
price: 30.5
)
# apress.books << book
# apress.reload
# book.reload
# assert_equal 3, apress.books.size
# assert_equal 'Apress', book.publisher.name
end
This is the error:
MY ERD:
My schema:
I think what you are missing is a meaningful setup of data needed before this test runs. Even though those publishers and books may exist in your development environment/database, the test suite usually is configured to wipe the database clean between runs.
If you have fixtures defined, then you can reference them like this:
test/fixtures/publishers.yml
apress:
id: 1
name: Apress
test/fixtures/authors.yml
hellsten:
id: 1
first_name: Christian
last_name: Hellsten
laine:
id: 2
first_name: Jarkko
last_name: Laine
test/fixtures/books.yml
beginning_ruby:
title: Beginning Ruby
publisher_id: 1
ruby_recipes:
title: Ruby Recipes
publisher_id: 1
So that the test would look more like this:
def test_has_many_and_belongs_to_mapping
apress = publishers(:apress)
assert_equal 2, apress.books.size
book = Book.new(
title: 'Rails E-Commerce 3nd Edition',
authors: [
Author.find_by(first_name: 'Christian', last_name: 'Hellsten'),
Author.find_by(first_name: 'Jarkko', last_name: 'Laine')
],
published_at: Time.now,
isbn: '123-123-123-x',
blurb: 'E-Commerce on Rails',
page_count: 300,
price: 30.5
)
apress.books << book
apress.reload
book.reload
assert_equal 3, apress.books.size
assert_equal 'Apress', book.publisher.name
end
Or instead of using fixtures, you could create the content before the test:
So that the test would look more like this:
setup do
publisher = Publisher.create!(name: 'Apress')
publisher.books.create!(title: 'Beginning Ruby')
publisher.books.create!(title: 'Ruby Recipes')
Author.create!(first_name: 'Christian', last_name: 'Hellsten')
Author.create!(first_name: 'Jarkko', last_name: 'Laine')
end
def test_has_many_and_belongs_to_mapping
apress = Publisher.find_by(name: 'Apress')
assert_equal 2, apress.books.size
book = Book.new(
title: 'Rails E-Commerce 3nd Edition',
authors: [
Author.find_by(first_name: 'Christian', last_name: 'Hellsten'),
Author.find_by(first_name: 'Jarkko', last_name: 'Laine')
],
published_at: Time.now,
isbn: '123-123-123-x',
blurb: 'E-Commerce on Rails',
page_count: 300,
price: 30.5
)
apress.books << book
apress.reload
book.reload
assert_equal 3, apress.books.size
assert_equal 'Apress', book.publisher.name
end
That way your database has those values, and your find_by() calls should return data, and not nil.

How to use a Stripe token in ruby code to create an account with Stripe::Account.create()?

I can create a new stripe connect account with
require 'stripe'
Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
Stripe::Account.create({
type: 'express',
country: 'US',
email: 'jenny.rosen#example.com',
capabilities: {
card_payments: {requested: true},
transfers: {requested: true},
},
})
I understand I can provide that account with a test address using a 'token' called address_full_match. But I have no idea what that 'token' is nor how to use it in ruby code, that is, I do not know what ruby code to write to use this 'token'
I tried the obvious to generate/access a token:
Stripe::address_full_match
NoMethodError: undefined method `address_full_match' for Stripe:Module
from (pry):10:in `__pry__'
How can I use this 'token' in ruby code (specifically, in Stripe::Account.create()) ?
Further attempts
Attempt 1
If we search the documentation for any instances of enabled": true it returns zero results, but if we search for instances of enabled": false we get 16 results. So the documentation doesn't provide a single example creating an account with either charges_enabled: true, nor with payouts_enabled: true, which is surprising since that's predominantly what developers want to do - create account functional accounts (not dysfunctional ones)
Attempt 2
I tried placing address_full_match in the 'Address line 1' field of the web UI. But it still results in payments_enabled: false
You will get it from StripeCheckout JS client
you have to include js script from stripe
<script src="https://checkout.stripe.com/checkout.js"></script>
Again create a script and In the js script you have to write like
StripeCheckout.configure({
key: "<%= Rails.configuration.stripe[:publishable_key] %>",
locale: "auto",
name: "XYZ",
description: "XYZ Description",
email: "<%= current_user.email %>",
billingAddress: true,
zipCode: true,
token: function(token) {
console.log("Stripe Token : " + token.id)
}
});

STRIPE checkout SESSION unknown parameters

I am trying to pass some parameters in a checkout session of stripe with this code:
#session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
shipping: {
adress: {
city: 'Paris',
country: 'France',
line1: 'hello',
line2: 'hello again',
postal_code: '0000',
state: 'whatever'
},
name: 'yeahman'
},
shipping_address_collection: {
allowed_countries: ['US', 'CA', 'FR', 'PT', 'ES']
},
customer_email: current_user.email,
line_items: line_items_order,
success_url: new_order_message_url(#order),
cancel_url: order_url(#order)
)
All the attributes are listed in the stripe API doc, but I get this error:
Stripe::InvalidRequestError (Received unknown parameter: shipping)
Has anyone an idea about the reason why?
It's not possible to pre-fill the shipping address in Stripe Checkout. You simply provide a list of allowed countries to the shipping_address_collection parameter and Stripe will display input fields for your customer's to enter their shipping details into. Note that shipping isn't listed as a possible parameter when creating a Checkout Session:
https://stripe.com/docs/api/checkout/sessions/create?lang=ruby

How do i get a unique 'id' for my conversation between my twilio end-point and the person texting to my end-point

Twilio API / TWIML
when a user responds to my text (via text) and i receive their answer to my end-point, how do i identify the sender?
at first i thought it would be the 'sid', but it is not.. I cannot find an 'conversation_id' that exists both in the 'sending' and the 'responding' that could alert my end-point who the sender is.
when i send a text from my server, the response looks like:
{ sid: 'MMf9...',
date_created: 'Thu, 18 Aug 2016 03:24:50 +0000',
date_updated: 'Thu, 18 Aug 2016 03:24:50 +0000',
date_sent: null,
account_sid: '...',
to: '...,
from: '...',
messaging_service_sid: '...',
body: 'Hi. this is an anonymous text from my server',
status: 'accepted',
num_segments: '1',
num_media: '1',
direction: 'outbound-api',
api_version: '2010-04-01',
price: null,
price_unit: null,
error_code: null,
error_message: null,
uri: '/2010-04-01/Accounts/...../Messages/......json',
subresource_uris: { media: '/2010-04-01/Accounts/...../Messages/...../Media.json' },
dateCreated: Thu Aug 18 2016 03:24:50 GMT+0000 (UTC),
dateUpdated: Thu Aug 18 2016 03:24:50 GMT+0000 (UTC),
dateSent: null,
accountSid: '....',
messagingServiceSid: '.....',
numSegments: '1',
numMedia: '1',
apiVersion: '2010-04-01',
priceUnit: null,
errorCode: null,
errorMessage: null,
subresourceUris: { media: '/2010-04-01/Accounts/..../Messages/..../Media.json' } }
When I respond with a text from my phone back to my server, my end-point receives...
{
"ToCountry": "US",
"ToState": "IL",
"SmsMessageSid": "SMe....",
"NumMedia": "0",
"ToCity": "Chicago",
"FromZip": "60626",
"SmsSid": "SMe....",
"FromState": "IL",
"SmsStatus": "received",
"FromCity": "CHICAGO",
"Body": "Try",
"FromCountry": "US",
"To": "....",
"MessagingServiceSid": "....",
"ToZip": "",
"NumSegments": "1",
"MessageSid": "SMe...",
"AccountSid": "...",
"From": "...",
"ApiVersion": "2010-04-01"
}
as you can see the sid in the original sms starts with MMf9... and all the ids in my phone's response start with SMe...
I was hoping for some conversation-id in the sending and responding would be there so that on my end-point, i could create a database entry for the conversation, and store the response of my user from the conversation. But now since there is no conversation_id between sender & receiver, I have no idea where to store the conversation and i have no idea who my server is sending responses to.
since i get the 'to' phone number in the twilio meta-data, that is a way to identify the user.
Since Twilio does let you add custom 'meta-data' to the text message the user would need to respond with extra text in the text message itself to identify the 'conversation', for example:
'John, do you think the winner should be: A, B or C? Add your voter-id to the response: 43z'
Then the user would need to text back: B 43z.
You end-point would identify the answer 'B', and '43z' would identify the respondent as 'John' (supposing you have a database look-up mapping John to 43z on your sever).
Then you can get a total count for who the winner should be, and also know each user's response for the conversation.

Access just the value of BSON::ObjectId in ruby

I'm working on a ruby/rails app backed by Mongodb (using Mongoid). Within the context of the Rails application everything works flawlessly but we're also accessing objects outside of the Rails environment, where I'm having trouble getting the id of an object to return as anything but a hash in the format:
{"$oid"=>"4e0005b78ba4db213500001f"}
I've figured out that I'm seeing because I'm getting back a value that's not just an id string but rather of the type BSON::ObjectId. In addition to requiring the rails environment I've also tried requiring bson explicitly in the file that's doing this work:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "bson"
I need to get the value simply as a string containing the id, which seems to be the default everywhere else but in this one case. Oddly this behavior only occurs in my dev environment (the rest of the guys on my team don't have this problem).
Requisite disclaimer that I'm new to Mongodb so could be missing something truly obvious.
Thanks!
You could try calling to_s on the object. In irb:
ruby-1.9.2-p180 > p = Project.last #=> #<Project _id: 4e00e77d399a46759d000002, _type: nil, version: 1, created_at: 2011-06-21 18:48:34 UTC, updated_at: 2011-06-21 18:48:34 UTC, name: "Testing MongoDB", client_id: 3, client_name: nil, group_id: 35, requestor_id: 14, requestor_name: "Test Client User", requestor_phone: "", creator_id: 2, creator_name: "Some Guy", manager_id: 23, manager_name: "Some Other Guy", manager_phone: "", manager_email: "", active: true, status: "open", default_hourly_cost: "0.0", default_hourly_charge: "0.0", default_material_markup: "0.35", add_email_internal: "", add_email_client: "", client_po_number: "", client_ticket_number: "", date_requested: nil, date_requested(1i): "2011", date_requested(2i): "6", date_requested(3i): "21">
ruby-1.9.2-p180 > p.id.to_s #=> "4e00e77d399a46759d000002"
If that doesn't work, can you post your environment.rb?

Resources