Example of PHP Paypal Smart Buttons – How to Integrate Smart PayPal Buttons

A Short tutorial and snippets on how to integrate PHP PayPal Smart Buttons.

This is a snippet code of the integration of PayPal Smart Buttons. I struggled so much in creating this for SandBox so this is the sandbox integration for PayPal Smart Buttons. 

First you must include the Script Tag

<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=MXN&intent=capture"></script>

Second This is the Integration to Post Via AJAX

<div id="paypal-response"></div>
<div id="paypal-button-container"></div>
<div class="loaders"><img src="<?php echo base_url(); ?>assets/payment/loader.gif" width="100" alt=""></div>



<script>
paypal.Buttons({

  createOrder: function(data, actions) {
    return actions.order.create({
      purchase_units: [{
        amount: {
          value: <?php echo $amount; ?>,

        },
        payee: {
          email_address: 'abrahamgarcia27-store@gmail.com'
        }
      }],
      application_context: {
          shipping_preference: 'NO_SHIPPING',
      }

    });
  },
  onApprove: function(data, actions) {
    <?php $paypal_submit_url = site_url() . 'signup/paypalpayment'; ?>
    return actions.order.capture().then(function(details) {
      $('.loaders').show();
      console.log(details);
       $.ajax({
        method: "POST",
        url: '<?php echo $paypal_submit_url; ?>',
        data: { 
          name: $('#name-submit').val(),
          email: $('#email-submit').val(),
          product_id: <?php echo $product[0]->product_id; ?>,
          <?php if($coupon){ ?>
            coupon_id: <?php echo $coupon[0]->coupon_id; ?>,
          <?php } ?>
          transaction_id: details.purchase_units[0].payments.captures[0].id
        }
      })
        .done(function( msg ) {
          $('#paypal-response').append(msg);
          $('.loaders').hide();
        });
                                        
      
     
      
    });
  }
}


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

I hope this explains alot this was very difficult to read from PayPal’s Platform.