Пример #1
0
 /**
  * Executes some API calls and obtains a customer id
  * @method customerId
  * @return {string} The customer id
  */
 function customerId()
 {
     $options = $this->options;
     // Common Set Up for API Credentials
     $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
     $merchantAuthentication->setName($options['authname']);
     $merchantAuthentication->setTransactionKey($options['authkey']);
     $refId = 'ref' . time();
     $user = $options['user'];
     $merchantCustomerId = $user->id;
     $customer = new Assets_Customer();
     $customer->userId = $user->id;
     $customer->payments = 'authnet';
     if ($customer->retrieve()) {
         return $customer->customerId;
     }
     $customerprofile = new AnetAPI\CustomerProfileType();
     $customerprofile->setMerchantCustomerId($merchantCustomerId);
     $customerprofile->setDescription($user->displayName());
     $customerprofile->setEmail($user->emailAddress);
     $request = new AnetAPI\CreateCustomerProfileRequest();
     $request->setMerchantAuthentication($merchantAuthentication);
     $request->setRefId($refId);
     $request->setProfile($customerprofile);
     $controller = new AnetController\CreateCustomerProfileController($request);
     $response = $controller->executeWithApiResponse($options['server']);
     if ($response != null && $response->getMessages()->getResultCode() == "Ok") {
         return $response->getCustomerProfileId();
     }
     if ($response != null && $response->getMessages()->getResultCode() == "Ok") {
         $customerId = $response->getCustomerProfileId();
     } else {
         $messages = $response->getMessages()->getMessage();
         $message = reset($messages);
         // workaround to get customerProfileId
         // https://community.developer.authorize.net/t5/Integration-and-Testing/How-to-lookup-customerProfileId-and-paymentProfileId-by/td-p/52501
         if (isset($response) and $message->getCode() != "E00039") {
             throw new Assets_Exception_InvalidResponse(array('response' => $message->getCode() . ' ' . $message->getText()));
         }
         $parts = explode(' ', $message->getText());
         $customerId = $parts[5];
     }
     $customer->customerId = $customerId;
     $customer->save();
     return $customerId;
 }
Пример #2
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.token=null] required unless the user is an existing customer
  * @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 \Stripe\Error\Card
  * @return {string} The customerId of the Assets_Customer that was successfully charged
  */
 function charge($amount, $currency = 'USD', $options = array())
 {
     $options = array_merge($this->options, $options);
     Q_Valid::requireFields(array('secret', 'user'), $options, true);
     \Stripe\Stripe::setApiKey($options['secret']);
     $user = $options['user'];
     $customer = new Assets_Customer();
     $customer->userId = $user->id;
     $customer->payments = 'stripe';
     if (!$customer->retrieve()) {
         Q_Valid::requireFields(array('token'), $options, true);
         $sc = \Stripe\Customer::create(array("source" => $options['token']["id"], "description" => $options['user']->displayName()));
         $customer->customerId = $sc->id;
         $customer->save();
     }
     $params = array("amount" => $amount * 100, "currency" => $currency, "customer" => $customer->customerId);
     Q::take($options, array('description', 'metadata'), $params);
     \Stripe\Charge::create($params);
     // can throw some exception
     return $customer->customerId;
 }