public function process(&$payment, $action)
 {
     if (!$this->validate_billing_information($payment)) {
         wp_redirect(esc_url_raw($payment->get_checkout_url()));
         die;
     }
     if ('pending' != $payment->get_status()) {
         die;
     }
     $payment->clear_errors();
     if (!class_exists('AuthorizeNetAIM')) {
         require_once WPBDP_PATH . 'vendors/anet_php_sdk/AuthorizeNet.php';
     }
     if ($payment->has_item_type('recurring_fee')) {
         // TODO: round fees not within 7-365 days (or make non-recurring).
         return $this->process_recurring($payment);
     }
     $data = $payment->get_data('billing-information');
     $aim = new AuthorizeNetAIM(wpbdp_get_option('authorize-net-login-id'), wpbdp_get_option('authorize-net-transaction-key'));
     if (wpbdp_get_option('payments-test-mode')) {
         $aim->setSandbox(true);
     } else {
         $aim->setSandbox(false);
     }
     // Order info.
     $aim->setFields(array('amount' => $payment->get_total(), 'description' => $payment->get_short_description(), 'invoice_num' => $payment->get_id()));
     // Card info.
     $aim->setFields(array('card_num' => $data['cc_number'], 'exp_date' => $data['cc_exp_month'] . substr($data['cc_exp_year'], 0, 2), 'card_code' => $data['cc_cvc']));
     // Billing addres info.
     $aim->setFields(array('first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'address' => $data['address_line1'], 'city' => $data['address_city'], 'state' => $data['address_state'], 'country' => $data['address_country'], 'zip' => $data['zipcode']));
     // TODO: maybe add zip, phone, email and cust_id
     $aim->setCustomField('payment_id', $payment->get_id());
     $aim->setCustomField('listing_id', $payment->get_listing_id());
     $response = $aim->authorizeAndCapture();
     if ($response->approved) {
         $payment->set_status(WPBDP_Payment::STATUS_COMPLETED, WPBDP_Payment::HANDLER_GATEWAY);
     } elseif ($response->error) {
         $payment->set_data('validation-errors', array(sprintf(_x('The payment gateway didn\'t accept your credit card or billing information. The following reason was given: "%s".', 'authorize-net', 'WPBDM'), '(' . $response->response_reason_code . ') ' . rtrim($response->response_reason_text, '.'))));
     } elseif ($response->held) {
         $payment->add_error(sprintf(_x('Your payment is being held for review by the payment gateway. The following reason was given: "%s".', 'authorize-net', 'WPBDM'), '(' . $response->response_reason_code . ') ' . rtrim($response->response_reason_text, '.')));
     } else {
         $payment->add_error(sprintf(_x('Payment was rejected. The following reason was given: "%s".', 'authorize-net', 'WPBDM'), '(' . $response->response_reason_code . ') ' . rtrim($response->response_reason_text, '.')));
         $payment->set_status(WPBDP_Payment::STATUS_REJECTED, WPBDP_Payment::HANDLER_GATEWAY);
     }
     $payment->save();
     wp_redirect(esc_url_raw($payment->get_redirect_url()));
     die;
 }
function authorizepayment($REQUEST)
{
    if (!checkCreditCard($REQUEST['x_card_num'], $REQUEST['card_type'], $ccerror, $ccerrortext)) {
        $_SESSION['donate_msg'] = 'Please enter a valid credit card number.';
        return false;
    } else {
        $transaction = new AuthorizeNetAIM();
        $transaction->setSandbox(AUTHORIZENET_SANDBOX);
        $transaction->setFields(array('amount' => $REQUEST['amount'], 'card_num' => $REQUEST['x_card_num'], 'exp_date' => $REQUEST['exp_month'] . '/' . $REQUEST['exp_year'], 'first_name' => $REQUEST['first_name'], 'last_name' => $REQUEST['last_name'], 'address' => $REQUEST['address'], 'city' => $REQUEST['city'], 'state' => $REQUEST['state'], 'country' => $REQUEST['country'], 'zip' => $REQUEST['zip'], 'email' => $REQUEST['email']));
        $transaction->setCustomField("Donation Form", $REQUEST["form_id"]);
        $transaction->setCustomField("Donation Type", $REQUEST["donation_type"]);
        $transaction->addLineItem("Donation", "Donation to '" . get_bloginfo("name") . "'", "Donation to '" . get_bloginfo("name") . "' using the form: " . $REQUEST["form_id"], 1, $REQUEST['amount'], false);
        $response = $transaction->authorizeAndCapture();
        if ($response->approved) {
            $_SESSION['donate_msg'] = $response->response_reason_text;
            return true;
        } else {
            $_SESSION['donate_msg'] = $response->response_reason_text;
            return false;
        }
    }
}
 public function testGetTransactionDetailsWithSolutionId()
 {
     $sale = new AuthorizeNetAIM();
     $amount = rand(1, 100);
     $sale->setCustomField('x_solution_id', 'A1000002');
     $response = $sale->authorizeAndCapture($amount, '4012888818888', '04/17');
     $this->assertTrue($response->approved);
     $transId = $response->transaction_id;
     $request = new AuthorizeNetTD();
     $response = $request->getTransactionDetails($transId);
     $this->assertTrue($response->isOk());
     $this->assertEquals($transId, (string) $response->xml->transaction->transId);
     $this->assertEquals($amount, (string) $response->xml->transaction->authAmount);
     $this->assertEquals("Visa", (string) $response->xml->transaction->payment->creditCard->cardType);
     $this->assertEquals("A1000002", (string) $response->xml->transaction->solution->id);
 }
 public function testResponseMethods()
 {
     $amount = rand(1, 1000);
     $zipcode = "02301";
     $sale = new AuthorizeNetAIM();
     $sale->setFields(array('amount' => $amount, 'card_num' => '6011000000000012', 'exp_date' => '0415', 'zip' => $zipcode));
     $sale->setCustomField("custom1", "custom1value");
     $sale->setCustomField("custom2", "custom2value");
     $result = $sale->authorizeAndCapture();
     $this->assertTrue($result->approved);
     $this->assertEquals("custom2value", $result->custom2);
     $this->assertEquals($amount, $result->amount);
     $this->assertEquals("CC", $result->method);
     $this->assertEquals("auth_capture", $result->transaction_type);
     $this->assertEquals("Discover", $result->card_type);
     $this->assertEquals($zipcode, $result->zip_code);
 }