...
✅ WEB ja WordPressi uudised, teemad, pistikprogrammid. Siin jagame näpunäiteid ja parimaid veebisaidi lahendusi.

Korduvate maksete seadistamine saidil Authorize.net

22

Oleme kirjutanud artikli Krediitkaardimaksete aktsepteerimine Authorize.net Payment Gateway abil. Selles artiklis me arutasime, kuidas saab autorise.net kaudu krediitkaardimakseid vastu võtta. Samuti keskendub see artikkel põhimõtteliselt ühekordsele maksele.

Aga mis siis, kui keegi soovib oma rakendusse integreerida korduvad maksed? Selles artiklis näitame teile, kuidas seadistada korduvaid makseid Authorize.net abil.

Mis on korduv makse?

Korduv või tellimusmakse tähendab krediitkaardilt automaatselt fikseeritud summa tasumist kindla intervalli järel ja kindla ajavahemiku jooksul.

Oletame näiteks, et kasutate oma veebisaiti Bluehosti hostis ja olete esimese aasta eest summa maksnud. Kui teie plaan aegub, võtab Bluehost teie krediitkaardimakse automaatselt järgmise aasta eest (kui olete valinud korduva makseviisi).

Tellimispaketi lubamise eeliseks on see, et te ei pea meeles pidama kasutatava teenuse aegumiskuupäeva. Teenusepakkuja hoiab seda automatiseeritud süsteemina. Kui teie plaan on aegunud, uuendavad nad teie teenust, võttes taustal krediitkaardilt maksed.

Seda öeldes vaatame, kuidas seada korduvaid makseid Authorize.net abil.

Kõigepealt peaksite lugema meie artiklit Krediitkaardimaksete aktsepteerimine Authorize.net Payment Gateway abil. Selles artiklis oleme arutanud Authorize.net SDK teekide installimise ja konfigureerimise kohta .

Õpetuse jaoks kasutame liivakasti kontot. Nii et kõigepealt peaksite looma oma liivakasti konto ja hankima oma API-võtmed. API võtmed leiate jaotisest Konto-> API mandaadid ja võtmed.

Paigaldamine

Alustamiseks peame installima PHP SDK teegi. Teegi installimiseks soovitame heliloojat.

Looge composer.jsonoma projekti juurkataloogis fail ja lisage sellesse allolev kood.

helilooja.json

{
    "require": {
        "php": ">=5.6",
        "authorizenet/authorizenet": "~1.9.6"
    }
}

Avage terminal oma projekti juurkataloogis ja käivitage composer installkäsk. See käsk laadib alla teegi sõltuvuse.

Seadistage keskkond

Oleme installinud PHP SDK teegi. Nüüd looge kaks faili nimega constants.phpja subscriptionpayment.php. Failis constants.php salvestame oma API võtmed järgmiselt.

konstandid.php

<?php
define('ANET_LOGIN_ID', 'YOUR_LOGIN_ID');
define('ANET_TRANSACTION_KEY', 'YOUR_TRANSACTION_KEY');
?>

Veenduge, et oleksite asendanud kohahoidjad YOUR_LOGIN_ID ja YOUR_TRANSACTION_KEY tegelike väärtustega.

subscriptionpayment.phpon fail, kuhu kirjutame krediitkaardi tellimusmaksete koodi. Selleks peame kõigepealt kaasama raamatukogu põhifailid.

subscriptionpayment.php

<?php
require_once "vendor/autoload.php";
require_once "constants.php";
 
use netauthorizeapicontractv1 as AnetAPI;
use netauthorizeapicontroller as AnetController;
?>

Tegelik kood korduvate maksete jaoks veebisaidil Authorize.net

Authorize.net pakub koodide teegi näidist arendajatele. Meie juhendaja jaoks kasutame viidet RecurringBillingile.

Me muudame createSubscriptionmeetodit pisut viitenumbrist ja kirjutame selle järgmiselt.

function createSubscription($arr_data = [])
{
    extract($arr_data);
    /* Create a merchantAuthenticationType object with authentication details
       retrieved from the constants file */
    $merchantAuthentication = new AnetAPIMerchantAuthenticationType();
    $merchantAuthentication->setName(ANET_LOGIN_ID);
    $merchantAuthentication->setTransactionKey(ANET_TRANSACTION_KEY);
     
    // Set the transaction's refId
    $refId = 'ref'. time();
    // Subscription Type Info
    $subscription = new AnetAPIARBSubscriptionType();
    $subscription->setName("Sample Subscription");
    $interval = new AnetAPIPaymentScheduleTypeIntervalAType();
    $interval->setLength($intervalLength);
    $interval->setUnit("days");
    $paymentSchedule = new AnetAPIPaymentScheduleType();
    $paymentSchedule->setInterval($interval);
    $paymentSchedule->setStartDate(new DateTime($start_date));
    $paymentSchedule->setTotalOccurrences($totalcycles);
    //$paymentSchedule->setTrialOccurrences("1");
    $subscription->setPaymentSchedule($paymentSchedule);
    $subscription->setAmount($amount);
    //$subscription->setTrialAmount("0.00");
     
    $creditCard = new AnetAPICreditCardType();
    $creditCard->setCardNumber($card_number);
    $creditCard->setExpirationDate($expiry_date);
    $payment = new AnetAPIPaymentType();
    $payment->setCreditCard($creditCard);
    $subscription->setPayment($payment);
    $order = new AnetAPIOrderType();
    $order->setInvoiceNumber(mt_rand(10000, 99999));   //generate random invoice number     
    $order->setDescription("Daily Subscription For 1 USD"); 
    $subscription->setOrder($order); 
     
    $billTo = new AnetAPINameAndAddressType();
    $billTo->setFirstName($first_name);
    $billTo->setLastName($last_name);
    $subscription->setBillTo($billTo);
    $request = new AnetAPIARBCreateSubscriptionRequest();
    $request->setmerchantAuthentication($merchantAuthentication);
    $request->setRefId($refId);
    $request->setSubscription($subscription);
    $controller = new AnetControllerARBCreateSubscriptionController($request);
    $response = $controller->executeWithApiResponse( netauthorizeapiconstantsANetEnvironment::SANDBOX);
     
    if (($response != null) && ($response->getMessages()->getResultCode() == "Ok"))     {
        echo "SUCCESS: Subscription ID: ". $response->getSubscriptionId(). "n";
     }
    else
    {
        echo "ERROR :  Invalid responsen";
        $errorMessages = $response->getMessages()->getMessage();
        echo "Response: ". $errorMessages[0]->getCode(). "  " .$errorMessages[0]->getText(). "n";
    }
    return $response;
}

Pange tähele $response = $controller->executeWithApiResponse(netauthorizeapiconstantsANetEnvironment::SANDBOX);ülaltoodud meetodi joont. Tootmisele üleminekul peaksime selles reas asendama SANDBOX tootmiseks. Samuti peame muutma oma sisselogimise ID-d ja tehinguvõtmeid, et need vastaksid meie reaalkontole.

Meie meetod createSubscriptionvõtab massiivi parameetri, mille peaksime allpool üles ehitama.

$arr_subscription = [
    'intervalLength' => 7, // Here 7 means recurring payment occurs after each 7 days
    'start_date' => date('Y-m-d'),
    'totalcycles' => 10, //Billing cycles. Here 10 means 10 transactions
    'amount' => 1.00,
    'card_number' => '4111111111111111',
    'expiry_date' => '2020-12',
    'first_name' => 'John',
    'last_name' => 'Smith'
];

Me kasutame test krediitkaardi numbreid esitada testimine Guide.

Masinast eespool funktsiooni edastades createSubscriptionloob see iga 7 päeva järel kuni 10 arveldustsükliga 1,00 USD liitumistasu.

Lõplik koodeks

subscriptionpaymen.phpt

<?php
require_once "vendor/autoload.php";
require_once "constants.php";
 
use netauthorizeapicontractv1 as AnetAPI;
use netauthorizeapicontroller as AnetController;
 
$arr_subscription = [
    'intervalLength' => 7,
    'start_date' => date('Y-m-d'),
    'totalcycles' => 10,
    'amount' => 1.00,
    'card_number' => '4111111111111111',
    'expiry_date' => '2020-12',
    'first_name' => 'John',
    'last_name' => 'Smith'
];
 
createSubscription($arr_subscription);
 
function createSubscription($arr_data = [])
{
    extract($arr_data);
    /* Create a merchantAuthenticationType object with authentication details
       retrieved from the constants file */
    $merchantAuthentication = new AnetAPIMerchantAuthenticationType();
    $merchantAuthentication->setName(ANET_LOGIN_ID);
    $merchantAuthentication->setTransactionKey(ANET_TRANSACTION_KEY);
     
    // Set the transaction's refId
    $refId = 'ref'. time();
    // Subscription Type Info
    $subscription = new AnetAPIARBSubscriptionType();
    $subscription->setName("Sample Subscription");
    $interval = new AnetAPIPaymentScheduleTypeIntervalAType();
    $interval->setLength($intervalLength);
    $interval->setUnit("days");
    $paymentSchedule = new AnetAPIPaymentScheduleType();
    $paymentSchedule->setInterval($interval);
    $paymentSchedule->setStartDate(new DateTime($start_date));
    $paymentSchedule->setTotalOccurrences($totalcycles);
    //$paymentSchedule->setTrialOccurrences("1");
    $subscription->setPaymentSchedule($paymentSchedule);
    $subscription->setAmount($amount);
    //$subscription->setTrialAmount("0.00");
     
    $creditCard = new AnetAPICreditCardType();
    $creditCard->setCardNumber($card_number);
    $creditCard->setExpirationDate($expiry_date);
    $payment = new AnetAPIPaymentType();
    $payment->setCreditCard($creditCard);
    $subscription->setPayment($payment);
    $order = new AnetAPIOrderType();
    $order->setInvoiceNumber(mt_rand(10000, 99999));   //generate random invoice number     
    $order->setDescription("Daily Subscription For 1 USD"); 
    $subscription->setOrder($order); 
     
    $billTo = new AnetAPINameAndAddressType();
    $billTo->setFirstName($first_name);
    $billTo->setLastName($last_name);
    $subscription->setBillTo($billTo);
    $request = new AnetAPIARBCreateSubscriptionRequest();
    $request->setmerchantAuthentication($merchantAuthentication);
    $request->setRefId($refId);
    $request->setSubscription($subscription);
    $controller = new AnetControllerARBCreateSubscriptionController($request);
    $response = $controller->executeWithApiResponse( netauthorizeapiconstantsANetEnvironment::SANDBOX);
     
    if (($response != null) && ($response->getMessages()->getResultCode() == "Ok"))     {
        echo "SUCCESS: Subscription ID: ". $response->getSubscriptionId(). "n";
     }
    else
    {
        echo "ERROR :  Invalid responsen";
        $errorMessages = $response->getMessages()->getMessage();
        echo "Response: ". $errorMessages[0]->getCode(). "  " .$errorMessages[0]->getText(). "n";
    }
    return $response;
}
?>

Loodame, et saate aru, kuidas autorize.netis korduvaid makseid seadistada. Palun jagage oma mõtteid allpool olevas kommentaaride jaotises.

See veebisait kasutab teie kasutuskogemuse parandamiseks küpsiseid. Eeldame, et olete sellega rahul, kuid saate soovi korral loobuda. Nõustu Loe rohkem