0
votes

I am new to set up Subscription using Paypal Smart buttons. I have created a Paypal smart button subscribe from Paypal account and have pasted it in my html. Have added my OnInit and Onclick function to validate before user clicks.

I am checking if the subscription is Active or not after the user goes through the "agree and subscribe" page. (This is done in "notify.php" where I call using paypal REST api and get the subscription status)

<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXXXXXXXX&vault=true&intent=subscription" data-sdk-integration-source="button-factory"></script>
                    <script>
                        paypal.Buttons({

                            style: {
                              shape: 'pill',
                              color: 'gold',
                              layout: 'vertical',
                              label: 'subscribe'
                            },

                            // onInit is called when the button first renders
                            onInit: function(data, actions) {

                              // Disable the buttons
                              actions.disable();

                              // Listen for changes to the radio
                                var items = document.getElementsByName("settings");
                                [].forEach.call(items, function (item) {
                                    item.addEventListener("change", function () {
                                                  // Enable or disable the button when it is checked or unchecked
                                        if (event.target.checked) {
                                            actions.enable();
                                        } else {
                                            actions.disable();
                                        }
                                    });
                                });

                             
                            },

                            // onClick is called when the button is clicked
                            onClick: function() {
                                var radios=document.querySelectorAll('input[type="radio"]:checked').length;
                              // Show a validation error if the checkbox is not checked
                              if (radios == 0) {
                                document.querySelector('#error').classList.remove('hidden');
                              }
                            
                                
                            },
                            
                            createSubscription: function(data, actions) {
                                return actions.subscription.create({
                                  'plan_id': 'P-XXXXXXXXXXXX'
                                });
                            },
                            
                            onApprove: function(data, actions) {
                                alert(data.subscriptionID);
                                alert(data.SubscriptionDate);

                                // You must return a promise from onClick to do async validation
                                  return fetch("notify.php", {
                                    method: 'post',
                                    headers: {
                                      'content-type': 'application/json'
                                    },
                                    body:JSON.stringify({
                                        subscriptionID:data.subscriptionID
                                    })
                                    
                                  }).then(response => {
                                        
                                         return response.text();
                                    })
                                    .then(data => {
                                         
                                          console.log(data);
                                          if(data == "ACTIVE"){

                                            location.href = "validate.html?data=ACTIVE";
                                          
                                      }
                                         
                                    })
                                    .catch(error => {console.log(error)
                                        });
                                  }

                      }).render('#paypal-button-container');
                    </script>
                </div>

Q1. Could somebody please confirm when the subscription status returns as "ACTIVE", it means that the user has paid and has subscribed successfully. And thus I can update this information in my DB. OR Do I need to capture the transaction in OnApprove as below:

 onApprove: function (data, actions) {
// Capture the transaction funds
          return actions.order.capture().then(function () {

Q2. Is this way of integrating the button on my web page is secure?

UPDATE: (All this is for my personal webpage where I am providing a service to customers. I want to activate a subscription for them.) I made a subscription plan from my Paypal account. Added the generated code on my webpage and now I want to check whether the user/customer has paid/subscribed successfully to my service so that I can enable service for them and also update their information in my DB.

1
Does this answer your question? PayPal Smart Subscribe server side - Preston PHX
@PrestonPHX: No, this I have already checked. I am not creating subscription plan using API. I just want to check whether the subscription is ACTIVE or not so that I can make entry of the details in my DB. I want to know if the paypal makes the subscription active, does this confirms that the user has paid for it ? - technew
The plan should be created beforehand, via API or not is irrelevant. The subscription should be created via API, so you can activate it from your server. Then you know it is active. Javascript code should not trigger events that write to your database, communicating with the PayPal API should. - Preston PHX
I am passing information (subscriptionID received via onApprove ) from JS to notify.php where I call the subscription API via REST and check for subscription status. There if active then only I update DB - technew
Then subscriptions may become active without your server being notified, since the notification comes from client JS. As I said, your server should call the API to activate the subscription. See the above link for details. - Preston PHX

1 Answers

0
votes

An active PayPal subscription does not indicate the first payment has already been made.

Subscriptions bill on a batch schedule. To be notified when a successful payment is made, subscribe to the Webhook event PAYMENT.SALE.COMPLETED