Недавно я интегрировал авторизацию Authorize.net в сайт. Теперь я должен интегрироваться еще раз, но с регулярным ежемесячным выставлением счетов. Я использовал sdk, который они предоставляют в качестве примера, и мне было интересно, может ли кто-нибудь сделать какую-то проверку работоспособности и сказать мне, где я отказываюсь.
Примечание: я новичок в php.
Это код, который они предоставляют
<?php
require 'vendor/autoload.php';
require_once 'constants/SampleCodeConstants.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
date_default_timezone_set('San Juan/Puerto Rico');
define("AUTHORIZENET_LOG_FILE", "phplog");
function createSubscription($intervalLength)
{
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(\SampleCodeConstants::MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(\SampleCodeConstants::MERCHANT_TRANSACTION_KEY);
$refId = 'ref' . time();
// Subscription Type Info
$subscription = new AnetAPI\ARBSubscriptionType();
$subscription->setName("Sample Subscription");
$interval = new AnetAPI\PaymentScheduleType\IntervalAType();
$interval->setLength($intervalLength);
$interval->setUnit("days");
$paymentSchedule = new AnetAPI\PaymentScheduleType();
$paymentSchedule->setInterval($interval);
$paymentSchedule->setStartDate(new DateTime('2020-08-30'));
$paymentSchedule->setTotalOccurrences("12");
$paymentSchedule->setTrialOccurrences("1");
$subscription->setPaymentSchedule($paymentSchedule);
$subscription->setAmount(rand(1,99999)/12.0*12);
$subscription->setTrialAmount("0.00");
$creditCard = new AnetAPI\CreditCardType($cardDetails);
$creditCard->setCardNumber("card-number");
$creditCard->setExpirationDate( $cardDetails["year"] . "-" . $cardDetails["month"]);
$creditCard->setCardCode($cardDetails["cardCode"]);
$payment = new AnetAPI\PaymentType();
$payment->setCreditCard($creditCard);
// Subscription //
$subscription->setPayment($payment);
$order = new AnetAPI\OrderType();
$order->setInvoiceNumber("1234354");
$order->setDescription("Description of the subscription");
$subscription->setOrder($order);
// Duda en esta Programación //
$billTo = new AnetAPI\NameAndAddressType();
$billTo->setFirstName("John");
$billTo->setLastName("Smith");
$billTo->setCompany("");
$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);
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") )
{
echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n";
}
else
{
echo "ERROR : Invalid response\n";
$errorMessages = $response->getMessages()->getMessage();
echo "Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n";
}
return $response;
}
if(!defined('DONT_RUN_SAMPLES'))
createSubscription(23);
?>
Я сделал некоторые изменения, которые в прошлом работали с единой интеграцией платежей, которую я сделал. Пока это то, что я получил …
Это мой код
<?php
require 'sample-code-php-master/vendor/autoload.php';
require_once 'sample-code-php-master/constants/SampleCodeConstants.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
class AuthorizeNetSubscription
{
private $APILoginId;
private $APIKey;
private $refId;
private $merchantAuthentication;
public $responseText;
public function __construct()
{
require_once "config.php";
$this->APILoginId = API_LOGIN_ID;
$this->APIKey = TRANSACTION_KEY;
$this->refId = 'ref' . time();
$this->merchantAuthentication = $this->setMerchantAuthentication();
$this->responseText = array("1"=>"Approved", "2"=>"Declined", "3"=>"Error", "4"=>"Held for Review");
}
public function setMerchantAuthentication()
{
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($this->APILoginId);
$merchantAuthentication->setTransactionKey($this->APIKey);
return $merchantAuthentication;
}
public function setCreditCard($cardDetails)
{
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber($cardDetails["card-number"]);
$creditCard->setExpirationDate( $cardDetails["year"] . "-" . $cardDetails["month"]);
$creditCard->setCardCode($cardDetails["cardCode"]);
$paymentType = new AnetAPI\PaymentType();
$paymentType->setCreditCard($creditCard);
return $paymentType;
}
public function createSubscription($intervalLength)
{
$subscription = new AnetAPI\ARBSubscriptionType();
$subscription->setName("Sample Subscription");
$interval = new AnetAPI\PaymentScheduleType\IntervalAType();
$interval->setLength($intervalLength);
$interval->setUnit("days");
}
public function setPaymentSchedule(){
$paymentSchedule = new AnetAPI\PaymentScheduleType();
$paymentSchedule->setInterval($interval);
$paymentSchedule->setStartDate(new DateTime('2020-08-30'));
$paymentSchedule->setTotalOccurrences("12");
$paymentSchedule->setTrialOccurrences("1");
}
function setSubscription(){
$subscription->setPaymentSchedule($paymentSchedule);
$subscription->setAmount(rand(1, 99999) / 12.0 * 12);
$subscription->setTrialAmount("0.00");
$subscription->setPayment($payment);
}
function setOrderType(){
$order = new AnetAPI\OrderType();
$order->setInvoiceNumber("1234354");
$order->setDescription("Description of the subscription");
$subscription->setOrder($order);
return $subscription;
}
public function setBillTo($billTo)
{
$address = new AnetAPI\CustomerAddressType();
$address->setFirstName($billTo["firstName"]);
$address->setLastName($billTo["lastName"]);
$address->setCompany($billTo["company"]);
$address->setAddress($billTo["address"]);
$address->setCity($billTo["city"]);
$address->setState($billTo["state"]);
$address->setZip($billTo["zip"]);
$address->setCountry($billTo["country"]);
$address->setPhoneNumber($billTo["phoneNumber"]);
return $address;
}
public function chargeCreditCard($cardDetails)
{
$paymentType = $this->setCreditCard($_POST);
$address = $this->setBillTo($_POST);
$transactionRequestType = $this->setTransactionRequestType($paymentType, $_POST["amount"],$address);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($this->merchantAuthentication);
$request->setRefId( $this->refId);
$request->setTransactionRequest($transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
return $response;
}
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
echo "SUCCESS: Subscription ID : " . $response->getSubscriptionId() . "\n";
} else {
echo "ERROR : Invalid response\n";
$errorMessages = $response->getMessages()->getMessage();
echo "Response : " . $errorMessages[0]->getCode() . " " . $errorMessages[0]->getText() . "\n";
}
return $response;
}
if(!defined('DONT_RUN_SAMPLES'))
createSubscription(23);
?>
Я хочу знать, правильно ли я иду, если нет, какие-либо предложения о том, как интегрировать периодическое выставление счетов?
Задача ещё не решена.
Других решений пока нет …