private function charge_credit_card() { // Common setup for API credentials $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); $merchantAuthentication->setName($this->api_login_id); $merchantAuthentication->setTransactionKey($this->api_transaction_key); $refId = 'ref' . time(); // Create the payment data for a credit card $creditCard = new AnetAPI\CreditCardType(); $creditCard->setCardNumber($this->card_number); $creditCard->setExpirationDate($this->expiration_year . '-' . $this->expiration_month); $paymentOne = new AnetAPI\PaymentType(); $paymentOne->setCreditCard($creditCard); // Order info $order = new AnetAPI\OrderType(); $order->setInvoiceNumber($this->invoice_number); $order->setDescription(get_bloginfo('name')); $cart_contents = $this->purchase_log->get_cart_contents(); $lineitems = array(); // Line Item Info foreach ($cart_contents as $index => $item) { // this is for the product options support that can be used in place of variations if (defined('OPTION_BASE')) { $options = wpsc_get_cart_item_meta($item->id, OPTION_BASE, true); if (!empty($options)) { $options_message = strip_tags($options->message()); } } $custom_description = $item->name . ' ' . $options_message . ' ' . $item->custom_message; $lineitems[$index] = new AnetAPI\LineItemType(); $lineitems[$index]->setItemId($item->prodid); $lineitems[$index]->setName(substr($item->name, 0, 31)); $lineitems[$index]->setDescription(substr($custom_description, 0, 255)); $lineitems[$index]->setQuantity($item->quantity); $lineitems[$index]->setUnitPrice($this->force_two_decimals($item->price)); $lineitems[$index]->setTaxable(0.0 != floatval($item->tax_charged)); } // Tax info $tax = new AnetAPI\ExtendedAmountType(); $tax->setName("Sales Tax"); $tax->setAmount($this->force_two_decimals($this->purchase_log->get('wpec_taxes_total'))); $tax->setDescription("Sales Tax"); // Customer info $customer = new AnetAPI\CustomerDataType(); $wp_user = get_user_by('email', $this->checkout_data->get('billingemail')); if ($wp_user) { $customer->setId($wp_user->ID); } $customer->setEmail($this->checkout_data->get('billingemail')); // PO Number $ponumber = $this->checkout_data->get('billingponumber'); //Ship To Info $shipto = new AnetAPI\NameAndAddressType(); $shipto->setFirstName($this->checkout_data->get('shippingfirstname')); $shipto->setLastName($this->checkout_data->get('shippinglastname')); // $shipto->setCompany( $this->checkout_data->get( 'shippingcompany') ); $shipto->setAddress($this->checkout_data->get('shippingaddress')); $shipto->setCity($this->checkout_data->get('shippingcity')); $shipto->setState($this->checkout_data->get('shippingstate')); $shipto->setZip($this->checkout_data->get('shippingpostcode')); $shipto->setCountry($this->checkout_data->get('shippingcountry')); // Bill To $billto = new AnetAPI\CustomerAddressType(); $billto->setFirstName($this->checkout_data->get('billingfirstname')); $billto->setLastName($this->checkout_data->get('billinglastname')); // $billto->setCompany( $this->checkout_data->get( 'billingcompany') ); $billto->setAddress($this->checkout_data->get('billingaddress')); $billto->setCity($this->checkout_data->get('billingcity')); $billto->setState($this->checkout_data->get('billingstate')); $billto->setZip($this->checkout_data->get('billingpostcode')); $billto->setCountry($this->checkout_data->get('billingcountry')); $billto->setPhoneNumber($this->checkout_data->get('billingphone')); //create a transaction $transactionRequestType = new AnetAPI\TransactionRequestType(); foreach ($lineitems as $lineitem) { $transactionRequestType->addToLineItems($lineitem); } $transactionRequestType->setTransactionType("authCaptureTransaction"); $transactionRequestType->setAmount($this->force_two_decimals($this->purchase_log->get('totalprice'))); $transactionRequestType->setTax($tax); $transactionRequestType->setPayment($paymentOne); $transactionRequestType->setOrder($order); if (!empty($ponumber)) { $transactionRequestType->setPoNumber($ponumber); } $transactionRequestType->setCustomer($customer); $transactionRequestType->setBillTo($billto); $transactionRequestType->setShipTo($shipto); if (!empty($_SERVER['REMOTE_ADDR'])) { $transactionRequestType->setCustomerIP($_SERVER['REMOTE_ADDR']); } $request = new AnetAPI\CreateTransactionRequest(); $request->setMerchantAuthentication($merchantAuthentication); $request->setRefId($refId); $request->setTransactionRequest($transactionRequestType); $controller = new AnetController\CreateTransactionController($request); if ($this->sandbox_mode) { $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); } else { $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION); } $result = false; if ($response != null) { $tresponse = $response->getTransactionResponse(); if ($tresponse != null) { // see http://developer.authorize.net/api/reference/ for definitions if ($tresponse->getResponseCode() == "1") { // 1 = Approved $this->set_purchaselog_status(WPSC_Purchase_Log::ACCEPTED_PAYMENT); $result = true; } elseif ($tresponse->getResponseCode() == "2") { // 2 = Declined $this->set_purchaselog_status(WPSC_Purchase_Log::INCOMPLETE_SALE); $result = true; } elseif ($tresponse->getResponseCode() == "3") { // 3 = Error $this->set_purchaselog_status(WPSC_Purchase_Log::INCOMPLETE_SALE); $result = false; } elseif ($tresponse->getResponseCode() == "4") { // 4 = Held for Review $this->set_purchaselog_status(WPSC_Purchase_Log::INCOMPLETE_SALE); $result = true; } else { // Unknown transaction code $this->set_purchaselog_status(WPSC_Purchase_Log::INCOMPLETE_SALE); error_log(__CLASS__ . '::' . __FUNCTION__ . ' ' . "ERROR: Charge Credit Card ERROR : Unknown transaction response code"); } wpsc_update_purchase_meta($this->invoice_number, 'pbci_auth_net_raw_response', $tresponse); $messages = wpsc_get_customer_meta('checkout_misc_error_messages'); if (!is_array($messages)) { $messages = array(); } // get the transaction error messages $transaction_error_messages = $response->getMessages(); if ($transaction_error_messages) { $transaction_error_messages = $transaction_error_messages->getMessage(); } if (!is_array($transaction_error_messages)) { $transaction_error_messages = array($transaction_error_messages); } foreach ($transaction_error_messages as $error_message) { $messages[] = $error_message->getText(); } // get the transaction response error messages $transaction_errors = $tresponse->getErrors(); foreach ($transaction_errors as $transaction_error) { $messages[] = $transaction_error->getErrorText(); } wpsc_update_customer_meta('checkout_misc_error_messages', $messages); $this->purchase_log->set('transactid', $tresponse->getTransId()); $this->purchase_log->set('authcode', $tresponse->getAuthCode()); } else { error_log(__CLASS__ . '::' . __FUNCTION__ . ' ' . "ERROR: Charge Credit Card ERROR : Invalid response"); } } else { error_log(__CLASS__ . '::' . __FUNCTION__ . ' ' . "ERROR: Charge Credit card Null response returned"); } $this->purchase_log->save(); return $result; }
$lineitem = new AnetAPI\LineItemType(); $lineitem->setItemId("Shirts"); $lineitem->setName("item1"); $lineitem->setDescription("golf shirt"); $lineitem->setQuantity("1"); $lineitem->setUnitPrice(20.95); $lineitem->setTaxable(false); // Tax info $tax = new AnetAPI\ExtendedAmountType(); $tax->setName("level 2 tax name"); $tax->setAmount(4.5); $tax->setDescription("level 2 tax"); // Customer info $customer = new AnetAPI\CustomerDataType(); $customer->setId("15"); $customer->setEmail("*****@*****.**"); // PO Number $ponumber = "15"; //Ship To Info $shipto = new AnetAPI\NameAndAddressType(); $shipto->setFirstName("Bayles"); $shipto->setLastName("China"); $shipto->setCompany("Thyme for Tea"); $shipto->setAddress("12 Main Street"); $shipto->setCity("Pecan Springs"); $shipto->setState("TX"); $shipto->setZip("44628"); $shipto->setCountry("USA"); // Bill To $billto = new AnetAPI\CustomerAddressType(); $billto->setFirstName("Ellen");
protected function processPayment(Application $app, $cardInfo, $amount, $user, $students, $bill_address) { define("AUTHORIZENET_LOG_FILE", "../authorize.net.log"); // Common setup for API credentials $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); $merchantAuthentication->setName($app['authrize.net.name']); $merchantAuthentication->setTransactionKey($app['authrize.net.key']); $refId = 'ref' . time(); // Create the payment data for a credit card $creditCard = new AnetAPI\CreditCardType(); $creditCard->setCardNumber($cardInfo->card_num); $creditCard->setExpirationDate($cardInfo->exp); $creditCard->setCardCode($cardInfo->code); $paymentOne = new AnetAPI\PaymentType(); $paymentOne->setCreditCard($creditCard); // Order info // $order = new AnetAPI\OrderType(); // $order->setInvoiceNumber($order->getInvoiceNumber()); // $order->setDescription('Payment for '.implode(' ', $students)); // Line Item Info // $lineitem = new AnetAPI\LineItemType(); // $lineitem->setItemId("Shirts"); // $lineitem->setName("item1"); // $lineitem->setDescription("golf shirt"); // $lineitem->setQuantity("1"); // $lineitem->setUnitPrice(20.95); // $lineitem->setTaxable("Y"); // Tax info // $tax = new AnetAPI\ExtendedAmountType(); // $tax->setName("level 2 tax name"); // $tax->setAmount(4.50); // $tax->setDescription("level 2 tax"); // Customer info $customer = new AnetAPI\CustomerDataType(); $customer->setId($user->getUserId()); $customer->setEmail($user->getEmail()); // PO Number // $ponumber = "15"; //Ship To Info // $shipto = new AnetAPI\NameAndAddressType(); // $shipto->setFirstName("Bayles"); // $shipto->setLastName("China"); // $shipto->setCompany("Thyme for Tea"); // $shipto->setAddress("12 Main Street"); // $shipto->setCity("Pecan Springs"); // $shipto->setState("TX"); // $shipto->setZip("44628"); // $shipto->setCountry("USA"); // Bill To $billto = new AnetAPI\CustomerAddressType(); $billto->setFirstName($user->getFirstName()); $billto->setLastName($user->getLastName()); $billto->setCompany(""); $billto->setAddress($bill_address->getStreet()); $billto->setCity($bill_address->getCity()); $billto->setState($bill_address->getState()); $billto->setZip($bill_address->getZip()); $billto->setCountry("USA"); //create a transaction $transactionRequestType = new AnetAPI\TransactionRequestType(); $transactionRequestType->setTransactionType("authCaptureTransaction"); $transactionRequestType->setAmount($amount); $transactionRequestType->setPayment($paymentOne); // $transactionRequestType->setOrder($order); // $transactionRequestType->addToLineItems($lineitem); // $transactionRequestType->setTax($tax); // $transactionRequestType->setPoNumber($ponumber); $transactionRequestType->setCustomer($customer); $transactionRequestType->setBillTo($billto); // $transactionRequestType->setShipTo($shipto); $request = new AnetAPI\CreateTransactionRequest(); $request->setMerchantAuthentication($merchantAuthentication); $request->setRefId($refId); $request->setTransactionRequest($transactionRequestType); $controller = new AnetController\CreateTransactionController($request); $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); if (null != $response) { $tresponse = $response->getTransactionResponse(); if ($tresponse != null && $tresponse->getResponseCode() == "1") { // return array( // "AUTH_CODE" => $tresponse->getAuthCode(), // "TRANS_ID" => $tresponse->getTransId() // ); return array('approved' => true, 'data' => $tresponse); } else { return array('approved' => false, 'data' => $tresponse); } } else { return array('approved' => false, 'data' => null); } }
function make_transaction2($post_order) { //echo "<pre>"; //print_r($post_order); //echo "</pre><br>-------------------------------<br>"; // Create the payment data for credit card $payment = $this->prepare_order($post_order); //$merchantAuthentication = $this->sandbox_authorize(); $merchantAuthentication = $this->authorize(); $refId = 'ref' . time(); // Order info $invoiceNo = time(); $order = new AnetAPI\OrderType(); $order->setInvoiceNumber($invoiceNo); if ($order->group == 0) { $order->setDescription($post_order->item); $lineitem = new AnetAPI\LineItemType(); $lineitem->setItemId(time()); $lineitem->setName($post_order->item); $lineitem->setDescription($post_order->item); $lineitem->setQuantity("1"); $lineitem->setUnitPrice($post_order->sum); $lineitem->setTaxable("N"); } else { $order->setDescription($post_order->item); $lineitem = new AnetAPI\LineItemType(); $lineitem->setItemId(time()); $lineitem->setName("{$post_order->item}"); $lineitem->setDescription($post_order->item); $lineitem->setQuantity("1"); $lineitem->setUnitPrice($post_order->sum); $lineitem->setTaxable("N"); } // end else // Customer info $custID = round(time() / 3785); $customer = new AnetAPI\CustomerDataType(); $customer->setId($custID); $customer->setEmail($post_order->cds_email); $names = explode("/", $post_order->cds_name); $firstname = $names[0]; $lastname = $names[1]; //Ship To Info $shipto = new AnetAPI\NameAndAddressType(); $shipto->setFirstName($firstname); $shipto->setLastName($lastname); $shipto->setCompany('Student'); $shipto->setAddress($post_order->cds_address_1); $shipto->setCity($post_order->cds_city); $shipto->setState($post_order->cds_state); $shipto->setZip($post_order->cds_zip); $shipto->setCountry("USA"); // Bill To $billto = new AnetAPI\CustomerAddressType(); $billto->setFirstName($firstname); $billto->setLastName($lastname); $billto->setCompany("Student"); $billto->setAddress($post_order->cds_address_1); $billto->setCity($post_order->cds_city); $billto->setState($post_order->cds_state); $billto->setZip($post_order->cds_zip); $billto->setCountry("USA"); //create a transaction $transactionRequestType = new AnetAPI\TransactionRequestType(); $transactionRequestType->setTransactionType("authCaptureTransaction"); $transactionRequestType->setAmount($post_order->sum); $transactionRequestType->setPayment($payment); $transactionRequestType->setOrder($order); $transactionRequestType->addToLineItems($lineitem); $transactionRequestType->setCustomer($customer); $transactionRequestType->setBillTo($billto); $transactionRequestType->setShipTo($shipto); $request = new AnetAPI\CreateTransactionRequest(); $request->setMerchantAuthentication($merchantAuthentication); $request->setRefId($refId); $request->setTransactionRequest($transactionRequestType); $controller = new AnetController\CreateTransactionController($request); //$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION); //echo "--------Card payment response1 <pre>"; //print_r($response); //echo "</pre><br>"; if ($response != null) { $tresponse = $response->getTransactionResponse(); //echo "--------Card payment response2 <pre>"; //print_r($tresponse); //echo "</pre><br>"; //die(); if ($tresponse != null && $tresponse->getResponseCode() == "1") { //echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n"; //echo "Charge Credit Card TRANS ID : " . $tresponse->getTransId() . "\n"; $status = array('auth_code' => $tresponse->getAuthCode(), 'trans_id' => $tresponse->getTransId(), 'auth_code' => $tresponse->getResponseCode(), 'sum' => $post_order->sum); return $status; } else { $this->save_log($tresponse, $post_order); return false; } } else { //echo "Charge Credit card Null response returned"; return false; } }
function createSubscription($post_order) { // Common Set Up for API Credentials $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); $merchantAuthentication->setName($this->LOGIN_ID); $merchantAuthentication->setTransactionKey($this->TRANSACTION_KEY); $intervalLength = round($this->period / $post_order->payments_num); $refId = 'ref' . time(); $start_date_h = date('Y-m-d', time()); // first subscription payment today $total_occurences = $post_order->payments_num; $expiration = $post_order->cds_cc_year . "-" . $post_order->cd_cc_month; $names = explode("/", $post_order->cds_name); // Customer info $custID = round(time() / 3785); $customer = new AnetAPI\CustomerDataType(); $customer->setId($custID); $customer->setEmail($post_order->cds_email); /* * echo "<br>--------------------<br>"; print_r($names); echo "<br>--------------------<br>"; echo "First name: ".$firstname."<br>"; echo "Last name: ".$lastname."<br>"; * */ $firstname = $names[0]; $lastname = $names[1]; //$firstname = ($names[0] == '') ? "Loyal" : $names[0]; //$lastname = ($names[2] == '') ? 'Client' : $names[2]; // Subscription Type Info $subscription = new AnetAPI\ARBSubscriptionType(); $subscription->setName("Subscription for {$post_order->item}"); $interval = new AnetAPI\PaymentScheduleType\IntervalAType(); $interval->setLength($intervalLength); $interval->setUnit("days"); $paymentSchedule = new AnetAPI\PaymentScheduleType(); $paymentSchedule->setInterval($interval); $paymentSchedule->setStartDate(new DateTime($start_date_h)); $paymentSchedule->setTotalOccurrences($total_occurences); $paymentSchedule->setTrialOccurrences("1"); $subscription->setPaymentSchedule($paymentSchedule); $subscription->setAmount($post_order->sum); $subscription->setTrialAmount("0.00"); $creditCard = new AnetAPI\CreditCardType(); $creditCard->setCardNumber($post_order->cds_cc_number); $creditCard->setExpirationDate($expiration); $payment = new AnetAPI\PaymentType(); $payment->setCreditCard($creditCard); $subscription->setPayment($payment); $billTo = new AnetAPI\NameAndAddressType(); $billTo->setFirstName($firstname); $billTo->setLastName($lastname); $subscription->setBillTo($billTo); $request = new AnetAPI\ARBCreateSubscriptionRequest(); $request->setmerchantAuthentication($merchantAuthentication); $request->setRefId($refId); $request->setSubscription($subscription); $controller = new AnetController\ARBCreateSubscriptionController($request); //$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION); /* * echo "--------Subscription response <pre>"; print_r($response); echo "<br>-------------------------<br>"; die('Stopped ....'); * */ if ($response != null && $response->getMessages()->getResultCode() == "Ok") { $msg = $response->getSubscriptionId(); //echo "Message: ".$msg."<br>"; } else { $this->save_log($response); $errorMessages = $response->getMessages()->getMessage(); $msg = $errorMessages[0]->getCode() . " " . $errorMessages[0]->getText(); } // end else return $msg; }