BASIC Smart Payment buttons integration help

Donquick
Contributor
Contributor

Hello,

 

Objective

I want to put Smart Payment buttons on my website (php).  There are no physical products, I just need to add some download links to the individuals account once they have paid.  Ideally, all the work gets done by the buttons and then when I get into the onApprove function, I want to redirect to a confirmation page on my website (if it can $_POST at the same time, all the better).  I will then do some changes to the database using my $_SESSION data (to make the customers products visible in their account - i.e. available for them to download).

 

Where I am now:

I am following this set of instructions:  https://developer.paypal.com/docs/checkout/integrate/# headed 'Basic Integration Paypal Checkout'.

 

I have got as far as step 5.  It works!  My test buyer account is debited, my test seller account is credited and I get the javascript alert window saying 'Transaction completed by ...'.

 

OK, now i am stuck.  Step 6 has a client side and a server side process. 

 

CLIENT: Of course, I can do the changes to the checkout code, but i do not understand the "fetch('/paypal-transaction-complete', ".  Do i need to make a file called 'paypal-transaction-complete' (what is the extension, this looks like a directory reference not a file reference to me) or is this something that is already part of paypal?  If i do need to make this file, I wonder what is in it.  

 

SERVER:  I must upload the SDK to my server or not?  There is a box that says - "If you are calling the API directly, you do not need to install the SDK."  I think this sounds appealing, but how do i know if I am calling the API directly, what do i do instead of the server part of step 6 if i am calling the API directly - there are no alternative instructions for not installing the SDK.  

 

The instructions for uploading the SDK here: https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/#

also imply it is optional.  [Another problem: The first step on the SDK installation implies some sort of direct console/shell to the server.  I don't know how i would do that, I just have a hosting service with 1 & 1.  I don't know if they allow installations direct console activity etc.].

 

Summary and question

I think i understand that the client should report transaction success to the server and record the results in the database and that that requires the SDK to be installed, but i think that this is for my benefit rather than a compulsory paypal process and it is something i would be willing to forego.  My risks are low - very low price items, very low volume of transactions that I can check manually in Paypal to see if the payment has been received etc. 

 

I would be quite happy to get a success response from the paypal overlay window and then proceed to a redirect on my site and do all the database work there, but i would like to pass some data.  I am happy to bypass the server side processes and/or communicate with the API directly without installing the SDK if this is possible.  However, i don't see the instructions for this (instructions including what to name files, where to put them and how to modify the php code in my checkout file to do this).

 

Many thanks for help.

Login to Me Too
14 REPLIES 14

shawnz
Contributor
Contributor

CLIENT: Of course, I can do the changes to the checkout code, but i do not understand the "fetch('/paypal-transaction-complete', ".  Do i need to make a file called 'paypal-transaction-complete' (what is the extension, this looks like a directory reference not a file reference to me) or is this something that is already part of paypal?  If i do need to make this file, I wonder what is in it.  


Yes, that's right. You make a URL on your server called "paypal-transaction-complete" which accepts a POST request containing the orderID parameter you received in the result of the "actions.capture()" function. Then you use the fetch function (or any other method, for example XMLHttpRequest) to send that orderID to that URL on your server.

 

The name doesn't have to be "paypal-transaction-complete", it is up to you what you name the endpoint. In your case you might want to name it "paypal-transaction-complete.php" since it is conventional for PHP apps to have each URL represented by a seperate PHP file. However other web development frameworks are not usually like that which is probably why they didn't put any extension for the example.

 

In this file, you will communicate with Paypal from your backend server (no client involvement for this part of the process) to check that the orderID received from the client is actually real and represents a successful transaction for the amount of money which you actually expected. This prevents the client from faking the orderID. If it's good, then you release the product to the user. You can see a rough example of how to use the PHP SDK to get the order information from the orderID on this page: https://developer.paypal.com/docs/checkout/integrate/#6-verify-the-transaction . From there, just check that the information received is what you expected, and if it is, release the product.

 

This is not the only way of doing this process -- you could also do a redirect to another page on your server, passing the orderID to the new page, and then do the server-side validation there. They just chose to use an ajax-based approach in the example, but that's not strictly necessary.

 


SERVER:  I must upload the SDK to my server or not?  There is a box that says - "If you are calling the API directly, you do not need to install the SDK."  I think this sounds appealing, but how do i know if I am calling the API directly, what do i do instead of the server part of step 6 if i am calling the API directly - there are no alternative instructions for not installing the SDK.  

 

The instructions for uploading the SDK here: https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/#

also imply it is optional.  [Another problem: The first step on the SDK installation implies some sort of direct console/shell to the server.  I don't know how i would do that, I just have a hosting service with 1 & 1.  I don't know if they allow installations direct console activity etc.].


You are correct that the SDKs are optional. The new Paypal API is designed as a REST API which means every action is performed by doing an HTTP request. So, rather than using the SDKs, you could communicate with the API directly by issuing HTTP requests from your server to the correct paypal API URLs described in the documentation. For example, to get the order info for a certain order ID you could issue an HTTP GET request to www.api.paypal.com/v2/checkout/orders/{order_id}, as shown here: https://developer.paypal.com/docs/api/orders/v2/#orders_get . However, using the APIs manually in this way requires a somewhat complicated process of authenticating with Paypal using OAuth, whereas that is taken care of for you when using the SDK.

 

In the example they show how you can acquire the API with PHP's "Composer" dependency management system, but if you do not have shell access on your server then you will not be able to run Composer on it. However that is not the only way to get the API, you could also just download the files from here: https://github.com/paypal/Checkout-PHP-SDK/releases . You could also use Composer on your own computer, and then copy the files to your server by your normal process.

 

Let me know if that helps,

 

Shawn

Login to Me Too

Donquick
Contributor
Contributor

Hi Shawn,

 

Thank you so much for the reply.  I have spent some more hours wildly trying things at random and have progressed no further.  If you have time, can i ask for some more help please [example code is good, if you have it].

 

Can i break it down into two steps.  A) The verification on the server side, the response and redirect.  B) Whether i need the SDK and how to install it if i do.

 

A) The verification on the client and server side, the response and redirect

 

Specifically can we focus on this please - "From there, just check that the information received is what you expected, and if it is, release the product".  this is where i could really use some guidance and example.

 

For info, my buttons and the call are in a file called checkout.php. 

 

In the sample code I see within onApprove an alert and a fetch.  The alert works and the fetch, I have no idea.  So presumably 100/100 people want to redirect their customer to another URL after a successful payment process and 0/100 actually want to display this alert window - i.e. i guess this alert is for test purposes only.  Also the alert is before the fetch anyway, so i guess that ultimately the alert gets removed?

 

I have created a file called paypal-transaction-complete.php and uploaded it and changed my checkout.php file to say "return fetch('/paypal-transaction-complete.php', {".    As you explained, the fetch should post the orderID to the php and return a response. 

 

I have put the code from part 2 of step 6 of the instructions into my paypal-transaction-complete.php file.  [The code that starts with "<?php namespace Sample; require __DIR__ . '/vendor/autoload.php';.... "].  This code is clearly reliant on other includes and things, so it's not going to do anything until I have installed the SDK, right?  However, interestingly, I do not see any errors when i run this.  I guess that the errors and warnings are happening serverside or my path is wrong.

 

Questions:

 

1)  Is it the code in the example that goes into the paypal-transaction-complete.php file or is it something entirely different that i need to write.  If the latter, do you have some sample code please?

2)  Am i correct in thinking that i process my database work and do my redirect inside my original checkout.php file after i have got the return from the fetch OR does the paypal-transaction-complete.php do this work.  I think the former as the paypal-transaction-complete is just a server side thing, right?

3)  If it is my checkout.php where i do the work after the positive response from the fetch, what is the response from the fetch, a json encoded string?  So, this is the "From there, just check that the information received is what you expected, and if it is, release the product" question!

 

  • Where in checkout.php do i put code to check the response;
  • what does apositive response look like;
  • How do i redirect?  header('Location: confirm.php');  or a hidden form with some post data and java submit or something i guess?

This is my checkout.php page at the moment - please, where do i put what i want to process the response and redirect?

 

<div id="shopping-cart">

<?php
if(isset($_SESSION["cart_item"])){
$total_quantity = 0;
$total_price = 0;
?>

<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
Confirm Order
Total Cost
</tr>

<?php
$total_price = 0;
foreach ($_SESSION["cart_item"] as $item){
$total_price += ($item["price"]*$item["quantity"]);
}
?>

<tr>
<td>We are ready to process your order. Please acknowledge the terms and conditions and continue to payment.</td>
<td align="left" colspan="1"><strong><?php echo "£ ".number_format($total_price, 2); ?></strong></td>
</tr>
</tbody>
</table>

<div id="validate_payment"><input type="checkbox" name="tandc" id="tandc" value="" onclick="validate_payment()"> I accept the terms and conditons and wish to proceed to purchase.<br></id></div>


<div class="align-right">
<div id= "paypal_screen" title="Please tick terms and conditions box to proceed."><img src="images/screen.jpg"></div>
<div id="paypal_button" class="align-right">

<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>

<!-- Include the PayPal JavaScript SDK -->

<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: ''
}
}]
});
},

// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
return fetch('/paypal-transaction-complete.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
});
});
}
}).render('#paypal-button-container');
</script>
</div>
</div>
<?
} else {
?>

<div class="no-records">Your Cart is Empty. Sorry, you shouldn't be here, please choose from the menus.</div>

<?php
}
?>
</div>

 

  1. B) Whether i need the SDK and how to install it if i do.

 

I think i can possibly muddle through A) on a good day, but where i really need help is this area.  Let's assume that i am going to install the SDK, as you say that the other alternative involves a complicated process.  How anything could be more complicated that this, I cannot image; but that's my problem 🙂

 

So i need to install the SDK.  I have tried:

 

1) following the instructions on this page: https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/#

 

I get as far as the first step:

 

cap4.JPG

 

Put simply, I don't know where i type this command.

 

2) following the 'Using composer' instructions on this page: https://github.com/paypal/PayPal-PHP-SDK/wiki/Installation .  I can get as far as this screen after downloading composer-setup.exe:

 

cap1.JPG

 

I don't have php on my PC I have it on my 1&1 webserver so i don't know how to progress.

 

2) following the 'Using direct downloads' instructions on the page.  I get as far as 'Go to your project directory', step 2.  

 

cap2.JPG

 

How can i be in a position where i have a command line console on my 1&1 webserver?  Or can i just create a directory in FileZilla and copy the files there? I wonder, does it have to be called 'project'.  I don't really want all that stuff in my root directory with my php and css files.

 

3) I tried downloading putty.  Completely baffling:

 

cap3.JPG

 

Uncertainty about the folder structure in the SDK: 

There are various Zip files on various websites.  I have downloaded the zip from here: http://paypal.github.io/PayPal-PHP-SDK/docs/ also from here: https://github.com/paypal/Checkout-PHP-SDK  I can't help but notice that in the sample code (in paypal-transaction-complete), there are references to '/vendor/autoloader.php' and Sample\PayPalClient and PaypalCheckoutSdk\Orders...  There are no such locations in the zip file directory structure that I have , so i think that this is going to fail anyway.

 

Conclusion

Well, if you are still with me then thank you again.  If you do manage to help me, then i will post a step by step guide for all web novices that can do a bit of php/java/html but have no idea about APIs.  So you will be helping a big group of people in the long run.  Thanks again.

 

 

 

 

Login to Me Too

shawnz
Contributor
Contributor

Hi again Donquick.

 

I'll start with your part A questions.

 

1) Yes, that code from the "step 6 - verify the transaction" page should go in the paypal-transaction-complete.php file (or whatever you name it). However as you noticed it depends on files from the SDK. I strongly recommend you use the SDK, but I think you might have the wrong one which is causing part of your troubles here. More on that later.

 

2) In the particular flow I was describing above (which isn't the only possibility, but it seems to be what the docs recommend) the database work would be done in the paypal-transaction-complete file. Let me describe the general flow:

 

- The customer loads the checkout.php page on your server. It doesn't need any PHP code in it except to get the price they owe. Mainly it just contains javascript code that runs in the customer's browser.

- The customer clicks the paypal button which calls your createOrder function, which calls actions.order.create(...) to communicate with paypal and create and return an orderID (which is basically like an invoice describing how much the customer has to pay). This all happens between the client's browser and paypal, no involvement with your server.

- The paypal button code takes the returned orderID and pops up a checkout screen where the customer can complete a checkout for that order.

- Once they complete the checkout then that order will now be "approved" and your onApprove function will be called. The next step is to call actions.order.capture() which will accept the money from the client and put it into your account.

- Now is where your server comes into play: After the capture completes successfully, you send an ajax request to your own server (the paypal-transaction-complete.php page) which contains the orderID. Then, PHP code in the paypal-transaction-complete.php page uses the PHP SDK to communicate directly between the server and paypal to get the money paid for that orderID and verify it against what you expected the client to pay. If it matches, then you save in your database that the client made a successful order and should receive the product. This all happens in paypal-transaction-complete.php

- Finally once paypal-transaction-complete.php is finished executing, the ajax request will finish and control will be returned to the javascript code running on the client's browser. The server-side portion of the work is finished. Now you redirect to the success page (or whatever) with javascript. You might do something like window.location = "/success.php" or something which is how you would do a redirect with javascript.

 

3) Checkout.php is not where you do the database work as I describe above. So the response to the fetch will be either successful or not successful and that's all you'll care about, which you can check with res.ok as I will describe in a moment.

 

Here is an example of how your checkout.php file might look: https://pastebin.com/EYkhWaw2 . I am not sure if this is totally accurate but maybe it can serve as a starting point.

 

Part B.

 

For starters, I think there is some confusion here about which SDK is which, so let me go over what's available (to my knowledge -- I'm not totally sure about all this myself.) Note that I have actually been working with the Java SDK myself, but it seems like the situation with the PHP SDK is basically the same.

 

- First of all, there is the Checkout-PHP-SDK. This is the latest and greatest product and it's what allows you to access the Orders V2 and Payments V2 REST API. So far it is all you will need with this integration (although it could get complicated if you need any more advanced features, see below). Here is the homepage for that product: https://github.com/paypal/Checkout-PHP-SDK

 

- Next there is the PayPal-PHP-SDK, which is actually an older product for the previous REST v1 APIs. From what I can tell, this SDK is deprecated for orders and payments usages, but some functionality like webhooks and dispute handling is only available in this older SDK. So there are some more advanced cases where you might need to use BOTH SDKs. But I don't think you will face that situation yet. This older SDK has a significantly different design from the newer one. Here is the homepage for this older SDK: https://github.com/paypal/PayPal-PHP-SDK

 

- Then there is "PayPal-PHP-SDK 2.0-beta" which has the features of the older SDK, but with a design more like the newer SDK. However it looks like it is no longer maintained so I have avoided this one. I will not be referring to this one in my post, but be aware that it exists. See: https://github.com/paypal/PayPal-PHP-SDK/tree/2.0-beta

 

OK. Now I will try to answer your part B questions.

 

1) I think it would be best if we just forget about composer for your situation. But if you did want to pursue the composer option, then you would type that in your server's shell or your computer's terminal depending on where you have composer installed.

 

2) That's right, as you can see you need PHP installed to be able to install Composer. Sorry, I didn't really think about that requirement at first. You could install PHP for windows but that might get complicated. If you do use that approach then you could run the "composer" command from the windows terminal. However again I think it will be easier if we just go with the zip file approach for now.

 

2 again) So as I explained above the page you linked here is actually the old PayPal-PHP-SDK, not the new Checkout-PHP-SDK which we want. This may be why the files you were seeing disagreed with what you saw in the examples. But to answer your question, you can run those commands on your own computer (in the terminal). Or you can just make the directory in filezilla by clicking "new directory", you don't have to use the mkdir command to do it like it suggests. And the directory does not have to be called "project".

 

3) Forget about PuTTY for now, if you do not have shell access to your web server then it will not be useful for you.

 

Uncertainty about folder structure: This is partly due to you having the wrong SDK here again. There is also another detail here which is that "vendor/autoload.php" is a Composer specific file and you won't be using that if you aren't using Composer. Just make sure that the PayPalCheckoutSdk\Orders folder is in the right place and get rid of that autoload.php line.

 

I know I didn't cover everything here but hopefully this lets you make some progress. Let me know if that helps any more

 

Shawn

Login to Me Too

shawnz
Contributor
Contributor

Sorry, one more note, it looks like if you are acquiring the SDK manually then you will ALSO need to grab this library which it depends on: https://github.com/braintree/braintreehttp_php/releases

 

Again just unpack the "lib" folder together with the checkout SDK.

 

EDIT: Also note we want to be using the zip files available under the "releases" page, not the zip file which you get by clicking "clone or download".

Login to Me Too

Donquick
Contributor
Contributor

Hi Shawn,

 

Thanks for your time and effort, I can see that this has taken you a long time.  I must say that it is very much appreciated.  For my part i have had another 10 hour day on this and made almost no progress.

 

Thanks for the details of the checkout.php code.  After a bit of tweeking, that is now working - it looks in paypal-transaction-complete.php and then returns control.  I'm not sure how we are supposed to know how to do this ".then(function(res))" part without relying on kind souls such as yourself.  I simply can't imaging why they don't give this as the example code as it is such a very likely scenario.  Anyway, moving on!

 

SDK!

Today I managed to get Putty working and tried to follow this: https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/#

I typed the word composer in every location i could - 'no such command'.  Abandoned after a few hours and thought i would try to just manually copy the files from the zip instead.

 

On the link that you sent:  https://github.com/paypal/Checkout-PHP-SDK there is a zip, as you say.  I downloaded that and copied the content into a folder that i called PapPal-PHP-SDK in my main folder where the php and css files are.  After that i looked at the sample code for the paypal-transaction-complete.php file. 

 

First line:  require __DIR__ . '/vendor/autoload.php';

 

Well there is no such folder and no such file in the file structure that i downloaded from the zip, so i guess that is never going to work.

 

I found a different zip here:  https://github.com/paypal/PayPal-PHP-SDK/releases

which does have an autoload.php file, so i deleted the original folder structure and copied this zip content into PayPal-PHP-SDK instead.

 

However, I notice that we have not defined ___DIR___ and there is no folder called vendor, so still a fundamental failing that means that it is not going to work.

 

Thanks for the supplementary comment.  I'm afraid that "Again just unpack the "lib" folder together with the checkout SDK" is not clear enough for me.  There is a folder called braintreehttp_php-0.3.0 in the zip and this contains files and a folder called lib.  If i try to put this content in the 'PayPal-PHP-SDK' folder then i will be overwriting certain files.  I guess I need to make another folder in the root next to the first one?

 

Questions please 🙂

1)  After putting files on my server from the zip files, does that mean that i have installed the SDK or do i need to then try to carry out the tasks listed here: https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/#  

a)  If the copy is the install then great, I have some hope.

b) if I then need to do all that extra stuff, then i'm going to throw the towel in.  Just reading 'Set Up The Environment', I can see that the instructions are too vague for me to follow.  'create a file' without saying what to call it or how it is going to get called. etc.

 

2)  Does the sample code for paypal-transaction-complete.php need editing significantly?  As i mentioned above, I see immediate problems with paths to files that do not exist on either SDK folder structure.  I have changed "require __DIR__ . '/vendor/autoload.php';" to "require '/PayPal-PHP-SDK/autoload.php';"  Is this a good idea - half of me thinks that the change is required, the other half thinks that Paypal surely wouldn't give sample code that fails on line 1 for all situations. 

3) I have tried to run the code all sorts of different ways adding lines / taking them out / changing paths, you name it - i can't get it to work.  Any command that i put in after the "require" line doesn't work.

 

4) The sample file sets up a class call GetOrder with a function called getOrder(), but nowhere in my checkout.php do i create a Get Order object nor call a function called getOrder, so how is this ever going to work.

 

Well, there it is - I am very close to giving up.  If you have the time and the inclination I think the only way ahead would be step-by-step instructions for installing am SDK and editing the papypal-transaction-complete.php file - like you would talk to a complete moron.  "Copy this file to a folder called XYZ in your root folder.  Type this in here etc.  Edit that line of the php to say this instead.  After this line is where you add your database insert statement, etc. etc.   Without that, I am not going to be able to do this.  If you wany paying, that's cool too.

 

P.S. here below is my code at the moment.  As you see i am using an sql insert just to see how far i am getting.  At the moment the first instance is working and i get an extra line in the table and the second one isn't - the code fails at the 'require' line. I have a folder with all my phps and css file and in that i have a folder called PayPal-PHP-SDK whihc has the content shown in the image below.

 

Cheers, Don

 

cap5.JPG

 

<?php

namespace Sample;


include_once 'database.php';
include_once 'phpfunctions.php';
$resultadd = mysqli_query($link,"INSERT INTO user_products VALUES ('','1','3','','','','','','')") or die("Error ".__LINE__);


//require __DIR__ . '/vendor/autoload.php';
require '/PayPal-PHP-SDK/autoload.php';

include_once 'database.php';
include_once 'phpfunctions.php';
$resultadd = mysqli_query($link,"INSERT INTO user_products VALUES ('','1','3','','','','','','')") or die("Error ".__LINE__);

use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;

class GetOrder
{
// 2. Set up your server to receive a call from the client
/**
*You can use this function to retrieve an order by passing order ID as an argument.
*/
public static function getOrder($orderId)
{
// 3. Call PayPal to get the transaction details
$client = PayPalClient::client();
$response = $client->execute(new OrdersGetRequest($orderId));
/**
*Enable the following line to print complete response as JSON.
*/
print json_encode($response->result);
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";

// mmw code
// end mmw code

}
}

if (!count(debug_backtrace()))
{
GetOrder::getOrder('REPLACE-WITH-ORDER-ID', true);
}
?>

Login to Me Too

shawnz
Contributor
Contributor

Hey Don,

 

I think some points might have gotten lost in the noise in my last post so let me restate a few key things here.

 

- We definitely want Checkout-PHP-SDK and  NOT PayPal-PHP-SDK for now. Also make sure you get the ZIP file from the "releases" page and not from the green "clone or download" button.

 

- The "autoload.php" file is something specific to composer and you won't have that since you're not using composer. That's fine, just don't include any "autoload.php" file.

 

- We won't need PuTTY here -- PuTTY is for connecting to shells on remote servers but you don't have that available so there's no use in using PuTTY. If you just want to run commands in your local terminal, just click "Start" and type "Command prompt", PuTTY isnt necessary for that. But also I don't think using the terminal is really necessary here at all if we're not using composer. You can just use Filezilla or Windows Explorer to create the directories and extract the files, which it seems like you've done already.

 

- Regarding the braintree SDK and paypal SDK: I think you are seeing conflicting files because again I think you are looking at the PayPal-PHP-SDK here but we want the Checkout-PHP-SDK. Take the lib folders from both the Checkout-PHP-SDK and BraintreeHttp_PHP libraries and put them together, there should be no conflicts. Now put all that stuff from both lib folders alongside your php files. Forget about the other files not in the "lib" directory. Forget about the PayPal-PHP-SDK package, delete those files especially the "autoload.php" file from that package. We won't be using any autoload.php file as I mentioned above.

 

Now I will try to address your questions.

 

1) Yes, you still must do the stuff under the "Set up the environment" section. Sorry, almost forgot about this. Basically the jist of it is that you need to create a PayPalEnvironment object with your client id and client secret, and then create a PayPalHttpClient from that PayPalEnvironment object. The PayPalHttpClient is what you will use to initiate all your requests as you will see later.

 

You don't necessarily have to put this code in a seperate file, you could put it right in your paypal-transaction-complete.php file, but that could get a little messy especially if you are using the paypal SDK in multiple places, which is why they suggest putting this setup code in a seperate file. If you do choose to create a seperate file for this initialization code then you can name it whatever you want and you will include it using the "require" statement, like "require '/my-paypal-setup-code.php';" or whatever.

 

2) There are some changes required here but I think you're almost there. First of all as I mentioned above just forget about the autoload.php file as well as any PayPal-PHP-SDK files. We only care about Checkout-PHP-SDK and no autoload.php is necessary for us, since we're not using composer, so just get rid of that line.

 

Then there are still two more necessary changes I see here. First is at the part where it says "// mmw code". Here, you need to check if the gross amount is what you expected it to be, and if so, save the product in the database. If not, just call die() or something to indicate a failure.

 

Next is at the very end where it says REPLACE-WITH-ORDER-ID, we need to get the orderID from the request body. Since we passed it as JSON, you could use an approach like this to get the order ID from the request:

 

$data = json_decode( file_get_contents( 'php://input' ), true );

print $data['orderID'];

 

We will want to pass that $data['orderID'] value in place of the REPLACE-WITH-ORDER-ID part. After these changes I think this file should be basically done for this integration.

 

3) As I mentioned, just get rid of the lines referencing autoload.php. That file will only be present in scenarios where you use composer.

 

4) The getOrder function is called from the very bottom of that same file. You will just need to change that call slightly to feed in the real orderID instead of the example 'REPLACE-WITH-ORDER-ID' value. See my answer above (q. 2) for how you would hook up the request data into that call.

 

Again I know I havent covered everything here but hopefully this will let you get closer to a working solution. Let me know how it goes

 

Shawn

Login to Me Too

shawnz
Contributor
Contributor

Okay, my apologies but you are absolutely right. This won't work without the autoloader. Looks like things in the PHP world have changed quite a bit since I last used it. Since we're not using composer I think the fastest way to get autoloading behaviour would be to add the following code snippet to an autoload.php file and require that in your script:

 

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md#example-implementation

 

Let me know if that works, I'm not sure that's really the correct way of doing this but perhaps it will.

 

Shawn

Login to Me Too

Donquick
Contributor
Contributor

Thanks as always Shawn,  I have been called away for work for a week or two and will resume with this when i get back.  Cheers, Don.

Login to Me Too

TrippyMonk707
Contributor
Contributor

Hello! I am so thankful I found this thread, as I have been struggling to implement PayPal smart buttons on my website as well.  Shawnz answers have been very helpful in understanding what the code is actually doing and I can't wait to get home Sunday and try to implement using his suggestions.  

 

I also wanted to throw out one tidbit that Donquick might find helpful.  I am using a shared-hosting server as well, and I found the following link that explains how to run composer for my specific hosting service.  I would imagine whoever you are using also offers a way to get control of the command line for the server you are using.  

 

Hopefully this helps some.

 

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.