The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Integrating with PayPal
#1
What is the best method for integrating PayPal's REST SDKs with userspice to achieve the following functionality?

The user (who is logged in) upgrades their account to a premuim account by making a payment through PayPal.
Paypal handles the transaction and then sends data back to userpice which is validated and then recorded in the users table identifying them as a premium user.

Thanks for any recommendations you might have.
  Reply
#2
I realised my question wasn't very detailed or precise.

I am using a folder structure that looks like this:

/users/
|--paypal/
|
|--checkout.php

I want to be able to somehow require the init.php file in the checkout.php so that I can access the db and update a table that I've created in the database called paypal_transactions.

Here's a hastebin of the checkout.php file

https://hastebin.com/bimerarixa.xml

I developed this code following a tutorial, so i'm quite sure that there is a better way to insert into the database than what is shown in the file.

Thank you for any guidance you might have!
  Reply
#3
You're exactly right about requiring the init. I've never used the paypal, API before but I can give you some pointers.

I would go right up to line 2 and
Code:
require_once 'users/init.php';

Then if you go down to the bottom, it looks like you're getting 3 things back...
user_id
payment_id
hash

I can't tell if that user_id is the same as userspice's user id or if that is paypal's user id.

Either way, I would make 5 columns in the database
paypal_id
payment_id
hash
premium
premium_expire

All varchar(255 or whatever) except the last one which should be date.
You can obviously make this fancier, but that's how I would do it to get started.

Then, right after that catch statement on line 88 I would add my update query

Code:
$expiry =
//Your math to add however many days to todays date in your date format

Code:
$fields = array (
Code:
'paypal_id'      => $_SESSION['user_id'],
Code:
'payment_id'     => $payment->getId(),
Code:
'hash'           => $hash,
Code:
'premium'        => 'premium',
Code:
'premium_expire' => $expiry,
Code:
);

//update the logged in user's account info
Code:
$db->update('users',$user->data()->id,$fields);

Here's that last bit on hastebin....
https://hastebin.com/furerinaqe.php

  Reply
#4
Thank you for very much for your reply. I haven't had an opportunity to implement your additions full yet. But I wanted to add a few things to my explanation.

There are two parts to the PayPal transaction. The first part (which is what I'm trying to achieve here) is to capture the user's information when they click the "buy now" button for example.

So after clicking the button, two things happen:

1) the user is taken to an external paypal checkout where the transaction is completed (user inputs card details or pays with paypal account)

2) Transaction information is captured in a separate table called transactions_paypal. The $hash = md5($payment->getID()) uses paypal's getId() method to grab an identifier of the payment being created which is then stored in a session (after being sent to paypal).

So the user_id (userspice), payment_id (paypal) and hash are stored in transacitons_paypal.


When the user completes the transaction at paypal they are redirected back to my site (another page like pay.php) where the payment_id is verified against the database and the then the rest of the action can take place (user becomes premium and is assigned an expiry period).

The other thing is that I for some reason just can't seem to require the init.php. From the error log:

[17-Jan-2017 10:40:28 UTC] PHP Warning: require_once(users/init.php): failed to open stream: No such file or directory in /home/hat3tank/public_html/users/paypal/checkout.php on line 3
[17-Jan-2017 10:40:28 UTC] PHP Fatal error: require_once(): Failed opening required 'users/init.php' (include_path='.:/opt/alt/php55/usr/share/pear:/opt/alt/php55/usr/share/php') in /home/hat3tank/public_html/users/paypal/checkout.php on line 3

Any tricks for finding file with a direct path?

Thanks for all your help with this. Apart from being an awesome product, the support is amazing. Well done!
  Reply
#5
Can you send me a copy of the file that paypal redirects back to? Like your pay.php? Can you copy/paste it to pastebin/hastebin?

You should be able to
Code:
require_once 'http://yourdomain.com/users/init.php';

You don't HAVE to use the relative paths
  Reply
#6
Apologies for the very slow response.

Here are the two files,

The checkout file: https://hastebin.com/rurexejugo.xml

The pay file: https://hastebin.com/ucixebamer.xml

There is probably a much cleaner way of doing this so I'm open to some guidance.
  Reply
#7
I've updated the pay.php file.

http://pastebin.com/mV1Akva8

Now instead of having to create a new column in the 'users' table, I added a new permission level called 'premium' and use the pay.php file to insert the new permission in the user_permission_matches table.


  Reply
#8
Sorry. I've been off on other projects. Do you still need help with this?
  Reply
#9
All good, just wanted to post the code in case someone else finds it handy. If you see any glaring errors I'd appreciate your critique, but otherwise no help required.
  Reply
#10
I'm the someone else that is going to find this very handy. Thanks for your work gathr! The pay file looks great, would you mind sharing the checkout file? I don't see it at https://hastebin.com/rurexejugo.xml

Thanks again!
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)