Cart/Subscription Advice needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Cart/Subscription Advice needed

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cart/Subscription Advice needed

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cart/Subscription Advice needed

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cart/Subscription Advice needed

Post 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?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cart/Subscription Advice needed

Post by simonmlewis »

Oh wow. But how does all that get compiled into a form button, as is it hidden in some way?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cart/Subscription Advice needed

Post by Celauran »

It doesn't go into a button, it goes into the form itself in hidden fields.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cart/Subscription Advice needed

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cart/Subscription Advice needed

Post 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>
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cart/Subscription Advice needed

Post by simonmlewis »

So period2 is the first of the priced values and period1 is zero?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cart/Subscription Advice needed

Post by Celauran »

If they get one free month and then every subsequent month is full price, I'd just use period1 and period 3
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cart/Subscription Advice needed

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cart/Subscription Advice needed

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cart/Subscription Advice needed

Post 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... ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cart/Subscription Advice needed

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Cart/Subscription Advice needed

Post 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).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply