Is there any alternative for Reference Transaction in Paypal REST API? - ruby-on-rails

After some time of researching and exploring the Paypal REST API documentation, I've found that currently reference transaction is available only in Paypal Classic API but not in Paypal REST API as stated in this StackOverflow post so I'm trying to find an alternative to bill users with different amounts every month.
Integrated Paypal REST API in my Rails 4 application. I'm trying to create agreements with auto-billing plans without charging real amount, and I planned to use set-balance to charge the remaining amounts and bill the users with outstanding amount. Eventually I've searched through the methods in paypal-sdk-rest but there's no method for me to set an outstanding balance on the agreement object.
And now I'm in stuck. Is there a Ruby method call to set and bill the outstanding balance to the agreements, or perhaps another way of charging users with different amounts every month?
A way I'd think of is to send invoices to users manually by using the Invoice object in REST API, but I prefer to bill users using agreements instead. Using Classic API might be the best option in this case but REST is still a better choice for future.
Hope to get reference transaction works in REST API very soon.
Plan:
plan = Paypal::Plan.new({
name: "Test Plan",
description: "Test recurring plan",
type: "infinite",
payment_definitions: [{
name:"Free Trial",
type:"TRIAL",
frequency:"MONTH",
frequency_interval:"1",
amount: {
value: "0.00",
currency: "USD"
},
cycles: "1"
},
{
name:"Standard Package",
type:"REGULAR",
frequency:"MONTH",
frequency_interval:"1",
amount: {
value: "0.01",
currency: "USD"
},
cycles: "11"
}],
merchant_preferences: {
setup_fee: {
value: "0",
currency: "USD"
},
auto_bill_amount: "YES",
return_url: return_url,
cancel_url: cancel_url,
}
})
Agreement:
agreement = Paypal::Agreement.new({
name: "Monthly billing",
description: "Billing Agreement",
start_date: (Date.today+ 1.month).strftime("%Y-%m-%dT%H:%M:%SZ"),
payer: {
payment_method: "paypal",
},
plan: {
id: Settings.plan_id
}})

Related

Stripe: Change currency depending on location?

I have set USD as the default currency on my product but added 4 more currencies (EUR, GBP, AUD and CHF). How can I make these swap depending on the customers physical location. For the sake of an example, let's assume you are from Germany and want to buy my product - I want your displayed currency to automatically set itself to EUR during checkout.
<script>
var stripe = Stripe(
"pk_live_51LzJXLEOzrDWipQGPi4HbwFv1xxGw7wty7jifLYuKU9VwfpNvCb1DIfDohwHfJSk9VKQ1BhdpDIR8bVnjA9fEzR200y8nDoKqj"
)
document.getElementById("checkout").addEventListener("click", function(){
stripe.redirectToCheckout({
lineItems: [
{
price: 'price_1MKKUgEOzrDWipQGG2tTXjxV',
quantity: 1,
},
],
mode: 'payment',
successUrl: `https://galusfilms.com/luts=success`,
cancelUrl: `https://galusfilms.com/luts=cancel`,
})
.then(function(result){
});
})
</script>
This it what my current checkout session looks like, what do I need to add? I have very little experience in coding and all of this stuff and because of this I would really appreciate some help...
Since the price you've created supports multiple currencies, Stripe Checkout automatically localizes and presents the local currency to your customers in either USD, EUR, GBP, AUD or CHF in this case.
To test your currency presentment in AUD, when creating the Checkout session, you can pass test+location_AU#example.com in the customer_email parameter. Your code will look something like this:
const session = await stripe.checkout.sessions.create({
line_items: [{price: '{{PRICE_ID}}', quantity: 1}],
mode: 'payment',
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
customer_email: 'test+location_AU#example.com',
});
To see more details on how to test currency presentments you can visit this document.

Stripe Subscription update the plan immediately with full amount

I'm looking to update the customer's subscription, and the new plan price will be deducted immediately.
Consider the following scenario:
User is on Plan A($15 per month) and wants to update to Plan B($25 per month) in the middle of the month.
I want the user to charge $25 right away rather than prorate - I don't want the difference will be charged to the customer.
I also try with proration_behavior='always_invoice' However, it is charge $10. I need $25 charged right away.
subscription = Stripe::Subscription.retrieve('sub_49ty4767H20z6a')
Stripe::Subscription.update(
subscription.id,
{
cancel_at_period_end: false,
proration_behavior: 'none',
items: [
{
id: subscription.items.data[0].id,
price: 'price_1LcBtVHhBkzUOaGo0gvFmaMS'
}
]
}
)
I see you’re looking to update a subscription without any prorations and to immediately charge for the upgraded price. The way to achieve this would be by specifically passing proration_behavior: ‘none’ and billing_cycle_anchor: ‘now’. Your code will look something like this:
Stripe::Subscription.update(
subscription.id,
{
cancel_at_period_end: false,
billing_cycle_anchor: ‘now’,
proration_behavior: ‘none’,
items: [
{
id: subscription.items.data[0].id,
price: 'price_xxxxxxx'
}
]
}
)
Please note that this changes the billing cycle of the subscription and to learn more about billing_cycle_anchor please visit here.
You can achieve it with proration_behavior and billing_cycle_anchor.
You only need to pass these two properties while updating your subscription details using stripe API.
subscription = Stripe::Subscription.retrieve('sub_49ty4767H20z6a')
Stripe::Subscription.update(
subscription.id,
{
cancel_at_period_end: false,
proration_behavior: 'none',
billing_cycle_anchor: 'now',
items: [
{
id: subscription.items.data[0].id,
price: 'price_1LcBtVHhBkzUOaGo0gvFmaMS'
}
]
}
)

Create a fully onboarded stripe connect account for testing purposes?

I'd like to be able to quickly run code in automated tests and development to create a payment:
Stripe.api_key = 'sk_test_51HYHSFGtUKse83O9J4QeAib3cp8sHzGaOQRrnwvnghEzuYQKUCKEP3CHE3AIHe5ModevMK7TVAUCyJU0ADSwIUoX00qxZmBI9r'
session = Stripe::Checkout::Session.create({
payment_method_types: ['card'],
line_items: [{
name: 'Kavholm rental',
amount: 1000,
currency: 'aud',
quantity: 1,
}],
payment_intent_data: {
application_fee_amount: 123,
transfer_data: {
destination: '{{CONNECTED_STRIPE_ACCOUNT_ID}}',
},
},
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/failure',
})
but note that it needs a CONNECTED_STRIPE_ACCOUNT_ID
The problem here is you have to manually go through the browser to create it. Very annoying for automated testing and development if you're constantly seeding new data.
How can I create a connected stripe account using code (rather than going through the browser)?
The answer depends on the type of Connect Account you're using, but the only flavor that would allow that is a Custom Account. If you're using Standard or Express, it makes more sense to specifically create - and reuse - test accounts for this purpose.

How to integrate SCA Stripe Checkout with Stripe Connect using Rails 5

I've been trying to enable payments to users (sellers in this case) on my site. I've managed to allow the seller to set up their own Stripe account, and then their Stripe account ID is saved in the database.
However, I can't manage to allow people to pay the sellers. I've been looking at the Stripe docs and although very detailed, I can't figure out where everything is meant to go. I would like something like this:
I have got a new charge view, and this code is supposed to be in it I think:
// Initialize Stripe.js with the same connected account ID used when creating
// the Checkout Session.
var stripe = Stripe('pk_test_CscsJfPdlaNBpLla09y0aA6W', {
stripeAccount: '{{CONNECTED_STRIPE_ACCOUNT_ID}}'
});
stripe.redirectToCheckout({
// Make the id field from the Checkout Session creation API response
// available to this file, so you can provide it as parameter here
// instead of the {{CHECKOUT_SESSION_ID}} placeholder.
sessionId: '{{CHECKOUT_SESSION_ID}}'
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
});
I've also got a Charges Controller, but I'm not sure if this is meant to be there:
session = Stripe::Checkout::Session.create({
payment_method_types: ['card'],
line_items: [{
name: "Cucumber from Roger's Farm",
amount: 200,
currency: 'gbp',
quantity: 10,
}],
payment_intent_data: {
application_fee_amount: 200,
},
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
}, {stripe_account: '{{CONNECTED_STRIPE_ACCOUNT_ID}}'})
How do I Initialize Stripe.js with the right account ID? Where is this code meant to be? Do I need more?
Any help would be massively appreciated. Thanks.

Paypal recurring payments with variable amount 2 (in Spain)

some days ago, I wrote this post regarding Paypal recurring payments with variable amount Paypal recurring payments with variable amount
I marked it as fixed, however it is not.
First, I decided to develop approach 1 (by removing old profile and creating a new one). This worked. But, after that I realized, that I didn´t cover all my requirements. So finally, the right approach for me is number 2. This means, as #Andrew Angell suggested, I will develop a custom billing system that bills the customers. For that, I will create Billing Agreements and I will use the returned ids to execute Reference transactions with the amount I need and whenever I need. So far, it´s right?
According to Paypal docs, this is possible: https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECReferenceTxns/
So, I´m trying to follow the steps, so first a execute a setExpressCheckout:
# Paypal setExpressCheckout
def setExpressCheckout(billingType, returnURL, cancelURL, price, description)
#api = PayPal::SDK::Merchant::API.new
if billingType == "credit-card"
billingType = "Billing"
else
billingType = "Login"
end
#set_express_checkout = #api.build_set_express_checkout({
SetExpressCheckoutRequestDetails: {
ReturnURL: returnURL,
CancelURL: cancelURL,
LocaleCode: "US",
LandingPage: billingType,
PaymentDetails: [{
NotifyURL: returnURL,
OrderTotal: {
currencyID: "EUR",
value: price
},
ShippingTotal: {
currencyID: "EUR",
value: "0"
},
TaxTotal: {
currencyID: "EUR",
value: "0"
},
PaymentDetailsItem: [{
Name: description,
Quantity: 1,
Amount: {
currencyID: "EUR",
value: price
},
}],
PaymentAction: "Authorization" # To authorize and retain the funds, and when booking is confirmed capture them.
}],
BillingAgreementDetails: [{
BillingType: "MerchantInitiatedBillingSingleAgreement",
BillingAgreementDescription: description
}]
}
})
# Make API call & get response
#express_checkout_response = #api.set_express_checkout(#set_express_checkout)
# Access Response
if #express_checkout_response.success?
#token = #express_checkout_response.Token
puts "setExpressCheckout completed OK :)"
#paypal_url = #api.express_checkout_url(#express_checkout_response)
else
puts "setExpressCheckout KO :("
#express_checkout_response.Errors
puts "#express_checkout_response=" + #express_checkout_response.inspect
end
#express_checkout_response
end
However, I get this error:
#LongMessage="Merchant not enabled for reference transactions", #ErrorCode="11452"
It´s quite clear, just need to contact Paypal support guys and ask them to enable Reference transactions in my Paypal sandbox account. Right? I already did it and I´m just waiting.
However, what really worries me, is that I called Paypal support and they told me that this approach will not work in Spain. Although it´s in the doc, it´s only working in UK. Is this true?
If it´s true, I´m really in a trouble, because as far as I know, Paypal doesn´t not support subscriptions with variable amount.
Paypal technical support guy has enabled my sandbox account to reference transactions. I have developed the logic and it´s working. At least, in Sandbox and in Spain.
So, I will assume, it works.
Bad the Paypal guy on the phone that told me it wasn´t possible.

Resources