Skip to content

Commit 987e2b7

Browse files
authored
Merge pull request #7 from hypergori/webGLsupport
Fix webGL support bug adding dll to link.xml
2 parents b33c9df + 0f8ab5c commit 987e2b7

8 files changed

+190
-68
lines changed

Assets/Dlls/BtcPayClient.dll

0 Bytes
Binary file not shown.

Assets/Dlls/link.xml

+2
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
</assembly>
77
-->
88
<assembly fullname="BtcPayClient" preserve="all"/>
9+
<assembly fullname="WebSocketUnity" preserve="all"/>
10+
911
</linker>
-512 Bytes
Binary file not shown.

Assets/Dlls/webGL/WebSocketUnity.dll

-512 Bytes
Binary file not shown.

Assets/Dlls/websocket-sharp.dll.meta

+80-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/BTCPayUnity.cs

-13
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,19 @@ public void createInvoice()
3838
invoice.BuyerEmail = email;
3939
invoice.FullNotifications = true;
4040
invoice.NotificationEmail = email;
41-
invoice.PosData = "TEST POS DATA";
4241
invoice.ItemDesc = product.text;//購入アイテムの名称
43-
invoice.Id ="123";
4442

45-
Debug.Log("createInvoice(): BEFORE NULLCHECK");
46-
47-
Debug.Log("createInvoice(): initial Invoice isNull?:" + (invoice==null));
48-
Debug.Log("createInvoice(): initial Invoice:" + invoice.ToString());
4943
string json = JsonConvert.SerializeObject(invoice);
50-
Debug.Log("createInvoice(): serialized json:" + json);
5144
JObject jo = JObject.Parse(json);
52-
Debug.Log("createInvoice(): price:" + jo["price"]);
53-
Debug.Log("createInvoice(): id:" + jo["id"]);
5445

5546
//2.Create Invoice with initial data and get the full invoice
5647
//2.BTCPayServerにインボイスデータをサブミットして、インボイスの詳細データを取得する。
57-
Debug.Log("createInvoice(): btcPayClient is null?:"+ (btcPayClient == null));
5848
StartCoroutine(btcPayClient.createInvoice(invoice ,handleInvoice));
5949

6050
}
6151

6252
private void handleInvoice(Invoice invoice)
6353
{
64-
Debug.Log("handleInvoice(): Invoice Created:" + invoice.Id);
65-
Debug.Log("handleInvoice(): Invoice Url:" + invoice.Url);
66-
6754
//3.Lightning BOLT invoice string
6855
//3.Lightning BOLT invoice データは以下のプロパティから取得する。
6956
List<InvoiceCryptoInfo> cryptoInfoList = invoice.CryptoInfo;

README.ja.md

+52-24
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,32 @@ Read this in other languages: [English](README.md), [日本語](README.ja.md)
55

66
## BTCpayサーバー Unity クライアントの使用方法
77

8-
このライブラリーは、Unity用のBtcPay クライアントです。
8+
このライブラリーは、Unity用のBtcPay Server クライアントです。
9+
このライブラリーを使うことで、Unityで作るゲームやアプリに、BTCPAY SERVER経由での支払い機能を追加することができます。
910
メインの2つのクラスがあり、1つがBTCPayClientクラスで、もう一つが、Invoiceクラスです。
1011

1112
最初に、 BTCPayClientクラスを、コンストラクターでインスタンス化します。引数には、ペアリングコード(サーバー主導ペアリング)とBTCPay Serverのホスト名を渡します。
1213

1314
次に、Invoiceオブジェクトをインスタンス化し、必須の価格と通貨を引数に渡します。そのたのオプションの商品情報やお客さんのメールアドレスもセット可能です。
1415
そして、クライアントを使用して、BTCPayサーバーへ登録すると、リスポンスとして 支払先情報をセットされたInvoiceオブジェクトがリスポンスとして返ってきます。
1516

16-
必要であれば、その特定のInvoiceの状態の変化にサブスクライブし、コールバック関数を登録できます。
17+
必要であれば、支払先アドレス(BTCアドレスやBOLT11インボイス)のQRコードを表示し、その特定のInvoiceの状態の変化にサブスクライブした、コールバック関数を登録できます。
1718

1819
## 依存ライブラリー
1920
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
2133

22-
* BitCoinSharp
23-
* BouncyCastle.Crypto
24-
* Newtonsoft.Json
25-
* zxing.unity
2634

2735
## .net のバージョン
2836
Unityの設定で、donetのバージョンを4に変更する必要があります。
@@ -69,8 +77,8 @@ using System.Collections.Generic;
6977
using UnityEngine;
7078
using BTCPayAPI;
7179
using UnityEngine.UI;
72-
using System.Threading.Tasks;
73-
80+
using Newtonsoft.Json.Linq;
81+
using Newtonsoft.Json;
7482
public class BTCPayUnity : MonoBehaviour {
7583

7684
public string pairCode;//set pairing code from inspector
@@ -84,17 +92,18 @@ public class BTCPayUnity : MonoBehaviour {
8492

8593
private BTCPayClient btcPayClient = null;
8694

87-
void Start()
95+
public void Start()
8896
{
8997
//Instantiate the BTCPayClient Object with server-initiated pairing code and hostname of BTCpay server
9098
//Once instantiated, it will generate a new private key if not there, and SIN ,which is derived from public key.
9199
//then registered on BTCPay server
92100
//BTCpayCleintをインスタンス化する。BTCPayServerで取得したペアリングコードをとホスト名をセット
93101
//秘密鍵ファイルがワーキングディレクトリに作成され、公開鍵から作られたBitcoinアドレスのようなSINがBTCPayServerに登録される。
94-
btcPayClient = new BTCPayClient(pairCode, btcpayServerHost);
102+
btcPayClient = new BTCPayClient(this,pairCode, btcpayServerHost);
103+
StartCoroutine(btcPayClient.initialize());
95104
}
96105

97-
public async void createInvoice()
106+
public void createInvoice()
98107
{
99108

100109
//1.New Invoice Preparation
@@ -104,34 +113,54 @@ public class BTCPayUnity : MonoBehaviour {
104113
invoice.BuyerEmail = email;
105114
invoice.FullNotifications = true;
106115
invoice.NotificationEmail = email;
107-
invoice.PosData = "TEST POS DATA";
108116
invoice.ItemDesc = product.text;//購入アイテムの名称
109117
118+
string json = JsonConvert.SerializeObject(invoice);
119+
JObject jo = JObject.Parse(json);
120+
110121
//2.Create Invoice with initial data and get the full invoice
111122
//2.BTCPayServerにインボイスデータをサブミットして、インボイスの詳細データを取得する。
112-
invoice = btcPayClient.createInvoice(invoice);
123+
StartCoroutine(btcPayClient.createInvoice(invoice ,handleInvoice));
113124

114-
Debug.Log("Invoice Created:" + invoice.Id);
115-
Debug.Log("Invoice Url:" + invoice.Url);
125+
}
116126

127+
private void handleInvoice(Invoice invoice)
128+
{
117129
//3.Lightning BOLT invoice string
118130
//3.Lightning BOLT invoice データは以下のプロパティから取得する。
119131
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
121147
122148
//4.Set the QR code iamge to image Gameobject
123149
//4.取得したBOLTからQRコードを作成し、ウオレットでスキャンするために表示する。
124150
QRcode.GetComponent<Image>().sprite = Sprite.Create(texs, new Rect(0.0f, 0.0f, texs.width, texs.height), new Vector2(0.5f, 0.5f), 100.0f);
125151

126152
//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+
129157

130158
}
131159

132-
//Callback method when payment is executed.
160+
161+
//Callback method when payment is executed.
133162
//支払実行時に、呼び出されるコールバック 関数(最新のインボイスオブジェクトが渡される)
134-
public async Task printInvoice(Invoice invoice)
163+
public void printInvoice(Invoice invoice)
135164
{
136165
//Hide QR code image to Paied Image file
137166
//ステータス 一覧はこちら。 https://bitpay.com/docs/invoice-states
@@ -140,16 +169,15 @@ public class BTCPayUnity : MonoBehaviour {
140169
//インボイスのステータスがcompleteであれば、全額が支払われた状態なので、支払完了のイメージに変更する
141170
//Change the image from QR to Paid
142171
QRcode.GetComponent<Image>().sprite = Resources.Load<Sprite>("image/paid");
143-
//1 sec Delay to keep paid image/支払済みイメージを1秒間表示
144-
await Task.Delay(1000);
145172
Debug.Log("payment is complete");
146173
}else
147174
{
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.
149176
//全額支払いでない場合には、なにか処理をおこなう。以下は、ただ ステータスを表示して終了。
150177
Debug.Log("payment is not completed:" + invoice.Status);
151178
}
152179

153180
}
154181
}
182+
155183
```

0 commit comments

Comments
 (0)