PayPal button post action

Hi there. I created a store to sell digital downloads using dynamic pages. After receiving payment (PayPal), I want to send the personalized, specific, dynamic download link to the paying customer.

What I have: I know the specific item code, and I can relate that code to the customer’s email and to the specific download link.

What I need: a way to relate that item to that payment to be able to send that payer the specific download link (post action). Let’s say, being able to send the item code as a variable content to PayPal, then I’d know that a specific item has been paid for, then I can send the corresponding download link to the associated email.
Since the default Wix provided PayPal button doesn’t allow to send variable content, how can I do that?
I’m open to other alternatives that could allow me to implement this.
Help would be deeply appreciated. Thanks in advance.

Hi Agustin

What you can do is use a custom coded PayPal button. This will allow PayPal to send a success message back to the page if the payment was successfull, and then, you can construct a unique link related to that user with page code and email that custom link to the client…

It will be quite a process to make it work, you will need custom PayPal integration, custom sendgrid email integration and a clever bit of code…

Tiaan, thanks for your reply. Reading your answer, it may sound overwhelming, so I’m going to solve one thing at the time. I used to program complicated stuff about a century ago; I’m a newbie in wix code but I’m not afraid to learn.

I’ll start creating a custom PayPal button and adding in an HTML iframe. The first obstacle I foresee is how to populate a variable with -let’s say- the page url with something like the following:
import wixLocation from ‘wix-location’;
let paypalpathbuttonvariable = wixLocation.path;

I’ve been following your post “Populating PayPal button’s value on dynamic pages with code” and assume you’ve already got a solution, or at least you are way ahead of me. I’d really appreciate any knowledge you can share on this matter. Thanks in advance.

Hi Agustin

I don’t think it’s actually necessary to send the page url to PayPal and then have it returned if I’m u derstanding you correctly, you can simply create the page url when PayPal returns a success message and then attach it to the email you want to send…

So I would add a message funtion to the html element that returns the success message to the page, and then start the work from there

Tiaan, I think I approached the solution in a different way. Let me explain the setup:

Every digital product is unique, and it’s already loaded in a table.
Every digital product has its own unique identifier (say ‘product001’)
Every digital product loaded in the table has an associated email address to send the download link
The url for every dynamic page is a fixed prefix/the_unique_product_identifier (say agustin.com/digitaldownloads/product001)
TABLE STRUCTURE: product_id | linktodownloadablefile | emailtosendthelink

So, if anywhere in the process I get the page url, I can substr(url) to extract the_unique_product_identifier, hence I can identify the product (say again ‘product001’)

My line of reasoning is that if I can somehow insert the url who originated the payment as a variable in the PayPal button, whenever a payment is made, I’ll check that value and with a simple search in the table I’d be able to identify the product which would be associated to a specific email, in another field of the same record. (this would be preloaded in advance, as a part of a more complex automatized strategy).

See? So, I’ll be checking the PayPal notifications outside the website.
If only the PayPal notification would include the url of the payment originating page in the payment record / notification, I’d have the problem solved.

Very complex stuff…, although I know it would be possible via PayPal IPN integration. I haven’t gotten that far, so I’m afraid you’re on your own from here…

Yeah, I know that I’ll have to deal with something special on the PayPal side, and to think that I’ll have to read and understand their abundantly confusing documentation just gives me the creeps.
The question is: Are you using a custom PayPal button? Are you passing some value as a variable? Did it work? Wix code is a whole new path for me, any clues in this matter would be much appreciated.

Yeah their documentation is a nightmare…, there’s just too much honestly…

Yes, I’m using a custom PayPal button. I use postMessage in WiX to send the amount to the html element that I then replace inside my button with the ‘amount’ substitution. When the payment is successful, PayPal returns a message to the html element and then when that message is received, I send a message from the html element to the page to do something, eg update a database, send a confirmation email or send the user to a thank-you page. This works quite well because it’s like firing any event really…

let me do some text replacement here:
“Yes, I’m using a custom PayPal button. I use postMessage in Wix to send the PAGE URL to the html element that I then replace inside my button with the PAGE URL substitution. When the payment is successful, PayPal somehow would let me know that a payment was made including that PAGE URL” (I’ll cross that bridge when I get there)

Is the above possible? I’m proposing to substitute AMOUNT with a value that I would comfortably get from:
import wixLocation from ‘wix-location’;
let paypalpathbuttonvariable = wixLocation.path;

and considering that AMOUNT is a numeric value, and I need to send a string, could it be possible to tweak another variable?
I’m trying to divide each big problem into many small ones without losing the whole picture…

Using the button to send the page url back and forth between the page shouldn’t be a problem, in fact I think this is probably quite easy (but I still think unnecessary…)

What I think would take some time to figure out is how to have the unique page url show up in the email/notification that is sent from PayPal…, if that is what you want!? Possible, this I know, but buried somewhere deep within the docs

If you can replace a button variable content with any value you want, and if that value is somehow registered in the transaction record, it could be the solution I’m looking for.

Let me reformulate the question: You replace amount (default 10.00) with a specific value, say 9.85. Since I assume this amount is reflected in the transaction, and you customer effectively pays 9.85, do you receive an email or some sort of notification that a transaction for 9.85 was made? If so, that means that you can tweak a variable (I’m pretty sure that there are a couple of unused ones) and that the tweaked content will get recorded in the transaction notification. Voilá! There’s the page url, there’s the digital product, there’s me very happy.

That should work. PayPal does allow you to send a ‘custom’ value that they will record. I think if you can figure out how to build that into the button then you should be ready to go!

Nice. But do you receive some kind of “written” notification with that value as an email or something like that? I’m planning to use a robot to check for new PayPal notifications and take action from there. If the url is recorded and embedded in the transaction record, that could mean a great leap for mankind.

I would think so… Think of it this way, when you sell something trough ebay for instance, you receive an email notification from PayPal telling you what you’ve sold in as much detail as could be fitted in there. This should be similar to that.

Right then, Tiaan. I really appreciate your kindness to answer my questions. I’d be working on that during next week. I’ll start doing the custom button and then I’ll try to figure out how to replace (or fill) a variable value, the same way you do.
If push comes to shove, I’ll post my questions here.
Many thanks.

Best of luck Agustin!

Hi there Tiaan, I hope you’re still following this post. Had to put this project in the fridge for a while, but I’m back now.
I think I solved the problem, and now I’m trying to parse a dynamic value into the variable “custom”. Since I used your code as a base, maybe you can help me out. (look for “HERE IS THE PROBLEM” down below.
I can’t make it right. I tried all sort of combinations and it’s not working. I can pass a fixed value as in
[custom: “this is the custom value to parse”], but nothing more.
It seems that the line “custom: ${CUSTOM}” somehow does not take/read/parse the value inside the CUSTOM variable. Most of other combinations result in the button not displaying or in execution mode, a blank window open and closes after a few seconds. Would you please take a look?

UPDATE: it’s something related to how I do the CUSTOM variable definition. Depending on if I use 's or "s to assign the value to the variable CUSTOM. Still working on it.
What I can say now is that I can parse the value of CUSTOM if I hard code it (CUSTOM = “This value”). Thing is I can’t load the variable with event.data (CUSTOM = event.data);
I’m getting closer now. Any ideas?

Hi Agustin

In the code, instead of " if ( event.data != ‘’) " use " if ( event.data > ‘’) "

That should do it!

Good luck!
Tiaan

Tiaan! You were still there! Thanks for your reply. This is driving me crazy. All I want is to send a text value instead of a numeric one and assign that value to the “custom” paypal variable. Somehow, I’m doing something wrong. This should be simple, yet, it’s not working.

Debugging it, apparently CUSTOM is loaded with a value that is longer than 256 characters. HOW???
Any help would be much appreciated. Thanks in advance.


// Page code:

$w.onReady( function () {
// when a message is received from the HTML Component
$w(“#html1”).postMessage(‘this is the value to be parsed into custom variable, a simple text’);
} );

in the html component:

<script type="text/javascript"> 

let AMOUNT = 0; 
let CUSTOM = 'initialize'; 
window.onmessage = (event) => { 
    		if (event.data > '') { 
		CUSTOM = event.data;   
		AMOUNT = 0.1; 
   	} 
	} 

    paypal.Button.render({ 

        env: 'sandbox', // sandbox | production 

        // PayPal Client IDs - replace with your own 
        // Create a PayPal app: https://developer.paypal.com/developer/applications/create 
        client: { 
            sandbox:    'AVv_oPTUWBaUY0m9TuJKBB27u1dosborN7A2X5AwLYYl1GhRoUQo2naGFUKRGVM84p23BjMV', 
            production: '<insert production client id>' 
        }, 

        // Show the buyer a 'Pay Now' button in the checkout flow 
        commit: true, 

        // payment() is called when the button is clicked 
        payment: function(data, actions) { 

            // Make a call to the REST api to create the payment 
            return actions.payment.create({ 
                payment: { 
                    transactions: [ 
                        { 
                          amount: { total: `${AMOUNT}`, currency: 'USD' }, 
		      custom: `${CUSTOM}` 
                        } 
                    ] 
                } 
            }); 
        }, 

        // onAuthorize() is called when the buyer approves the payment 
        onAuthorize: function(data, actions) { 

            // Make a call to the REST api to execute the payment 
            return actions.payment.execute().then(function() { 
                window.alert('Payment Complete!');                  
                window.alert(event.data); 
            }); 
        } 

    }, '#paypal-button-container'); 

</script>