Пример #1
0
 /**
  * Creates a new customer transaction.
  *
  * @param Model_Customer_Paymentmethod $payment_method The payment method.
  * @param string                       $amount         The amount to transact.
  * @param array                        $data           Optional data.
  *
  * @return Model_Customer_Transaction
  */
 public static function create(Model_Customer_Paymentmethod $payment_method, $amount, array $data = array())
 {
     $gateway_instance = Gateway::instance($payment_method->gateway, $payment_method->customer);
     if ($gateway_instance) {
         $external_id = $gateway_instance->transaction()->create(array('payment_method' => $payment_method, 'amount' => $amount));
         if (!$external_id) {
             return false;
         }
     } else {
         $external_id = null;
     }
     $transaction = Model_Customer_Transaction::forge();
     $transaction->customer = $payment_method->customer;
     $transaction->gateway = $payment_method->gateway;
     $transaction->external_id = $external_id;
     $transaction->type = $payment_method->gateway->type;
     $transaction->provider = $payment_method->provider;
     $transaction->account = $payment_method->account;
     $transaction->amount = $amount;
     try {
         $transaction->save();
     } catch (FuelException $e) {
         Log::error($e);
         return false;
     }
     Service_Event::trigger('customer.transaction.create', $transaction->customer->seller, $transaction->to_array());
     return $transaction;
 }
Пример #2
0
 /**
  * Gets a new instance of gateway $class_name.
  *
  * @param Model_Gateway  $gateway    The gateway model to use for the driver.
  * @param Model_Customer $customer   The customer model to use for the driver.
  * @param string         $class_name The class name to call on the driver.
  *
  * @return Gateway_Model
  */
 public static function forge(Model_Gateway $gateway, Model_Customer $customer = null, $class_name)
 {
     $driver_name = str_replace('Gateway_', '', get_called_class());
     $driver_name = str_replace('_Driver', '', $driver_name);
     $class = 'Gateway_' . Str::ucwords(Inflector::denamespace($driver_name)) . '_' . Str::ucwords(Inflector::denamespace($class_name));
     if (!class_exists($class)) {
         throw new GatewayException('Call to undefined class ' . $class);
     }
     $driver = Gateway::instance($gateway, $customer);
     return new $class($driver);
 }
Пример #3
0
 /**
  * Creates a new customer gateway relation.
  *
  * @param Model_Customer $customer The customer.
  * @param Model_Gateway  $gateway  The gateway.
  * @param array          $data     Optional data.
  *
  * @return Model_Customer
  */
 public static function create(Model_Customer $customer, Model_Gateway $gateway, array $data = array())
 {
     if (!($contact = Arr::get($data, 'contact'))) {
         $contact = current($customer->contacts);
     }
     $external_id = Gateway::instance($gateway)->customer()->create(array('customer' => $customer, 'contact' => $contact));
     if (!$external_id) {
         return false;
     }
     $customer_gateway = Model_Customer_Gateway::forge();
     $customer_gateway->customer_id = $customer->id;
     $customer_gateway->gateway_id = $gateway->id;
     $customer_gateway->external_id = $external_id;
     try {
         $customer_gateway->save();
     } catch (FuelException $e) {
         Log::error($e);
         return false;
     }
     return $customer_gateway;
 }
Пример #4
0
 /**
  * Deletes a payment method.
  *
  * @param Model_Customer_Paymentmethod $payment_method The payment method to delete.
  *
  * @return bool
  */
 public static function delete(Model_Customer_Paymentmethod $payment_method)
 {
     // A primary payment method cannot be deleted.
     if ($payment_method->primary()) {
         return false;
     }
     $gateway = $payment_method->gateway;
     $customer = $payment_method->customer;
     $gateway_instance = Gateway::instance($gateway, $customer);
     if ($gateway_instance) {
         $gateway_payment_method = $gateway_instance->paymentmethod($payment_method->external_id);
         if (!$gateway_payment_method) {
             return false;
         }
         if (!$gateway_payment_method->delete()) {
             return false;
         }
     }
     $payment_method->status = 'deleted';
     try {
         $payment_method->save();
     } catch (FuelException $e) {
         Log::error($e);
         return false;
     }
     Service_Event::trigger('customer.paymentmethod.delete', $payment_method->customer->seller, $payment_method->to_array());
     return true;
 }