cancel
Showing results for 
Search instead for 
Did you mean: 

Desperately Need PHP Code Samples For Dynamic Pricing

Can anyone please provide some php code samples for dynamic pricing?  We have been stuck on this for over a month and it's nowhere to be found in the documentation.  Please if you can give, post a reply.  Thank you.

ssgdev
Member
9 REPLIES 9

@ssgdev It may help if you provide a more detailed description of what you are trying to accomplish with Authorize.Net.

 

Richard

RichardH
Administrator Administrator
Administrator

We are going to create invoices for our customers. So, we need to create dynamic pricing. For example, suppose one customer needs to pay $50, another needs to pay $96, and another needs to pay $76 etc., For this we need to send the price details to Authorize.net. How do send it?  For that, we need step by step process flow with sample code.  Can you please provide?

@ssgdev

 

Our PHP SDK and sample code allows you to accept payment,  but we don't offer code to implement sending invoices and presenting a payment page to the user in response to the invoice.  Instead, you'll need to create that interim page within your own code.

 

Richard 

After going through the php sdk sample, we found it is in pure php.  We are working within "CodeIgniter" web framework (CI).   This means for us it needs to be converted in php CodeIgniter.   Do you have any specific relevant instructions on php CodeIgniter?  This is where we are currently stuck.

Using the Authorize.net payment gateway in PHP is an easy task. All you need is the official PHP SDK and the code below. You can implement it as a standalone application or integrate with frameworks like Laravel, Codeigniter, and others.

 

If using composer, add the following to your composer.json file

 

{
"require": {
"php": ">=5.5",
"ext-curl": "*",
"phpunit/phpunit": "~4.8||~6.0",
"authorizenet/authorizenet": "1.9.3"
},
"autoload": {
"classmap": ["constants"]
}
}


Run composer install.

 

 

Once this process is completed, you should have a vendor folder created in your project directory.

 

Alternatively, you can download the Authorize.net PHP SDK. Extract it to your project directory and run composer update command.

  

require 'vendor/autoload.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;

 If your not using Composer. Authorize.net provides a custom SPL autoloader. Just download the SDK and point to its autoload.php file with:

 

require 'path/to/anet_php_sdk/autoload.php';

Go to the constants directory and open up the constants.php file. Set the up login_id and transaction_key that you have with your Authorize.net sandbox account.

 

<?php
require 'vendor/autoload.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
define("AUTHORIZENET_LOG_FILE", "phplog");
function chargeCreditCard($amount) {
// Common setup for API credentials
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY);
$refId = 'ref' . time();

//While testing, create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4111111111111111");
$creditCard->setExpirationDate("1227");
$creditCard->setCardCode("123");
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);
$order = new AnetAPI\OrderType();
$order->setDescription("New Order Description");
//create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
$transactionRequestType->setAmount($amount);
$transactionRequestType->setOrder($order);
$transactionRequestType->setPayment($paymentOne);

//Prepare customer information object from your form $_POST data
$cust = new AnetAPI\CustomerAddressType();
$cust->setFirstName($_POST['fname']);
$cust->setLastName($_POST['lname']);
$cust->setAddress($_POST['address']);
$cust->setCity($_POST['city']);
$cust->setState($_POST['state']);
$cust->setCountry($_POST['country']);
$cust->setZip($_POST['zip']);
$cust->setPhoneNumber($_POST['phone']);
$cust->setEmail("Email-here");
$transactionRequestType->setBillTo($cust);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setTransactionRequest($transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);

//Get response from Authorize.net
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
if ($response != null) {
if ($response->getMessages()->getResultCode() == \SampleCode\Constants::RESPONSE_OK) {
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null) {
echo " Transaction Response code : " . $tresponse->getResponseCode() . "\n";
echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
echo "Charge Credit Card TRANS ID : " . $tresponse->getTransId() . "\n";
echo " Code : " . $tresponse->getMessages()[0]->getCode() . "\n";
echo " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n";
} else {
echo "Transaction Failed \n";
if ($tresponse->getErrors() != null) {
echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
}
}
} else {
echo "Transaction Failed \n";
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getErrors() != null) {
echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
} else {
echo " Error code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
echo " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
}
}
} else {
echo "No response returned \n";
}
return $response;
}
$amount = \SampleCode\Constants::SAMPLE_AMOUNT;
if (!empty($_POST['amount']))
$amount = $_POST['amount'];
if (!defined('DONT_RUN_SAMPLES'))
chargeCreditCard($amount);

 

Powered by NexWebSites.com -
Certified Authorize.net developers
NexusSoftware
Trusted Contributor

Please please PLEASE provide an example of what you're talking about when you say "Using the Authorize.net payment gateway in PHP is an easy task. All you need is the official PHP SDK and the code below. You can implement it as a standalone application or integrate with frameworks like Laravel, Codeigniter, and others." I have been writing PHP for over 10 years and I've never had to install anything like an SDK, nor have I heard of anything like Composer, Laravel, or the sort.

 

Is there anywhere at all which can explain what the heck we're supposed to do with Composer or anything to "install" the "SDK" so we can use it? It seems to be the first step in any tutorial I've found on Authorize.net and nowhere does it actually explain this step! It's infuriating!

 

Secondly, can we just to something simple, like move a bunch of files to our FTP and accomplish this same task?

 

(I don't like Authorize and its lack of support. That's why we usually just go with another company for money exchanges, but every so often, like now, I revisit Authorize and try to get it working.)

Hi @bparker

 

Have a look at our sample code and SDK in PHP for more details 

 

https://github.com/AuthorizeNet/sample-code-php

 

https://github.com/AuthorizeNet/sdk-php

 

Thanks

Anurag

 





Send feedback at developer_feedback@authorize.net

Thanks, but I've seen those already. They're scripts, sure, but where do I start? It doesn't look like there's a file with the actual front end form where I'd begin.

 

Also, what would I do with these? Download the zip and store them on my FTP? I've tried it with the one set of files and it looked like certain files were referencing things not supplied in the download, so maybe I'm missing something?

@bparker,

You download that code. You unzip the file and place the resulting folder in your website folder. The next step is to install the dependencies. The easiest way is to use a free dependency manager called composer.
Download composer and then run composer update. After you have successfully ran composer, you go
To the API reference and copy and paste some of the sample code on your own php file. You can call it “store.php” or “payment.php”, the name doesn’t matter.

The sample code is the “front end form” you are looking for. The files in the sdk folder can be thought of as the magic behind the scenes that makes the sample code work. You don’t have to know how it works for why. It’s useful to know both but neither is required to process payments or do anything else. No matter what you do there will be a learning curve. The step to take is to start working on it. You won’t know where you will get stuck until you are actually stuck.

I started my first integration working on the weekends in June of 2017, having never written a single line of code in my life. Google is your friend. The API reference is your friend. This forum is your friend. It will all seem easy in retrospect once you’ve done it, and it’s not unbearably difficult the first time around. You can however give up on skipping the learning curve altogether. There is a lot that goes into this. There is no way to give a step by step roadmap that is guaranteed to never have a bump for someone doing it for the first time. This forum is a hobby and I am on it at least once a day. Once you get rolling post where you get stuck and I will help you when I can.