g33kme/adapay

使用 ADAPay,您可以在您的网店中轻松创建卡多支付发票,并验证收到的付款

v1.0.0 2021-11-09 11:28 UTC

This package is auto-updated.

Last update: 2024-09-09 18:03:54 UTC


README

ADAPay 是一个免费的开源卡多 (ADA) 支付处理器脚本,用 PHP 编写。您需要运行一个 cardano-wallet 来与 ADAPay 一起工作。

使用 ADAPay,您可以在您的网店中轻松创建卡多支付发票,并验证收到的付款。

🙏 通过将您的 ADA 投注到以下标记为 GEEK 的池中以帮助我们改进此项目
GeekMe 投注池

1. 安装:cardano-wallet

非常重要!

在您的服务器上运行 cardano-wallet 非常简单,但您需要注意,您的服务器需要有足够的硬件要求来运行您的 cardano-node。

  • 至少 2 个 vCPU
  • 至少 8GB 的 RAM(推荐 12GB)
  • 50 GB 的磁盘空间(理想情况下为 SSD)
  • 良好的互联网连接(至少 10Mbps)

如果您的硬件没有达到最低要求,您的 cardano-node 可能无法启动/同步,您将无法使用 cardano-wallet 提供的 API,也无法使用 ADAPay。

通过 docker-compose 安装 cardano-wallet

因此,您的服务器已启动并运行,您可以简单地下载最新的 docker-compose.yml 并将其保存到您的服务器。使用 docker-compose 快速启动 cardano-wallet 和支持的块生产者。以下是一个示例 docker-compose.yaml 文件,它结合了最新版本的 cardano-walletcardano-node

我们运行在 Ubuntu 20.04 LTS 上

# Update operation system
apt-get update 
apt-get upgrade

# Install docker and docker-compose
apt-get install docker
apt-get install docker-compose

# Download latest docker-compose.yml 
wget https://raw.githubusercontent.com/input-output-hk/cardano-wallet/master/docker-compose.yml

# Start cardano-wallet in Background
NETWORK=mainnet docker-compose up -d

# Or if your prefer to run your node on testnet you can do:
# NETWORK=testnet docker-compose up -d

# check docker status
docker ps

当您的 cardano-wallet 启动并运行时,您可以使用以下 URL 检查 Cardano 区块链的高度:NETWORK=mainnet docker-compose up -d

http://<IP4-SERVER-ADDRESS>:8090/v2/network/information

可能需要一些时间,可能是 1-2 天才能完全同步我们的 cardano-wallet。完成时,您在浏览上述 URL 时会收到一些“ready”状态。

{"network_tip":{"time":"2021-11-02T17:01:09Z","epoch_number":300,"absolute_slot_number":44306178,"slot_number":69378},"node_era":"alonzo","node_tip":{"height":{"quantity":6450485,"unit":"block"},"time":"2021-11-02T17:00:44Z","epoch_number":300,"absolute_slot_number":44306153,"slot_number":69353},"sync_progress":{"status":"ready"},"next_epoch":{"epoch_start_time":"2021-11-06T21:44:51Z","epoch_number":301}}

重要信息

如果您使用 NETWORK=mainnet docker-compose down 关闭 composer,然后使用 NETWORK=mainnet docker-compose up 启动,卡多节点可能会重建其账本数据库,这可能需要很长时间。Cardano-Node 仅在此重建完成后创建套接字文件。您只能在此套接字文件创建后连接到 API。

或者,您可以通过以下方式对您的 docker 进行清理:

NETWORK=mainnet docker-compose down
docker rm -f $(docker ps -a -q)
docker volume rm -f $(docker volume ls -q)
docker image rm -f $(docker image ls -q)
NETWORK=mainnet docker-compose up

然后,您的 API 将立即运行,但您需要等待数小时,直到您的节点高度同步。

非常重要!

cardano-wallet 在端口 8090(默认)上创建了一个公开可访问的 API。最简单和最好的方法是为访问 cardano-wallet API 创建防火墙规则,仅允许来自特定 IP 的访问。您可以在像 Hetzner、Vultr 或 DigitalOcean 这样的流行托管提供商上创建防火墙,通常免费。当然,您也可以在您的操作系统上设置自定义防火墙规则。

想要在 https 上运行 cardano-wallet API 吗?

只需为 cardano-wallet 提供的 API 设置 nginx 代理转发即可

apt-get install nginx

# create some ssl certs
openssl req -x509 -nodes -days 5478 -newkey rsa:2048 -keyout /etc/nginx/cert.key -out /etc/nginx/cert.crt

# switch to nginx dir
cd /etc/nginx/sites-enabled

# download nginx setup for adapay proxy
wget https://raw.githubusercontent.com/g33kme/adapay/main/nginx.adapay

# check nginx config and restart
service nginx configtest
systemctl restart nginx

现在,您应该能够通过 https/ssl 浏览 cardano-wallet API。请注意,我们没有创建有效的 SSL 证书,所以您会在浏览器中收到不安全的警告。然而,我们在 ADAPay 中不验证这一点。

https://<IP4-SERVER-ADDRESS>/v2/network/information

您还可以注意更新您的防火墙设置,并阻止端口 443 的公共流量。

2. 安装:ADAPay

好了,您的 cardano-wallet 已经启动运行。让我们看看我们的 ADAPay 库,以便轻松创建发票和检查付款。

此库可以通过 Composer 安装。

composer require g33kme/adapay

您也可以简单地从该存储库手动包含 source/adapay.php 到您的项目中,并使用 ADAPay PHP 类。

include('path/to/my/adapay.php');

要求

此库需要 PHP 7.1 或更高版本,如果您使用 Composer,则需要版本 2+。

3. 如何使用 ADApay

使用 ADAPay PHP 库,与您的 cardano-wallet API 一起工作非常简单。检查您的钱包,创建发票,检查接收到的付款……

/*
 * You have to setup some Settings
 */

// Define your URL to API, with version, but NO ending slash!
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

//Optional, you can define an walletid, but you can also pass a walletid in the parameters
define('ADAPAY_WALLETID', '129328d18339990c7398e02975c4513754881337');

创建或恢复 Cardano 钱包

// Define your URL to API
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

/*
 * First we need to create or restore a wallet on our cardano-wallet server
 * Keep in mind, that the cardano-node have to be fully synced so your wallet comes up
 * 
 * name              = set any name 
 * mnemonic_sentence = create new or add your existing mnemonic recovery phrase to restore a wallet, 24 words for shelly wallet
 * passphrase        = set a password
 * 
 * You will get an "id" back, save this unique id to use the wallet for ADAPay
 */

$wallet = ADAPAY::restoreWallet(array(
    'name' => 'adapay',
    'mnemonic' => 'that are just twenty four words that have no meaning and only for adapay as placeholder so dont try to copy this cheers adapay'
    'passphrase' => 'myAwesomeAdaPayPassword',
));
print_r($wallet);

/*
 * If you already created a wallet you can simply show all created wallets and grab your walletid
 */
$wallets = ADAPAY::wallets(array();
print_r($wallets);

/*
 * List all you receiving wallet cardano addresses
 * Use one  from the list to receive your payments on and create invoices
 */
$addresses = ADAPAY::walletAdresses();
print_r($addresses);

/*
 * Check current network Status, Cardano Node height
 */
$networkStatus = ADAPAY::networkStatus();
print_r($networkStatus);

创建发票

// Define your URL to API
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

/*
 * How to create an invoice
 * 
 * pair     = set a pair like ADAUSD, ADAEUR, ADAGBP
 * amount   = fiat amount, based on the pair your ada amount for the invoice will be calculated
 * identify = how to identify the paid invoice
 * death    = in seconds when the invoice expires
 * address  = set one of your cardano addresses from your created wallet where you want to receive the payment
 * walletid = set a walletid from your cardano-wallet you created in the first step
 * 
 * Each created cardano invoice return an unique hash and the calculated ADA price from your fiat amount
 * We recommend to save your invoice in a database to flag them as paid if you received it
 * 
 * Currently ADAPay will create an unique number on the payment amount to identify incoming payment
 * Creating a new Cardano addresse for each invoice is currently not easy with the Cardano Wallet API, we may provide this in the future 
 */
$invoice = ADAPAY::invoiceCreate(array(
   'pair' => 'ADAUSD',
   'amount' => 10,
   'identify' => array(
        'type' => 'amount'
   ), 
   'death' => 1800,
   'address' => 'addr1qxksn95zhgje7tvdsgfpk9t49sssz4fqewt74neh56cnl4ml8zpc3556jh8exfp70a6f3pva7yf4fmfmw52tdh3dh94sqdvu27',
   'walletid' => '129328d18339990c7398e02975c4513754881337' 
));
print_r($invoice);

验证付款

// Define your URL to API
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

/*
 * Check you invoice was paid and you got the payment to your cardano wallet
 * You can simply pass the return parameters you got in your $invoice
 * 
 */
$verify = ADAPAY::verifyPayment(array(
   'hash' => $invoice['hash'],
   'walletid' => $invoice['walletid'],
   'ada' => $invoice['ada'],
   'created' => $invoice['created'],
   'death' => $invoice['death']
));
print_r($verify);

//Or simply pass the $invoice you created
$verify = ADAPAY::verifyPayment($invoice);
print_r($verify);


if($verify['waiting']) {
    //Still waiting for the payment, not received
    
} 
elseif($verify['expired']) {
    //Invoice is expired
    
}
elseif($verify['paid']) {
     /*
     * Payment received
     * Do your database updates, flag invoice as paid etc ...
     */
    
}

获取当前 ADA 汇率(欧元、美元、英镑……)

# Get last cardano fiat price, via a pair like: ADAEUR, ADAUSD, ADAGBP ...
$lastPrice = ADAPAY::lastPrice(array('pair' => 'ADAEUR'));
print_r($lastPrice);

$fiat = 10;
$ada = $fiat / $lastPrice;
echo $fiat.' EUR are currently '.$ada.' ADA';

# Get last price in EURO
$lastPriceEuro = ADAPAY::lastPriceEuro();
print_r($lastPriceEuro);

# Get last price in Dollar
$lastPriceDollar = ADAPAY::lastPriceDollar();
print_r($lastPriceDollar);

# Get last price in British Pound
$lastPricePound = ADAPAY::lastPricePound();
print_r($lastPricePound);

更多内容

//Inspect a single cardano address
$inspect = ADAPAY::addressInspect(array('id' => 'addr1qxksn95zhgje7tvdsgfpk9t49sssz4fqewt74neh56cnl4ml8zpc3556jh8exfp70a6f3pva7yf4fmfmw52tdh3dh94sqdvu27'));
print_r($inspect);

/*
 * List all sent and receive transaction on your wallet
 * 
 * id       = Set one of your wallet ids
 * start    = Optional, set a start date in ISO8601
 * end      = Optional, set an end date in ISO8601
 */
$transactions = ADAPAY::walletTransactions(array(
    'id' => '129328d18339990c7398e02975c4513754881337',

    //List all transaction for walletid in the last hour
    'start' => date(DATE_ISO8601, time()-3600)
));
print_r($transactions);

最佳实践和建议

  • 为了最佳隐私,运行您自己的 cardano 节点,并且永远不要将您的恢复短语提供给任何第三方服务器!

  • 您可以在 Deadalus 钱包 上恢复您在服务器上创建的相同钱包。如果您在 Deadalus 有钱包,您可以看到传入的交易,管理您的钱包将更加容易,例如,如果您想向客户发送一些退款或查看更多详细信息。

  • 质押并委托您赚取的 ADA 到我们的 Ticker: GEEK 并获得额外的质押收益!如果您将 ADA 委托给我们的 GEEK 钱包池,所有持续接收的付款将自动质押。如果您的钱包增长,您的质押金额将自动增长,您将赚更多的钱。您可以在 Deadalus 钱包中轻松委托 ADA。所以,就像我们上面提到的那样,简单地在 Deadalus 上恢复您创建的钱包。

  • 直接在 Deadalus 钱包中创建第二个钱包,并将一些付款发送到您创建的 ADAPay 钱包中的某些地址以测试您的设置。

  • 为您的用户创建一个数据库表,用于您创建的发票,这样您就可以将发票标记为“已支付”。此外,如果您已将所有数据库条目连接到您的一位用户,您可以轻松显示已支付、待支付或过期的发票列表。

  • 您可以使用带有 AJAX 请求的 JavaScript setInterval 函数来检查 ADAPAY::verifyPayment(); 请记住,您应该在数据库中将创建的发票标记为“已支付”,并且只有当您尚未将条目标记为“已支付”时,才使用 ADAPAY::verifyPayment();。

<script>
var invoiceStatus = setInterval(function(){
    //need jQuery, you can set any params you want to pass
    $.post("/path/to/your/ajax.php?hash=<?=$invoice['hash];?>&param2=userid&id=dbid&param3=anything", {}, function(data){

        console.info('invoice status: '+ data);
        //if data empty valid invoice but not expired or paid

        if(data == 'paid') {
            //Do some stuff, eg hide some loader indicator 
        }
        if(data == 'waiting') {
            //Do some stuff
        }
        if(data == 'expired') {
            //Do some stuff, eg show messages invoice expired
        }
    });
}, 3000);
</script>
/*
 * This is some basic example on your ajax.php
 * Of course you can do your checks however you want
 */

//Highly recommend to clean your parameter requests, ADAPay will help
include('path/to/my/adapay.php');
$request = ADAPAY::cleanRequest();

//Now you should be save to use the parameters from your request
print_r($request);

$hash = $request['hash'];
$id = $request['id'];
$param2 = $request['param2'];
$param3 = $request['param3'];

//First get your created invoice from your database maybe from your set parameters in your Javascript Ajax request
//TODO

//Check invoice from DB
if(!empty($invoice)) {
    if($invoice['paid']) {
        echo 'paid';
    } else {
        $status = ADAPAY::verifyPayment();
        
        if($status['waiting']) {
            echo 'waiting';
        }
        elseif($status['expired']) {
            echo 'expired';
        }
        else($status['paid']) {
            echo 'paid';
            
            //Highly recommend to update your created invoice in
            //TODO
 
        }
    }
}

🙏 支持者

将您的 ADA 钩到我们的 GEEK 池子
GeekMe 投注池

☕ 想要买我一杯咖啡吗?将一些 ADA 发送到我们的捐赠地址:addr1qxksn95zhgje7tvdsgfpk9t49sssz4fqewt74neh56cnl4ml8zpc3556jh8exfp70a6f3pva7yf4fmfmw52tdh3dh94sqdvu27