Vishal Suri
Vishal Suri

Reputation: 455

How to implement recurring billing in PHP application using Braintree payment gateway?

I have a web application that provides some service to customers. I want to integrate Braintree payment gateway. I have created a page which gets customer's credit card information and creates new customer in brain tree secure vault using transparent redirect method.

I have no idea what to do next to implement recurring billing. Amount to be charged from customers is different for every customer, depending on users of customers. Also billing period of each cutomer is different. I have no idea how to implement recurring billing.

Following is my code for credit card page:

<?php
require_once '../_environment.php';
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != "") {
    $queryString = $_SERVER['QUERY_STRING'];
    $result = Braintree_TransparentRedirect::confirm($queryString); 
    if ($result->success) {
       //Do your stuff
    } else {
        foreach ($result->errors->deepAll() as $error) {
            $errorFound = $error->message . "<br />";
        }
        echo $errorFound ;
        exit;
    }
}
$trData = Braintree_TransparentRedirect::createCustomerData(
  array(
    'redirectUrl' => 'https://www.example.com/creditcard.php',    
  )
);
?>

<form method="POST" action="<?php echo Braintree_TransparentRedirect::url(); ?>" autocomplete="off">
<table cellpadding="0" cellspacing="0" border="0" width="98%" align="left"> 
   <tr><td align="right" style="color:#6593cf" width="40%">Customer Information</td><td align="left" colspan="2"><hr style="color:#6593cf;margin-right:30%;margin-left:2px"></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">First Name</td><td>&nbsp;</td><td><input type="text" name="customer[first_name]" /></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">Last Name</td><td>&nbsp;</td><td><input type="text" name="customer[last_name]" /></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">Company</td><td>&nbsp;</td><td><input type="text" name="customer[company]" /></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">Email</td><td>&nbsp;</td><td><input type="text" name="customer[email]" /></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">Phone</td><td>&nbsp;</td><td><input type="text" name="customer[phone]" /></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>                    
         <tr><td align="right" style="color:#6593cf">Payment Information</td><td align="left" colspan="2"><hr style="color:#6593cf;margin-right:30%;margin-left:2px"></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">Name on Card</td><td>&nbsp;</td><td><input type="text" name="customer[credit_card][cardholder_name]" /></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">Credit Card Number</td><td>&nbsp;</td><td><input type="text" name="customer[credit_card][number]" /></td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">Expiration date (mm/yy format)</td><td>&nbsp;</td><td><input type="text" name="customer[credit_card][expiration_date]" /></td></tr>   
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td align="right">CVV</td><td>&nbsp;</td><td><input type="text" name="customer[credit_card][cvv]" /></td></tr>
        <input type="hidden" name="tr_data" value="<?php echo htmlentities($trData) ?>" />
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
    <tr><td align="right"><a href=""><b>Cancel</b></a></td><td style='width:30px;'></td><td align="left"><input type="submit" value="Submit" class="btnSize" name="submit"/></td></tr>
    <tr><td colspan="3">&nbsp;</td></tr>    
</table>  

Upvotes: 0

Views: 2391

Answers (1)

rafi
rafi

Reputation: 1543

You have to use braintree's specific api call for recurring billing.

Braintree_Subscription::create(array(
   'paymentMethodToken' => $payment_method_token,
   'planId' => $package_code,
   'price' => $monthly_price
));

This "Create" is for recurring billing.

Upvotes: 0

Related Questions