Page 1 of 2
Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 4:37 pm
by simonmlewis
I am setting up a website that takes one simple subscription (tho maybe two at some stage).
I am doing it on a custom build, not woocommerce.
To get the option to make the payment, they have to register first. I don't need their address details.
Then I want them to click a button that stores the value of their order into a SESSION.
Because there will only be ONE thing to "buy", this session cannot be added to anyway.
Obviously when they have done that I can show item in Cart myself in PHP.
But the query is, how in the Cart do I then take them to PayPal to pay, without using the "cheap" 'subscribe' buttons you build in PayPal. I want to do it the smart way they do in WooCommerce. But how does it do it?
When you setup Woo, you just pop in your PayPal email address, but nothing else. And when you add to cart and then go to PayPal to pay, you see what is in your cart as it has somehow passed over. But how do they do it?
Does Woo permanently run and connect with PayPal to ensure payments have gone through, as I need to check each month money has passed over.
My other option is Go Cardless, but that doesn't allow month one to be free. So I am open to ideas.
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 4:53 pm
by Celauran
simonmlewis wrote:When you setup Woo, you just pop in your PayPal email address, but nothing else. And when you add to cart and then go to PayPal to pay, you see what is in your cart as it has somehow passed over. But how do they do it?
Probably a form that posts to PayPal. Embed the items in the form, set paypal itself as the form's action, and you're sent off there to process the payment. Take a look at their developer documentation, especially IPN.
simonmlewis wrote:I need to check each month money has passed over.
Last time I used recurring subscriptions with PayPal, they sent notifications to your designated endpoint every time a subscription renewed, so you can treat the renewals exactly like you would the initial purchase.
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 5:16 pm
by simonmlewis
So how do I dictate that month one is free, as you can do that in woocommerce. There must be a setting or entry in a form?
I did look at APIs but it went right over my head.
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 5:32 pm
by Celauran
simonmlewis wrote:So how do I dictate that month one is free, as you can do that in woocommerce. There must be a setting or entry in a form?
https://developer.paypal.com/docs/class ... 91EB0901HT
If you set period1 to one month and mc_amount1 to 0?
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 5:42 pm
by simonmlewis
Oh wow. But how does all that get compiled into a form button, as is it hidden in some way?
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 5:44 pm
by Celauran
It doesn't go into a button, it goes into the form itself in hidden fields.
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 5:51 pm
by simonmlewis
Can you show me an example you know of..as I don't get it. I get that it has to be hidden fields, but no idea where or how.
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 6:09 pm
by Celauran
Obfuscated some old code I had lying around. Can't promise it's perfect, but it should give you the general idea.
Code: Select all
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input name="cmd" value="_xclick-subscriptions">
<input name="item_name" value="<?= $item->name; ?>">
<input name="item_number" value="<?= $item->number; ?>">
<input name="currency_code" value="USD">
<input name="business" value="you@example.com">
<input name="notify_url" value="https://example.com/paypal/notify">
<input name="return" value="https://example.com/payment/thankyou">
<input name="cancel_return" value="https://example.com/payment/cancel">
<input name="period3" value="1M">
<input name="mc_amount3" value="<?= $item->price; ?>">
<input type="submit" value="Pay with PayPal">
</form>
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 6:17 pm
by simonmlewis
So period2 is the first of the priced values and period1 is zero?
Re: Cart/Subscription Advice needed
Posted: Thu Mar 17, 2016 7:23 pm
by Celauran
If they get one free month and then every subsequent month is full price, I'd just use period1 and period 3
Re: Cart/Subscription Advice needed
Posted: Fri Mar 18, 2016 3:46 am
by simonmlewis
Code: Select all
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input name="cmd" value="_xclick-subscriptions">
<input name="item_name" value="<?= $item->name; ?>">
<input name="item_number" value="<?= $item->number; ?>">
<input name="currency_code" value="GBP">
<input name="business" value="you@example.com">
<input name="notify_url" value="WHAT IS THIS FOR?">
<input name="return" value="https://example.com/payment/thankyou">
<input name="cancel_return" value="https://example.com/payment/cancel">
<input name="period3" value="1M">
<input name="mc_amount3" value="<?= $item->price; ?>">
<input type="submit" value="Pay with PayPal">
</form>
So this would generate a button, and give one month free, and then the value of mc_amount3 would be charged thereafter.
Item Number I assume can be anything.
What is "notify_url"? Been reading about it. Sounds like it might be some sort of "backend" PHP file I can use to trigger at my end, rather than to take the user somewhere?
So Return URL would go to a "thanks for your payment" page, while notify url might just trigger "update db and tell the system they have paid". Which return URL could do... could it not?
Re: Cart/Subscription Advice needed
Posted: Fri Mar 18, 2016 6:25 am
by Celauran
simonmlewis wrote:Code: Select all
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input name="cmd" value="_xclick-subscriptions">
<input name="item_name" value="<?= $item->name; ?>">
<input name="item_number" value="<?= $item->number; ?>">
<input name="currency_code" value="GBP">
<input name="business" value="you@example.com">
<input name="notify_url" value="WHAT IS THIS FOR?">
<input name="return" value="https://example.com/payment/thankyou">
<input name="cancel_return" value="https://example.com/payment/cancel">
<input name="period3" value="1M">
<input name="mc_amount3" value="<?= $item->price; ?>">
<input type="submit" value="Pay with PayPal">
</form>
So this would generate a button, and give one month free, and then the value of mc_amount3 would be charged thereafter.
You'd need to add period1 and mc_amount1 for the free month. Also, in my haste, I had omitted type="hidden", which you will definitely want on the form elements. Remember PayPal has a Sandbox you can use for testing these transactions and their responses, so if everything isn't perfect out of the box, it's not a big deal.
simonmlewis wrote:Item Number I assume can be anything.
Yes. That's a product ID on your end. Lets you look up the price and confirm it matches the payment amount, update stock (not necessary in your scenario), etc.
simonmlewis wrote:So Return URL would go to a "thanks for your payment" page, while notify url might just trigger "update db and tell the system they have paid". Which return URL could do... could it not?
That's exactly right and yes, they can be the same URL if you like.
Re: Cart/Subscription Advice needed
Posted: Fri Mar 18, 2016 6:39 am
by simonmlewis
Yes. That's a product ID on your end. Lets you look up the price and confirm it matches the payment amount, update stock (not necessary in your scenario), etc.
What difference would that make?
I don't have the subscription in a database table. Because it's one subscription, it didn't seem worth it. So it would be hard coded.
So how would I say "is this £10" that's being paid... ?
Re: Cart/Subscription Advice needed
Posted: Fri Mar 18, 2016 6:46 am
by Celauran
You'll still want to check that the payments you're receiving are for the right amount. Forms can be edited. If I replace 10.00 with 0.01, you need to make sure I don't get a subscription.
Re: Cart/Subscription Advice needed
Posted: Fri Mar 18, 2016 8:34 am
by simonmlewis
Yes I get that, but how do I query on return, that they have paid 10.00?
Does it put that value into the URL somehow??
How does I make it check the value (since its not in a database... altho as I say, it could be if needed).