/**
 * Make eway payment.
 *
 * @param array $params
 *
 * @throws API_Exception
 * @throws CiviCRM_API3_Exception
 *
 * @return array
 *   API Result array
 */
function civicrm_api3_ewayrecurring_payment($params)
{
    // If the site is in developer mode we return a mock success.
    if (civicrm_api3('setting', 'getvalue', array('group' => 'eway', 'name' => 'eway_developer_mode'))) {
        return civicrm_api3_create_success(array($params['managed_customer_id'] => array('trxn_id' => uniqid())), $params);
    }
    $client = CRM_Core_Payment_EwayUtils::getClient($params['payment_processor_id']);
    $endPoint = 'https://www.eway.com.au/gateway/managedpayment/ProcessPayment';
    $paymentInfo = array('man:managedCustomerID' => $params['managed_customer_id'], 'man:amount' => $params['amount_in_cents'], 'man:InvoiceReference' => $params['invoice_id'], 'man:InvoiceDescription' => $params['description']);
    $result = $client->call('man:ProcessPayment', $paymentInfo, '', $endPoint);
    if (empty($result)) {
        throw new API_Exception('No response from eWay.');
    }
    if (!empty($result['faultcode'])) {
        throw new API_Exception($result['faultstring']);
    }
    if ($result['ewayTrxnStatus'] == 'True') {
        return civicrm_api3_create_success(array($params['managed_customer_id'] => array('trxn_id' => $result['ewayTrxnNumber'])), $params);
    } else {
        if (!empty($result['ewayTrxnError'])) {
            throw new API_Exception($result['ewayTrxnError']);
        }
        throw new API_Exception('unknown EWAY processing error');
    }
}
/**
 * Query Eway customer token details.
 *
 * @param array $params
 *
 * @throws API_Exception
 * @throws CiviCRM_API3_Exception
 *
 * @return array
 *   API Result array
 */
function civicrm_api3_ewayrecurring_tokenquery($params)
{
    $result = array();
    if (empty($params['managed_customer_id'])) {
        $recur = civicrm_api3('contribution_recur', 'getsingle', array('id' => $params['contribution_recur_id']));
        $params['managed_customer_id'] = (double) $recur['processor_id'];
        $params['payment_processor_id'] = $recur['payment_processor_id'];
    }
    $client = CRM_Core_Payment_EwayUtils::getClient($params['payment_processor_id']);
    $endPoint = 'https://www.eway.com.au/gateway/managedpayment/QueryCustomer';
    $ewayData = array('man:managedCustomerID' => $params['managed_customer_id']);
    $ewayResult = $client->call('man:QueryCustomer', $ewayData, '', $endPoint);
    if (empty($ewayResult)) {
        throw new API_Exception('No response from eWay.');
    }
    if (!empty($ewayResult['faultcode'])) {
        throw new API_Exception($ewayResult['faultstring']);
    }
    $result[$params['managed_customer_id']] = array_merge($ewayResult, array('contact_id' => $recur['contact_id'], 'code' => $params['managed_customer_id'], 'reference' => $ewayResult['CCNumber'], 'email' => $ewayResult['CustomerEmail'], 'expiry_date' => date('Y-m-d', strtotime($ewayResult['CCExpiryYear'] . '-' . $ewayResult['CCExpiryMonth'] . '-01')), 'payment_processor_id' => $recur['payment_processor_id']));
    return civicrm_api3_create_success($result, $params);
}
/**
 * Get the eWAY recurring payment processors as an array of client objects.
 *
 * @param $domainID
 *
 * @throws CiviCRM_API3_Exception
 *
 * @return array
 *   An associative array of Processor Id => eWAY Token Client
 */
function get_eway_token_clients($domainID)
{
    $params = array('class_name' => 'Payment_Ewayrecurring', 'return' => 'id');
    if (!empty($domainID)) {
        $params['domain_id'] = $domainID;
    }
    $processors = civicrm_api3('payment_processor', 'get', $params);
    $result = array();
    foreach (array_keys($processors['values']) as $id) {
        $result[$id] = CRM_Core_Payment_EwayUtils::getClient($id);
    }
    return $result;
}