/**
     * Creates a payment.
     *
     * @param  Payment $payment Payment to be created
     *
     * @return Payment          The created payment, reloaded if the transaction
     *                          was not overridden
     */
    public function create(Payment $payment)
    {
        // Set create authorship data if not already set
        if (!$payment->authorship->createdAt()) {
            $payment->authorship->create(new DateTimeImmutable(), $this->_currentUser->id);
        }
        $this->_validate($payment);
        $event = new Event\TransactionalEvent($payment);
        $event->setTransaction($this->_trans);
        $payment = $this->_eventDispatcher->dispatch(Events::CREATE_START, $event)->getPayment();
        $result = $this->_trans->run('
			INSERT INTO
				payment
			SET
				created_at  = :createdAt?d,
				created_by  = :createdBy?in,
				method      = :method?sn,
				currency_id = :currencyID?s,
				amount      = :amount?f,
				`change`    = :change?fn,
				reference   = :reference?sn
		', array('createdAt' => $payment->authorship->createdAt(), 'createdBy' => $payment->authorship->createdBy(), 'currencyID' => $payment->currencyID, 'method' => $payment->method->getName(), 'amount' => $payment->amount, 'change' => $payment->change, 'reference' => $payment->reference));
        $sqlVariable = 'PAYMENT_ID_' . uniqid();
        $this->_trans->setIDVariable($sqlVariable);
        $payment->id = '@' . $sqlVariable;
        if (!$this->_transOverridden) {
            $this->_trans->commit();
            return $this->_loader->getByID($this->_trans->getIDVariable($sqlVariable));
        }
        return $payment;
    }
 /**
  * Sets the "used" timestamp on vouchers that have been used entirely for
  * a payment.
  *
  * The timestamp is set to the "created at" timestamp for the payment.
  *
  * @param Payment\Event\TransactionalEvent $event
  */
 public function setUsedTimestamp(Payment\Event\TransactionalEvent $event)
 {
     $payment = $event->getPayment();
     $voucherLoader = $this->get('voucher.loader');
     $voucherEdit = $this->get('voucher.edit');
     // Set voucher edit decorator to use the transaction from the event
     $voucherEdit->setTransaction($event->getTransaction());
     // Skip if the payment isn't a voucher payment
     if ('voucher' !== $payment->method->getName()) {
         return false;
     }
     $voucher = $voucherLoader->getByID($payment->reference);
     // Skip if the voucher could not be found
     if (!$voucher instanceof Voucher) {
         return false;
     }
     // Skip if the voucher wasn't fully used
     if ($payment->amount != $voucher->getBalance()) {
         return false;
     }
     $voucherEdit->setUsed($voucher, $payment->authorship->createdAt());
 }