示例#1
0
 /**
  * Make a one-time charge using the payments processor
  * @method charge
  * @param {double} $amount specify the amount (optional cents after the decimal point)
  * @param {string} [$currency='USD'] set the currency, which will affect the amount
  * @param {array} [$options=array()] Any additional options
  * @param {string} [$options.description=null] description of the charge, to be sent to customer
  * @param {string} [$options.metadata=null] any additional metadata to store with the charge
  * @param {string} [$options.subscription=null] if this charge is related to a subscription stream
  * @param {string} [$options.subscription.publisherId]
  * @param {string} [$options.subscription.streamName]
  * @throws Assets_Exception_DuplicateTransaction
  * @throws Assets_Exception_HeldForReview
  * @throws Assets_Exception_ChargeFailed
  * @return {string} The customerId of the Assets_Customer that was successfully charged
  */
 function charge($amount, $currency = 'USD', $options = array())
 {
     $customerId = $this->customerId();
     $paymentProfileId = $this->paymentProfileId($customerId);
     $options = array_merge($this->options, $options);
     // Common setup for API credentials
     $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
     $merchantAuthentication->setName($options['authname']);
     $merchantAuthentication->setTransactionKey($options['authkey']);
     $refId = 'ref' . time();
     $paymentProfile = new AnetAPI\PaymentProfileType();
     $paymentProfile->setPaymentProfileId($paymentProfileId);
     $profileToCharge = new AnetAPI\CustomerProfilePaymentType();
     $profileToCharge->setCustomerProfileId($customerId);
     $profileToCharge->setPaymentProfile($paymentProfile);
     $transactionRequestType = new AnetAPI\TransactionRequestType();
     $transactionRequestType->setTransactionType("authCaptureTransaction");
     $transactionRequestType->setAmount($amount);
     $transactionRequestType->setProfile($profileToCharge);
     $request = new AnetAPI\CreateTransactionRequest();
     $request->setMerchantAuthentication($merchantAuthentication);
     $request->setRefId($refId);
     $request->setTransactionRequest($transactionRequestType);
     $controller = new AnetController\CreateTransactionController($request);
     $response = $controller->executeWithApiResponse($options['server']);
     if (!isset($response)) {
         throw new Assets_Exception_InvalidResponse(array('response' => 'empty response'));
     }
     $tresponse = $response->getTransactionResponse();
     if (!isset($tresponse)) {
         throw new Assets_Exception_ChargeFailed();
     }
     switch ($tresponse->getResponseCode()) {
         case '1':
             return $customerId;
         case '3':
             throw new Assets_Exception_DuplicateTransaction();
         case '4':
             throw new Assets_Exception_HeldForReview();
         default:
             throw new Assets_Exception_ChargeFailed();
     }
 }
<?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");
// Common setup for API credentials
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName("5KP3u95bQpv");
$merchantAuthentication->setTransactionKey("4Ktq966gC55GAX7S");
$refId = 'ref' . time();
$profileToCharge = new AnetAPI\CustomerProfilePaymentType();
$profileToCharge->setCustomerProfileId("36731856");
$paymentProfile = new AnetAPI\PaymentProfileType();
$paymentProfile->setPaymentProfileId("33211899");
$profileToCharge->setPaymentProfile($paymentProfile);
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
$transactionRequestType->setAmount(100.5);
$transactionRequestType->setProfile($profileToCharge);
$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 ($response != null) {
    $tresponse = $response->getTransactionResponse();
    if ($tresponse != null && $tresponse->getResponseCode() == "1") {
        echo "Charge Customer Profile APPROVED  :" . "\n";
        echo " Charge Customer Profile AUTH CODE : " . $tresponse->getAuthCode() . "\n";