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; }
// For card information you can pass exactly what comes back from an GetCustomerPaymentProfile // if you don't need to update that info $creditCard = new AnetAPI\CreditCardType(); $creditCard->setCardNumber("XXXX1111"); $creditCard->setExpirationDate("XXXX"); $paymentCreditCard = new AnetAPI\PaymentType(); $paymentCreditCard->setCreditCard($creditCard); // Create the Bill To info for new payment type $billto = new AnetAPI\CustomerAddressType(); $billto->setFirstName("Mrs Mary"); $billto->setLastName("Doe"); $billto->setAddress("1 New St."); $billto->setCity("Brand New City"); $billto->setState("WA"); $billto->setZip("98004"); $billto->setPhoneNumber("000-000-0000"); $billto->setfaxNumber("999-999-9999"); $billto->setCountry("USA"); // Create the Customer Payment Profile object $paymentprofile = new AnetAPI\CustomerPaymentProfileExType(); $paymentprofile->setCustomerPaymentProfileId("33211899"); $paymentprofile->setBillTo($billto); $paymentprofile->setPayment($paymentCreditCard); // Submit a UpdatePaymentProfileRequest $request->setPaymentProfile($paymentprofile); $controller = new AnetController\UpdateCustomerPaymentProfileController($request); $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); if ($response != null && $response->getMessages()->getResultCode() == "Ok") { echo "Update Customer Payment Profile SUCCESS: " . "\n"; // Update only returns success or fail, if success // confirm the update by doing a GetCustomerPaymentProfile
// Common setup for API credentials $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); $merchantAuthentication->setName("556KThWQ6vf2"); $merchantAuthentication->setTransactionKey("9ac2932kQ7kN2Wzq"); // An existing customer profile id for this merchant name and transaction key $existingcustomerprofileid = "35872074"; // Create the customer shipping address $customershippingaddress = new AnetAPI\CustomerAddressType(); $customershippingaddress->setFirstName("James"); $customershippingaddress->setLastName("White"); $customershippingaddress->setCompany("Addresses R Us"); $customershippingaddress->setAddress(rand() . " North Spring Street"); $customershippingaddress->setCity("Toms River"); $customershippingaddress->setState("NJ"); $customershippingaddress->setZip("08753"); $customershippingaddress->setCountry("USA"); $customershippingaddress->setPhoneNumber("000-000-0000"); $customershippingaddress->setFaxNumber("999-999-9999"); // Create a new customer shipping address for an existing customer profile $request = new AnetAPI\CreateCustomerShippingAddressRequest(); $request->setMerchantAuthentication($merchantAuthentication); $request->setCustomerProfileId($existingcustomerprofileid); $request->setAddress($customershippingaddress); $controller = new AnetController\CreateCustomerShippingAddressController($request); $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX); if ($response != null && $response->getMessages()->getResultCode() == "Ok") { echo "Create Customer Shipping Address SUCCESS: ADDRESS ID : " . $response->getCustomerAddressId() . "\n"; } else { echo "Create Customer Shipping Address ERROR : Invalid response\n"; echo "Response : " . $response->getMessages()->getMessage()[0]->getCode() . " " . $response->getMessages()->getMessage()[0]->getText() . "\n"; }
// Create the payment data for a credit card $creditCard = new AnetAPI\CreditCardType(); $creditCard->setCardNumber("4012888818888"); $creditCard->setExpirationDate("2038-11"); $paymentCreditCard = new AnetAPI\PaymentType(); $paymentCreditCard->setCreditCard($creditCard); // Create the Bill To info for new payment type $billto = new AnetAPI\CustomerAddressType(); $billto->setFirstName("Jane"); $billto->setLastName("Doe"); $billto->setCompany("My company"); $billto->setAddress("588 Willis Ct"); $billto->setCity("Vacaville"); $billto->setState("CA"); $billto->setZip("95688"); $billto->setPhoneNumber("555-555-1212"); $billto->setfaxNumber("999-999-9999"); $billto->setCountry("USA"); // Create a new Customer Payment Profile $paymentprofile = new AnetAPI\CustomerPaymentProfileExType(); $paymentprofile->setCustomerType('individual'); $paymentprofile->setBillTo($billto); $paymentprofile->setPayment($paymentCreditCard); $paymentprofile->setCustomerPaymentProfileId($getcustomerprofileid); $paymentprofiles[] = $paymentprofile; // Submit a UpdatePaymentProfileRequest to update an existing create a new Customer Payment Profile $paymentprofilerequest = new AnetAPI\UpdateCustomerPaymentProfileRequest(); $paymentprofilerequest->setMerchantAuthentication($merchantAuthentication); $paymentprofilerequest->setCustomerProfileId($getcustomerprofileid); $paymentprofilerequest->setPaymentProfile($paymentprofile); $paymentprofilerequest->setRefId($refId);