@@ -5,24 +5,32 @@ Read this in other languages: [English](README.md), [日本語](README.ja.md)
5
5
6
6
## BTCpayサーバー Unity クライアントの使用方法
7
7
8
- このライブラリーは、Unity用のBtcPay クライアントです。
8
+ このライブラリーは、Unity用のBtcPay Server クライアントです。
9
+ このライブラリーを使うことで、Unityで作るゲームやアプリに、BTCPAY SERVER経由での支払い機能を追加することができます。
9
10
メインの2つのクラスがあり、1つがBTCPayClientクラスで、もう一つが、Invoiceクラスです。
10
11
11
12
最初に、 BTCPayClientクラスを、コンストラクターでインスタンス化します。引数には、ペアリングコード(サーバー主導ペアリング)とBTCPay Serverのホスト名を渡します。
12
13
13
14
次に、Invoiceオブジェクトをインスタンス化し、必須の価格と通貨を引数に渡します。そのたのオプションの商品情報やお客さんのメールアドレスもセット可能です。
14
15
そして、クライアントを使用して、BTCPayサーバーへ登録すると、リスポンスとして 支払先情報をセットされたInvoiceオブジェクトがリスポンスとして返ってきます。
15
16
16
- 必要であれば、その特定のInvoiceの状態の変化にサブスクライブし 、コールバック関数を登録できます。
17
+ 必要であれば、支払先アドレス(BTCアドレスやBOLT11インボイス)のQRコードを表示し、その特定のInvoiceの状態の変化にサブスクライブした 、コールバック関数を登録できます。
17
18
18
19
## 依存ライブラリー
19
20
BTCpayクライアントは、以下ライブラリーに依存していますので、dllが必要です。
20
- もしくは、BTCpay クライアントの unityパッケージをインポートすることも可能です。
21
+ もしくは、BTCpay クライアントの unityパッケージをインポートすることでバージョンの心配が不要です。
22
+
23
+ * Newtonsoft.Json
24
+ * NBitcoin エリプティックカーブを使った鍵の生成や署名
25
+ * BouncyCastle.Crypto NBitcoinの依存先
26
+ * websocket-sharp WEBGL以外用のWEBSOCKETライブラリー
27
+ * log4net
28
+ * zxing.unity QR コード生成
29
+
30
+ ## 依存アセット
31
+ Json for Unity が必要です。
32
+ https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347
21
33
22
- * BitCoinSharp
23
- * BouncyCastle.Crypto
24
- * Newtonsoft.Json
25
- * zxing.unity
26
34
27
35
## .net のバージョン
28
36
Unityの設定で、donetのバージョンを4に変更する必要があります。
@@ -69,8 +77,8 @@ using System.Collections.Generic;
69
77
using UnityEngine ;
70
78
using BTCPayAPI ;
71
79
using UnityEngine .UI ;
72
- using System . Threading . Tasks ;
73
-
80
+ using Newtonsoft . Json . Linq ;
81
+ using Newtonsoft . Json ;
74
82
public class BTCPayUnity : MonoBehaviour {
75
83
76
84
public string pairCode ;// set pairing code from inspector
@@ -84,17 +92,18 @@ public class BTCPayUnity : MonoBehaviour {
84
92
85
93
private BTCPayClient btcPayClient = null ;
86
94
87
- void Start ()
95
+ public void Start ()
88
96
{
89
97
// Instantiate the BTCPayClient Object with server-initiated pairing code and hostname of BTCpay server
90
98
// Once instantiated, it will generate a new private key if not there, and SIN ,which is derived from public key.
91
99
// then registered on BTCPay server
92
100
// BTCpayCleintをインスタンス化する。BTCPayServerで取得したペアリングコードをとホスト名をセット
93
101
// 秘密鍵ファイルがワーキングディレクトリに作成され、公開鍵から作られたBitcoinアドレスのようなSINがBTCPayServerに登録される。
94
- btcPayClient = new BTCPayClient (pairCode , btcpayServerHost );
102
+ btcPayClient = new BTCPayClient (this ,pairCode , btcpayServerHost );
103
+ StartCoroutine (btcPayClient .initialize ());
95
104
}
96
105
97
- public async void createInvoice ()
106
+ public void createInvoice ()
98
107
{
99
108
100
109
// 1.New Invoice Preparation
@@ -104,34 +113,54 @@ public class BTCPayUnity : MonoBehaviour {
104
113
invoice .BuyerEmail = email ;
105
114
invoice .FullNotifications = true ;
106
115
invoice .NotificationEmail = email ;
107
- invoice .PosData = " TEST POS DATA" ;
108
116
invoice .ItemDesc = product .text ;// 購入アイテムの名称
109
117
118
+ string json = JsonConvert .SerializeObject (invoice );
119
+ JObject jo = JObject .Parse (json );
120
+
110
121
// 2.Create Invoice with initial data and get the full invoice
111
122
// 2.BTCPayServerにインボイスデータをサブミットして、インボイスの詳細データを取得する。
112
- invoice = btcPayClient .createInvoice (invoice );
123
+ StartCoroutine ( btcPayClient .createInvoice (invoice , handleInvoice ) );
113
124
114
- Debug .Log (" Invoice Created:" + invoice .Id );
115
- Debug .Log (" Invoice Url:" + invoice .Url );
125
+ }
116
126
127
+ private void handleInvoice (Invoice invoice )
128
+ {
117
129
// 3.Lightning BOLT invoice string
118
130
// 3.Lightning BOLT invoice データは以下のプロパティから取得する。
119
131
List < InvoiceCryptoInfo > cryptoInfoList = invoice .CryptoInfo ;
120
- Texture2D texs = btcPayClient .generateQR (cryptoInfoList [0 ].paymentUrls .BOLT11 );// Generate QR code image
132
+ string boltInvoice = null ;
133
+ foreach (InvoiceCryptoInfo info in cryptoInfoList ){
134
+ if (info .paymentType == " LightningLike" )
135
+ {
136
+ Debug .Log (" bolt :" + info .paymentUrls .BOLT11 );
137
+ boltInvoice = info .paymentUrls .BOLT11 ;
138
+ }
139
+ }
140
+ if (string .IsNullOrEmpty (boltInvoice ))
141
+ {
142
+ Debug .Log (" bolt Invoice is not set in Invoice in reponse.Check the BTCpay server's lightning setup" );
143
+ return ;
144
+ }
145
+
146
+ Texture2D texs = btcPayClient .generateQR (boltInvoice );// Generate QR code image
121
147
122
148
// 4.Set the QR code iamge to image Gameobject
123
149
// 4.取得したBOLTからQRコードを作成し、ウオレットでスキャンするために表示する。
124
150
QRcode .GetComponent <Image >().sprite = Sprite .Create (texs , new Rect (0 . 0 f , 0 . 0 f , texs .width , texs .height ), new Vector2 (0 . 5 f , 0 . 5 f ), 100 . 0 f );
125
151
126
152
// 5.Subscribe the an callback method with invoice ID to be monitored
127
- // 5.支払がされたら実行されるasync コールバックを引き渡して、await で実行する
128
- await btcPayClient .subscribeInvoiceAsync (invoice .Id , printInvoice );
153
+ // 5.支払がされたら実行されるコールバックを引き渡して、コールーチンで実行する
154
+ StartCoroutine (btcPayClient .SubscribeInvoiceCoroutine (invoice .Id , printInvoice ));
155
+ // StartCoroutine(btcPayClient.listenInvoice(invoice.Id, printInvoice));
156
+
129
157
130
158
}
131
159
132
- // Callback method when payment is executed.
160
+
161
+ // Callback method when payment is executed.
133
162
// 支払実行時に、呼び出されるコールバック 関数(最新のインボイスオブジェクトが渡される)
134
- public async Task printInvoice (Invoice invoice )
163
+ public void printInvoice (Invoice invoice )
135
164
{
136
165
// Hide QR code image to Paied Image file
137
166
// ステータス 一覧はこちら。 https://bitpay.com/docs/invoice-states
@@ -140,16 +169,15 @@ public class BTCPayUnity : MonoBehaviour {
140
169
// インボイスのステータスがcompleteであれば、全額が支払われた状態なので、支払完了のイメージに変更する
141
170
// Change the image from QR to Paid
142
171
QRcode .GetComponent <Image >().sprite = Resources .Load <Sprite >(" image/paid" );
143
- // 1 sec Delay to keep paid image/支払済みイメージを1秒間表示
144
- await Task .Delay (1000 );
145
172
Debug .Log (" payment is complete" );
146
173
}else
147
174
{
148
- // StartCoroutine(btcPayClient.subscribeInvoice(invoice.Id, printInvoice, this));
175
+ // for example, if the amount paid is not full, do something.the line below just print the status.
149
176
// 全額支払いでない場合には、なにか処理をおこなう。以下は、ただ ステータスを表示して終了。
150
177
Debug .Log (" payment is not completed:" + invoice .Status );
151
178
}
152
179
153
180
}
154
181
}
182
+
155
183
```
0 commit comments