﻿<?php


# Global Definetions :
$TerminalNumber = 1000; # Company terminal 
$UserName = 'test9611';   # API User

$OperationAdd = "newandupdate";  # newandupdate  do one or all of the following :  Update\Add New Account  And\Or Update\Add account payment info  And\Or Add new    recurring payment
//$OperationUpdate = "update";     # update        do one or all of the following :  Update Account          And\Or Update     account payment info  And\Or Add Update recurring payment
                               
###############################################################                              
### Add New Account new  Payment Info new recurring payment.###
###############################################################
$vars =  array();
// Const parameters:
$vars['terminalnumber'] = $TerminalNumber;
$vars['username'] = $UserName;
$vars['codepage'] = '65001'; // unicode
$vars["Operation"] = $OperationAdd;
### Add/Update Payment Method , all Params At : kb.XXXX.XXXX ###

##### send the LPC code , see h   https://support.cardcom.solutions/hc/he/articles/360008695513-%D7%94%D7%95%D7%A8%D7%90%D7%AA-%D7%A7%D7%91%D7%A2-%D7%91-API-%D7%99%D7%A6%D7%99%D7%A8%D7%AA-%D7%94%D7%95%D7%A8%D7%90%D7%AA-%D7%A7%D7%91%D7%A2-%D7%91%D7%9E%D7%A2%D7%A8%D7%9B%D7%AA-%D7%A7%D7%90%D7%A8%D7%93%D7%A7%D7%95%D7%9D-Direct-Debit-Api ####

 $vars["LowProfileDealGuid"] = "da3e5df8-059c-4ee0-9d69-554bbdc8fdcd"; 
#############################################################################

# if you whan a bank order : 
#$vars["BankInfo.Bank"] = "2"; 
#$vars["BankInfo.Branch"] = "122"; 
#$vars["BankInfo.AccountNumber"] = "543223"; 
#$vars["BankInfo.Description"] = "Test Company bank account"; 

 
// Add Acount Info , all Params At : kb.XXXX.XXXX
// $vars["Account.AccountId"] = "100"; # for "update" Operation. Will Update only the params that will be passed!
$vars["Account.CompanyName"] = "Test Company"; # Req Name of the account / company
$vars["Account.RegisteredBusinessNumber"] = "040617649"; #  
//$vars["Account.SiteUniqueId"] = "040617649"; #
$vars["Account.FirstName"] = "yaniv"; #  
$vars["Account.City"] = "holon"; #  
$vars["Account.Street1"] = "Street 1"; #  
$vars["Account.Street2"] = "Street 2"; #  
$vars["Account.ZipCode"] = "12345"; #  
$vars["Account.PhLine"] = "040617649"; #  
$vars["Account.PhMobile"] = "039619611"; #  
$vars["Account.Email"] = "DemoEmail@DemoSite.co.us"; #  
// $vars["Account.VatFree"] = "true"; #   is account Is vat free ?
// $vars["Account.IsDocumentLangEnglish"] = "true"; #  is the document lang in English
// $vars["Account.Comments"] = ""; #  
// $vars["Account.ContactName"] = ""; #  


#### Add two  Recurring Payments  ###
# $vars["RecurringPayments.RecurringId"] = "2000"; //   # for "update" Operation Only ! Will Update only the params that will be passed!

$vars["RecurringPayments.InternalDecription"] = "new order for Recurring0"; # some internal description for the Recurring Payment 
$vars["RecurringPayments.NextDateToBill"] = "31/12/2025"; #  the first time to bill the account .
$vars["RecurringPayments.TotalNumOfBills"] = "999999"; #  number of time to bill the account
$vars["RecurringPayments.FinalDebitCoinId"] = "1"; #   1- NIS , 2- USD -else ISO currency.  
$vars["RecurringPayments.ReturnValue"] = "Recurring0"; #  
$vars["RecurringPayments.FlexItem.InvoiceDescription"] = "subsciprion for Recurring0"; # Description for the account invoice 
$vars["RecurringPayments.FlexItem.Price"] = "100"; #   Sum to Bill the account  in every time. (total account bill is TotalNumOfBills*FlexItem.Price)
$vars["RecurringPayments.FlexItem.IsPriceIncludeVat"] = "true"; # the account will be bill 100 include vat - good for BTC , if BTB , send FlexItem.Price no vat and FlexItem.IsPriceIncludeVat=false   

// Add Secend Recurring Payment
# $vars["RecurringPayments.RecurringId"] = "2001"; //   # for "update" Operation Only ! Will Update only the params that will be passed!
$vars["RecurringPayments1.InternalDecription"] = "new order for Recurring1"; 
$vars["RecurringPayments1.NextDateToBill"] = "31/12/2025"; 
$vars["RecurringPayments1.TotalNumOfBills"] = "10"; 
$vars["RecurringPayments1.FinalDebitCoinId"] = "1"; 
$vars["RecurringPayments1.ReturnValue"] = "Recurring1"; #  
$vars["RecurringPayments1.FlexItem.InvoiceDescription"] = "subsciprion for Recurring1";
$vars["RecurringPayments1.FlexItem.Price"] = "100"; 
$vars["RecurringPayments1.FlexItem.IsPriceIncludeVat"] = "false"; # the account will be bill 100 pluse the currnt var 100*1.18 - good for  BTB




// Send Data To Bill Gold Server
$r = PostVars($vars,'https://secure.cardcom.solutions/Interface/RecurringPayment.aspx');


  parse_str($r,$responseArray);

  # Is Deal OK 
  if ($responseArray['ResponseCode'] == "0") 
  {
      echo "<br/>Description: ".$responseArray['Description']."<br/>"; #  response Description
      echo "Total Recurring: ".$responseArray['TotalRecurring']."<br/>"; # total number of new /update  Recurring Payment
      echo "Is New Account: ".$responseArray['IsNewAccount']."<br/>"; # Is the Account a new One (if not then update)
      echo "Account Id: ".$responseArray['AccountId']."<br/>"; # Update \ New Account ID.

      echo "<br/>Print Recurring Payment result<br/>";
      echo "------------------------------<br/>";
      $total = $responseArray['TotalRecurring'];



      for($i=0 ; $i<$total ; $i++ )
     {

          echo "Recurring Id ".$i.": ".$responseArray['Recurring'.$i.'_RecurringId']."<br/>"; # CardCom  Recurring Id
          echo "Return Value ".$i.": ".$responseArray['Recurring'.$i.'_ReturnValue']."<br/>"; # your custom temp value for the Recurring Payment
          echo "Is New Recurring ".$i.": ".$responseArray['Recurring'.$i.'_IsNewRecurring']."<br/><br/>"; # Is New Recurring Payment ?
    }

      echo "<br/><br/><br/>".$r;
  // See Full Response  At : kb.XXXX.XXXX    
  }
  # Show Error to developer only
  else
  {
        echo $r;
  }

function PostVars($vars,$PostVarsURL)
{
  $urlencoded = http_build_query($vars);
  #init curl connection
  if( function_exists( "curl_init" )) 
  { 
     $CR = curl_init();
    curl_setopt($CR, CURLOPT_URL, $PostVarsURL);
    curl_setopt($CR, CURLOPT_POST, 1);
    curl_setopt($CR, CURLOPT_FAILONERROR, true);
    curl_setopt($CR, CURLOPT_POSTFIELDS, $urlencoded );
    curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($CR, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($CR, CURLOPT_FAILONERROR,true);
    #actual curl execution perfom
    $r = curl_exec( $CR );
    $error = curl_error ( $CR );
    # some error , send email to developer
    if( !empty( $error )) {

      echo $error;

      die();
    }
   curl_close( $CR );
   return $r;
 }
  else
 {
  echo "No curl_init" ;
  die();
  }
}

?>