使用Greenfield API/PHP整合BTCPay Server:发票创建后无法跳转结账链接
问题:BTCPay Server发票创建后无法跳转至结账链接
我正尝试将BTCPay Server整合到自身网站中,本人为PHP新手。目前发票创建功能可正常运行,但用户点击比特币支付后,成功生成新发票却无法跳转至结账链接。我使用官方Greenfield PHP库辅助整合。
我的create_invoice.php代码
session_start(); // Include autoload file. require __DIR__ . '/../vendor/autoload.php'; // Import Invoice client class. use BTCPayServer\Client\Invoice; use BTCPayServer\Client\InvoiceCheckoutOptions; use BTCPayServer\Util\PreciseNumber; // Fill in with your BTCPay Server data. $apiKey = 'aaaaaaaaaa'; $host = 'https://aa.demo.btcpayserver.org/'; $storeId = 'aaaaaa'; $amount = $_POST['amount']; $currency = 'USD'; $orderId = $_SESSION['name']."#" . mt_rand(0, 1000); $buyerEmail = $_SESSION['name']; try { $client = new Invoice($host, $apiKey); var_dump( $client->createInvoice( $storeId, $currency, PreciseNumber::parseString($amount), $orderId, $buyerEmail ) ); } catch (\Throwable $e) { echo "Error: " . $e->getMessage(); }
表单提交后返回的Invoice对象(已替换敏感信息)
object(BTCPayServer\Result\Invoice)#6 (1) { ["data":"BTCPayServer\Result\AbstractResult":private] => array(16) { ["id"]=> string(22) "aaaaaaaaaaa" ["storeId"]=> string(44) "ssssssssssssssssssssssssssss" ["amount"]=> string(4) "18.0" ["checkoutLink"]=> string(62) "https://demo.aa.btcpayserver.org/i/aaaaaaaaaaaaaa" ["status"]=> string(3) "New" ["additionalStatus"]=> string(4) "None" ["monitoringExpiration"]=> int(1669087659) ["expirationTime"]=> int(1669080459) ["createdTime"]=> int(1669078659) ["availableStatusesForManualMarking"]=> array(2) { [0]=> string(7) "Settled" [1]=> string(7) "Invalid" } ["archived"]=> bool(false) ["type"]=> string(8) "Standard" ["currency"]=> string(3) "USD" ["metadata"]=> array(1) { ["orderId"]=> string(4) "#501" } ["checkout"]=> array(10) { ["speedPolicy"]=> string(9) "HighSpeed" ["paymentMethods"]=> array(1) { [0]=> string(3) "BTC" } ["defaultPaymentMethod"]=> NULL ["expirationMinutes"]=> int(30) ["monitoringMinutes"]=> int(120) ["paymentTolerance"]=> float(0) ["requiresRefundEmail"]=> NULL ["defaultLanguage"]=> NULL } ["receipt"]=> array(3) { ["enabled"]=> NULL ["showQR"]=> NULL ["showPayments"]=> NULL } } }
我的需求是在发票创建后跳转至该checkoutLink,但尝试以下代码时出现错误:Error: Cannot access offset of type string on string
if ($response->getStatus() === 200) { $data1 = $response->getBody(); $link1 = $data1["checkoutLink"]; echo $link1; } else { throw $this->getExceptionByStatusCode($method, $url, $response); }
解决方案
错误原因
你尝试的代码是针对原始HTTP响应的处理逻辑,但Greenfield PHP库已经将API响应自动解析成了BTCPayServer\Result\Invoice对象,不需要再手动处理getStatus()或getBody()。getBody()返回的是字符串格式的响应内容,不是数组,所以用数组方式访问会触发偏移量错误。
修改后的代码
直接从返回的Invoice对象中获取结账链接并跳转:
session_start(); require __DIR__ . '/../vendor/autoload.php'; use BTCPayServer\Client\Invoice; use BTCPayServer\Util\PreciseNumber; $apiKey = 'aaaaaaaaaa'; $host = 'https://aa.demo.btcpayserver.org/'; $storeId = 'aaaaaa'; $amount = $_POST['amount']; $currency = 'USD'; $orderId = $_SESSION['name']."#" . mt_rand(0, 1000); $buyerEmail = $_SESSION['name']; try { $client = new Invoice($host, $apiKey); // 保存生成的Invoice对象 $invoice = $client->createInvoice( $storeId, $currency, PreciseNumber::parseString($amount), $orderId, $buyerEmail ); // 两种方式获取结账链接二选一 $checkoutLink = $invoice['checkoutLink']; // 或者使用getter方法:$checkoutLink = $invoice->getCheckoutLink(); // 跳转至BTCPay结账页面 header("Location: " . $checkoutLink); exit; // 跳转后必须终止脚本执行,避免后续代码运行 } catch (\Throwable $e) { echo "Error: " . $e->getMessage(); }
内容的提问来源于stack exchange,提问作者mike_11
相关产品推荐
相关产品推荐

