How to accept recurring payment using Braintree SDK?

ve3
Contributor
Contributor

I have this code that is using Braintree SDK with PayPal express checkout and it is working.

 

<?php 
require __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . '_config.php';
require __DIR__ . '/php/braintree/lib/autoload.php';

$gateway = new Braintree\Gateway([
    'accessToken' => $access_token,
]);
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Test paypal braintree SDK.</title>
    </head>

    <body>
        <h2>Braintree SDK payment.</h2>
        <div id="paypal-button"></div>

        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script src="https://www.paypalobjects.com/api/checkout.js"></script>
        <script src="https://js.braintreegateway.com/web/3.25.0/js/client.min.js"></script>
        <script src="https://js.braintreegateway.com/web/3.25.0/js/paypal-checkout.min.js"></script>
        <script>
            paypal.Button.render({
                braintree: braintree,

                env: 'sandbox', // Or 'sandbox',

                commit: true, // Show a 'Pay Now' button

                client: {
                    sandbox:    '<?php echo $gateway->clientToken()->generate(); ?>',
                    production: 'xxxxxxxxx'
                },

                style: {
                    size: 'medium',
                    color: 'gold',
                    shape: 'pill',
                    label: 'pay',// https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/customize-button/#label
                    layout: 'horizontal',
                    fundingicons: true
                },

                /*funding: {
                    disallowed: [ paypal.FUNDING.CARD ]
                },*/

                payment: function (data, actions) {
                    /* 
                     * Set up the payment here 
                     */
                    console.log('on payment setup.');
                    console.log(data);
                    console.log(actions);
                    console.log('-------------------');
                    return actions.braintree.create({
                        flow: 'checkout', // Required
                        intent: 'sale',// http://braintree.github.io/braintree-web/current/PayPalCheckout.html
                        amount: '0.20', // Required
                        currency: 'USD', // Required
                        enableShippingAddress: false
                      });
                },

                onAuthorize: function (payload, actions) {
                    /* 
                     * Execute the payment here 
                     */
                    console.log('on authorize.');
                    console.log(payload);
                    /*
                     * Submit `payload.nonce` to your server.
                     * payload.orderID is invoice id.
                     */
                    jQuery.ajax({
                        url: 'btsdk-payment-serverside.php',
                        method: 'POST',
                        data: 'amount=0.20&payment_method_nonce='+payload.nonce+'&order_id='+payload.orderID+'&ppdesc=test braintree sdk payment'
                    })
                    .done(function(data, textStatus, jqXHR) {
                        console.log('paid success!');
                        alert('success');
                    })
                    .always(function(data, textStatus, jqXHR) {
                        console.log('server transaction response');
                        console.log(data);
                    });
                    // for more data about the user's PayPal account:
                    actions.payment.get().then(function(data) {
                        console.log('actions.payment.get data:');
                        console.log(data);
                    });
                    console.log('-------------------');
                },

                onCancel: function (data, actions) {
                    /* 
                     * Buyer cancelled the payment 
                     */
                    console.log('on cancel payment.');
                    console.log(data);
                    console.log(actions);
                    console.log('-------------------');
                },

                onError: function (err) {
                    /* 
                     * An error occurred during the transaction 
                     */
                    console.log('on error payment.');
                    console.log(err);
                    console.log('-------------------');
                }
            }, '#paypal-button');
        </script>
    </body>
</html>

But I can't code it for subscription or recurring payment (monthly, annually). I cannot signup Braintree account because my country is not supported. So, I cannot create plan or follow coding with this document (https://developers.braintreepayments.com/guides/recurring-billing/plans).

 

How to accept recurring payment using Braintree SDK?

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

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