Пример #1
0
 /**
  * pay a charge
  *
  * @param CreditCard|Billet $paymentType
  * @return bool
  */
 public function payCharge($paymentType)
 {
     if (empty($this->customer)) {
         throw new BadMethodCallException("You need to set the customer.");
     }
     if (empty($this->chargeId)) {
         throw new BadMethodCallException("Ops. Did you charge? It is missing the charge id");
     }
     $params = ['id' => $this->chargeId];
     $body = ['payment' => []];
     $model = null;
     if ($paymentType instanceof Billet) {
         $model = new Billet($paymentType);
         $model->customer = $this->customer;
         if (!$model->validate()) {
             $this->errors = $model->errors;
             return false;
         }
         $body['payment'] = ['banking_billet' => $model->toArray()];
     } elseif ($paymentType instanceof CreditCard) {
         $model = new CreditCard($paymentType);
         $model->customer = $this->customer;
         if (!$model->validate()) {
             $this->errors = $model->errors;
             return false;
         }
         $data = $model->toArray();
         if (empty($data['discount'])) {
             unset($data['discount']);
         }
         $body['payment'] = ['credit_card' => $data];
     } else {
         throw new \InvalidArgumentException("The param should be an instance of Billet or CreditCard.");
     }
     $data = $this->getApi()->payCharge($params, $body);
     if (!empty($data['code']) && $data['code'] == 200) {
         $this->refresh();
     }
     return $data;
 }