示例#1
0
/**
 ** Builds the payment link for WHMCS users to be redirected to GoCardless
 **/
function gocardless_link($params)
{
    # get global config params
    global $CONFIG;
    # create GoCardless database if it hasn't already been created
    gocardless_createdb();
    # check the invoice, to see if it has a record with a valid resource ID. If it does, the invoice is pending payment.
    # we will return a message on the invoice to prevent duplicate payment attempts
    $aGC = mysql_fetch_assoc(select_query('mod_gocardless', 'id,payment_failed', array('invoiceid' => $params['invoiceid'], 'resource_id' => array('sqltype' => 'NEQ', 'value' => ''))));
    if ($aGC['id']) {
        if ($aGC['payment_failed'] == 0) {
            # Pending Payment Found - Prevent Duplicate Payment with a Msg
            return '<strong>Your payment is currently pending and will be processed within 3-5 days.</strong>';
        } else {
            # display a message to the user suggesting that a payment against the invoice has failed
            return '<strong>One or more payment attempts have failed against this invoice. Please contact our support department.</strong>';
        }
    }
    # get relevant invoice data
    $aRecurrings = getRecurringBillingValues($params['invoiceid']);
    $recurringcycleunit = strtolower(substr($aRecurrings['recurringcycleunits'], 0, -1));
    # check a number of conditions to see if it is possible to setup a preauth
    if ($params['oneoffonly'] == 'on' || $aRecurrings === false || $aRecurrings['recurringamount'] <= 0) {
        $noPreauth = false;
        #MOD
    } else {
        $noPreauth = false;
    }
    # set appropriate GoCardless API details
    gocardless_set_account_details($params);
    # set user array based on params parsed to $link
    $aUser = array('first_name' => $params['clientdetails']['firstname'], 'last_name' => $params['clientdetails']['lastname'], 'email' => $params['clientdetails']['email'], 'billing_address1' => $params['clientdetails']['address1'], 'billing_address2' => $params['clientdetails']['address2'], 'billing_town' => $params['clientdetails']['city'], 'billing_county' => $params['clientdetails']['state'], 'billing_postcode' => $params['clientdetails']['postcode']);
    $invoice_item_query = select_query('tblinvoiceitems', 'relid', array('invoiceid' => $params['invoiceid'], 'type' => 'Hosting'));
    while ($invoice_item = mysql_fetch_assoc($invoice_item_query)) {
        $package_query = select_query('tblhosting', 'subscriptionid', array('id' => $invoice_item['relid']));
        $package = mysql_fetch_assoc($package_query);
        if (!empty($package['subscriptionid'])) {
            $preauthExists = true;
        }
    }
    if ($preauthExists) {
        # The customer already has a pre-auth, but it's yet to be charged so
        # let's not let them set up another...
        return (GoCardless::$environment == 'sandbox' ? '<strong style="color: #FF0000; font-size: 16px;">SANDBOX MODE</strong><br />' : null) . '<strong>Automatic payments via Direct Debit or another payment method are already configured for this invoice. You will receive an email once you have been billed.</strong>';
    } elseif ($noPreauth) {
        # if one of the $noPreauth conditions have been met, display a one time payment button
        # we are making a one off payment, display the appropriate code
        # Button title
        $title = 'Pay Now with GoCardless';
        # create GoCardless one off payment URL using the GoCardless library
        $url = GoCardless::new_bill_url(array('amount' => $params['amount'], 'name' => "Invoice #" . $params['invoiceid'], 'user' => $aUser, 'state' => $params['invoiceid'] . ':' . $params['amount']));
        # return one time payment button code
        $sButton = (GoCardless::$environment == 'sandbox' ? '<strong style="color: #FF0000; font-size: 16px;">SANDBOX MODE</strong><br />' : null) . '<a href="' . $url . '" onclick="window.location=\'' . $url . '\';" style="text-decoration: none"><input onclick="window.location=\'' . $url . '\';" type="button" value="' . $title . '" /></a>';
    } else {
        # we are setting up a preauth (description friendly name), display the appropriate code
        # get the invoice from the database because we need the invoice creation date
        $aInvoice = mysql_fetch_assoc(select_query('tblinvoices', 'date', array('id' => $params['invoiceid'])));
        # GoCardless only supports months in the billing period so
        # if WHMCS is sending a year value we need to address this
        if ($recurringcycleunit == 'year') {
            $recurringcycleunit = 'month';
            $aRecurrings['recurringcycleperiod'] = $aRecurrings['recurringcycleperiod'] * 12;
        }
        $pre_auth_maximum = 5000;
        # Always create a £5000 pre-auth
        # Button title
        $title = 'Create Subscription with GoCardless';
        # create GoCardless preauth URL using the GoCardless library
        $url = GoCardless::new_pre_authorization_url(array('max_amount' => $pre_auth_maximum, 'setup_fee' => $aRecurrings['firstpaymentamount'] > $aRecurrings['recurringamount'] ? $aRecurrings['firstpaymentamount'] - $aRecurrings['recurringamount'] : 0, 'name' => "Direct Debit payments to " . $CONFIG['CompanyName'], 'interval_length' => '1', 'interval_unit' => 'day', 'start_at' => date_format(date_create($aInvoice['date'] . ' -2 days'), 'Y-m-d\\TH:i:sO'), 'user' => $aUser, 'state' => $params['invoiceid'] . ':' . $aRecurrings['recurringamount']));
        # return the recurring preauth button code
        $sButton = (GoCardless::$environment == 'sandbox' ? '<strong style="color: #FF0000; font-size: 16px;">SANDBOX MODE</strong><br />' : null) . 'When you get to GoCardless you will see an agreement for the <b>maximum possible amount</b> we\'ll ever need to charge you in a single invoice for this order, with a frequency of the shortest item\'s billing cycle. But rest assured we will never charge you more than the actual amount due.
            <br /><a onclick="window.location=\'' . $url . '\';" href="' . $url . '" style="text-decoration: none"><input type="button" onclick="window.location=\'' . $url . '\';" value="' . $title . '" /></a>';
    }
    # return the formatted button
    return $sButton;
}
 public function gocardless_payment($b_id)
 {
     //get the bill to pay.
     $this->load->model('order_model');
     $bill = $this->order_model->get_bill($b_id);
     if (!$bill) {
         show_404();
     }
     //are we ready to pay it?
     if ($bill['b_status'] != 'Pending') {
         $this->flash->set('error', 'The payment for this bill can not be made right now.', TRUE);
         return redirect('bill/view/' . $b_id);
     }
     //is it enough for Go Cardless?
     if ($bill['b_price'] < 1) {
         $this->flash->set('error', 'The bill is below the Go Cardless minimum spend.', TRUE);
         return redirect('bill/view/' . $b_id);
     }
     //get going with Go Cardless
     require_once APPPATH . '/third_party/GoCardless.php';
     GoCardless::$environment = config_item('gocardless_environment');
     GoCardless::set_account_details(config_item('gocardless_account'));
     if ($this->input->post('automate_payments') == 1) {
         //set up an authorization
         if (!$this->input->post('gc_max_payment') || !is_numeric($this->input->post('gc_max_payment'))) {
             $this->flash->set('error', 'The maximum value must be a number', TRUE);
             return redirect('bill/view/' . $b_id);
         }
         if ($this->input->post('gc_max_payment') < $bill['b_price']) {
             $this->flash->set('error', 'That authorization will not be enough to pay this bill.', TRUE);
             return redirect('bill/view/' . $b_id);
         }
         $payment_details = array('redirect_uri' => site_url('bill/gocardless_complete/' . $b_id), 'cancel_uri' => site_url('bill/gocardless_cancelled/' . $b_id), 'state' => $b_id, 'max_amount' => $this->input->post('gc_max_payment'), 'name' => 'Weekly Delivery', 'interval_length' => 1, 'interval_unit' => 'week');
         $gc_url = GoCardless::new_pre_authorization_url($payment_details);
     } elseif ($this->input->post('make_payment') == 1) {
         //make one off payment
         $payment_details = array('redirect_uri' => site_url('bill/gocardless_complete/' . $b_id), 'cancel_uri' => site_url('bill/gocardless_cancelled/' . $b_id), 'state' => $b_id, 'amount' => $bill['b_price'], 'name' => 'Bill #' . $b_id);
         $gc_url = GoCardless::new_bill_url($payment_details);
     }
     //send them to Go Cardless
     if (!$gc_url) {
         $this->flash->set('error', 'The payment was not recognised.', TRUE);
         return redirect('bill/view/' . $b_id);
     } else {
         return redirect($gc_url);
     }
 }
示例#3
0
    }
    $confirmed_resource = GoCardless::confirm_resource($confirm_params);
    echo '<p>Payment confirmed!</p>';
}
echo '<h2>New payment URLs</h2>';
// New subscription
$payment_details = array('amount' => '10.00', 'interval_length' => 1, 'interval_unit' => 'month');
$subscription_url = GoCardless::new_subscription_url($payment_details);
echo '<p><a href="' . $subscription_url . '">New subscription</a>';
// New pre-authorization
$payment_details = array('max_amount' => '20.00', 'interval_length' => 1, 'interval_unit' => 'month');
$pre_auth_url = GoCardless::new_pre_authorization_url($payment_details);
echo ' &middot; <a href="' . $pre_auth_url . '">New pre-authorized payment</a>';
// New bill
$payment_details = array('amount' => '30.00', 'name' => 'Donation', 'user' => array('first_name' => 'Tom', 'last_name' => 'Blomfield', 'email' => '*****@*****.**'));
$bill_url = GoCardless::new_bill_url($payment_details);
echo ' &middot; <a href="' . $bill_url . '">New bill</a></p>';
echo 'NB. The \'new bill\' link is also a demo of pre-populated user data';
echo '<h2>API calls</h2>';
echo 'GoCardless_Merchant::find(\'258584\')';
echo '<blockquote><pre>';
$merchant = GoCardless_Merchant::find('258584');
print_r($merchant);
echo '</pre></blockquote>';
echo 'GoCardless_Merchant::find(\'258584\')->pre_authorizations()';
echo '<blockquote><pre>';
$preauths = GoCardless_Merchant::find('258584')->pre_authorizations();
print_r($preauths);
echo '</pre></blockquote>';
echo 'GoCardless_PreAuthorization::find(\'992869\')->create_bill($bill_details)';
echo '<blockquote><pre>';
 /**
  * Generate a URL to give a user to create a new bill
  *
  * @param array $params Parameters to use to generate the URL
  *
  * @return string The generated URL
  */
 public static function new_bill_url($params)
 {
     return GoCardless::$client->new_bill_url($params);
 }
 public function newBillUrl($payment_details)
 {
     return \GoCardless::new_bill_url($payment_details);
 }