Skip to content

Commit 321120f

Browse files
author
Corey McCormick
committed
Add BitCoin helper - #50
1 parent 023242c commit 321120f

3 files changed

Lines changed: 196 additions & 0 deletions

File tree

docs/en/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ You can display a PNG image without saving the file by providing a raw string an
247247

248248
Helpers are an easy way to create QrCodes that cause a reader to perform a certain action when scanned.
249249

250+
#### BitCoin
251+
252+
This helpers generates a scannable bitcoin to send payments. [More information](https://bitco.in/en/developer-guide#plain-text)
253+
254+
QrCode::BTC($address, $amount);
255+
256+
//Sends a 0.334BTC payment to the address
257+
QrCode::BTC('bitcoin address', 0.334);
258+
259+
//Sends a 0.334BTC payment to the address with some optional arguments
260+
QrCode::size(500)->BTC('address', 0.0034, [
261+
'label' => 'my label',
262+
'message' => 'my message',
263+
'returnAddress' => 'https://www.returnaddress.com'
264+
]);
265+
250266
#### E-Mail
251267

252268
This helper generates an e-mail qrcode that is able to fill in the e-mail address, subject, and body.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace SimpleSoftwareIO\QrCode\DataTypes;
4+
5+
class BTC implements DataTypeInterface
6+
{
7+
/**
8+
* The prefix of the QrCode.
9+
*
10+
* @var string
11+
*/
12+
protected $prefix = 'bitcoin:';
13+
14+
/**
15+
* The separator between the variables.
16+
*
17+
* @var string
18+
*/
19+
protected $separator = '&';
20+
21+
/**
22+
* The BitCoin address.
23+
*
24+
* @var string
25+
*/
26+
protected $address;
27+
28+
/**
29+
* The amount to send.
30+
*
31+
* @var integer
32+
*/
33+
protected $amount;
34+
35+
/**
36+
* The BitCoin transaction label.
37+
*
38+
* @var string
39+
*/
40+
protected $label;
41+
42+
/**
43+
* The BitCoin message to send.
44+
*
45+
* @var string
46+
*/
47+
protected $message;
48+
49+
/**
50+
* The BitCoin return URL.
51+
*
52+
* @var string
53+
*/
54+
protected $returnAddress;
55+
56+
/**
57+
* Generates the DataType Object and sets all of its properties.
58+
*
59+
* @param $arguments
60+
*/
61+
public function create(array $arguments)
62+
{
63+
$this->setProperties($arguments);
64+
}
65+
66+
/**
67+
* Returns the correct QrCode format.
68+
*
69+
* @return string
70+
*/
71+
public function __toString()
72+
{
73+
return $this->buildBitCoinString();
74+
}
75+
76+
/**
77+
* Sets the BitCoin arguments.
78+
*
79+
* @param array $arguments
80+
*/
81+
protected function setProperties(array $arguments)
82+
{
83+
if (isset($arguments[0])) {
84+
$this->address = $arguments[0];
85+
}
86+
87+
if (isset($arguments[1])) {
88+
$this->amount = $arguments[1];
89+
}
90+
91+
if (isset($arguments[2])) {
92+
$this->setOptions($arguments[2]);
93+
}
94+
}
95+
96+
/**
97+
* Sets the optional BitCoin options
98+
*
99+
* @param array $options
100+
*/
101+
protected function setOptions(array $options)
102+
{
103+
if (isset($options['label'])) {
104+
$this->label = $options['label'];
105+
}
106+
107+
if (isset($options['message'])) {
108+
$this->message = $options['message'];
109+
}
110+
111+
if (isset($options['returnAddress'])) {
112+
$this->returnAddress = $options['returnAddress'];
113+
}
114+
}
115+
116+
/**
117+
* Builds a BitCoin string.
118+
*
119+
* @return string
120+
*/
121+
protected function buildBitCoinString()
122+
{
123+
$query = http_build_query([
124+
'amount' => $this->amount,
125+
'label' => $this->label,
126+
'$message' => $this->message,
127+
'r' => $this->returnAddress
128+
]);
129+
130+
$btc = $this->prefix.$this->address.'?'.$query;
131+
132+
return $btc;
133+
}
134+
}

tests/DataTypes/BTC.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use SimpleSoftwareIO\QrCode\DataTypes\BTC;
4+
5+
class BTCTest extends \PHPUnit_Framework_TestCase
6+
{
7+
public function setUp()
8+
{
9+
$this->btc = new BTC();
10+
}
11+
12+
public function test_it_generates_a_valid_btc_qrcode_with_an_address_and_amount()
13+
{
14+
$this->btc->create(['btcaddress', 0.0034]);
15+
16+
$properFormat = 'bitcoin:btcaddress?amount=0.0034';
17+
18+
$this->assertEquals($properFormat, strval($this->btc));
19+
}
20+
21+
public function test_it_generates_a_valid_btc_qrcode_with_an_address_amount_and_label()
22+
{
23+
$this->btc->create(['btcaddress', 0.0034, ['label' => 'label']]);
24+
25+
$properFormat = 'bitcoin:btcaddress?amount=0.0034&label=label';
26+
27+
$this->assertEquals($properFormat, strval($this->btc));
28+
}
29+
30+
public function test_it_generates_a_valid_btc_qrcode_with_an_address_amount_label_message_and_return_address()
31+
{
32+
$this->btc->create([
33+
'btcaddress',
34+
0.0034,
35+
[
36+
'label' => 'label',
37+
'message' => 'message',
38+
'returnAddress' => 'https://www.returnaddress.com'
39+
]
40+
]);
41+
42+
$properFormat = 'bitcoin:btcaddress?amount=0.0034&label=label&%24message=message&r=' . urlencode('https://www.returnaddress.com');
43+
44+
$this->assertEquals($properFormat, strval($this->btc));
45+
}
46+
}

0 commit comments

Comments
 (0)