Exemplo n.º 1
0
})->bind('info');
$app->match('/new', function (\Symfony\Component\HttpFoundation\Request $request) use($app) {
    // some default data for when the form is displayed the first time
    /** @var \Symfony\Component\Form\FormFactory $factory */
    $factory = $app['form.factory'];
    $form = $factory->createBuilder()->add('address', 'text', array('attr' => array('class' => 'form-control', 'placeholder' => 'Bitcoin address'), 'constraints' => [new \Symfony\Component\Validator\Constraints\NotBlank(), new \BitWasp\Payments\Application\Validator\BitcoinAddress()]))->add('value', 'text', array('attr' => array('class' => 'form-control', 'placeholder' => 'Amount in satoshis'), 'constraints' => new \Symfony\Component\Validator\Constraints\NotBlank()))->add('send', 'submit', array('attr' => array('class' => 'btn btn-lg btn-primary btn-block')))->getForm();
    if ('POST' == $request->getMethod()) {
        $form->bind($request);
        if ($form->isValid()) {
            $data = $form->getData();
            $address = \BitWasp\Bitcoin\Address\AddressFactory::fromString($data['address']);
            $script = \BitWasp\Bitcoin\Script\ScriptFactory::scriptPubKey()->payToAddress($address);
            $txOut = new \BitWasp\Bitcoin\Transaction\TransactionOutput($data['value'], $script);
            $slug = bin2hex(openssl_random_pseudo_bytes(32));
            $signer = new \BitWasp\Bitcoin\PaymentProtocol\PaymentRequestSigner('none');
            $builder = new \BitWasp\Bitcoin\PaymentProtocol\PaymentRequestBuilder($signer, 'main', time());
            $builder->addOutput($txOut);
            $details = $builder->getPaymentDetails();
            $details->setPaymentUrl($app->url('request.payment', ['slug' => $slug]));
            $request = $builder->getPaymentRequest();
            $totalValue = $data['value'];
            $amount = new \BitWasp\Bitcoin\Amount();
            $dbRequest = \BitWasp\Payments\Db\Request::create(['slug' => $slug, 'value' => $totalValue, 'valueBtc' => $amount->toBtc($data['value']), 'payment_request' => $request->serialize()]);
            $output = \BitWasp\Payments\Db\OutputRequirement::create(['request_id' => $dbRequest->id, 'value' => $data['value'], 'valueBtc' => $amount->toBtc($data['value']), 'address' => $data['address'], 'script' => $txOut->getScript()->getBinary()]);
            $app['request_api']->pushRequest($slug, [$output]);
            return $app->redirect($app['url_generator']->generate('info', array('slug' => $dbRequest->slug)));
        }
    }
    return $app['twig']->render('new.twig', ['form' => $form->createView(), 'errors' => $form->getErrors()]);
})->bind('new');
$app->run();
<?php

require "../vendor/autoload.php";
require "../db/bootstrap.php";
$address = \BitWasp\Bitcoin\Address\AddressFactory::fromString('1J7jgWATD4Vfe9eUs7EL4YdadbPaM7cGgj');
$script = \BitWasp\Bitcoin\Script\ScriptFactory::scriptPubKey()->payToAddress($address);
/** @var \BitWasp\Bitcoin\Transaction\TransactionOutputInterface[] $outputs */
$outputs = [new \BitWasp\Bitcoin\Transaction\TransactionOutput(1000000, $script)];
$signer = new \BitWasp\Bitcoin\PaymentProtocol\PaymentRequestSigner('none');
$builder = new \BitWasp\Bitcoin\PaymentProtocol\PaymentRequestBuilder($signer, 'main', time());
foreach ($outputs as $output) {
    $builder->addOutput($output);
}
$request = $builder->getPaymentRequest();
$value = 1000000;
$amount = new \BitWasp\Bitcoin\Amount();
$list = \BitWasp\Payments\Db\Request::create(['slug' => bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)), 'value' => $value, 'valueBtc' => $amount->toBtc($value), 'payment_request' => $request->serialize()]);
$req = [];
foreach ($outputs as $output) {
    $req[] = \BitWasp\Payments\Db\OutputRequirement::create(['request_id' => $list->id, 'value' => $output->getValue(), 'valueBtc' => $amount->toBtc($value), 'script' => $output->getScript()->getBinary(), 'address' => $address->getAddress()]);
}