平台 API 允许第三方应用程序与 Pi 服务器通信。您可以请求有关 Pi 应用平台上部署的应用程序及其相关交易或先锋用户的信息,以及使用您应用程序的先锋用户的信息。
进行 API 调用
标头和授权方法
Pi 服务器会通过这种方式判断请求是否来自已批准的来源。根据所请求的端点不同,有两种格式。
访问令牌(持有者令牌)
这些 API 调用需要您提供 Pioneer 的访问令牌才能获取资源。它们通常与 Pioneer 的数据相关(例如:/me)。该令牌由 Pi 应用平台 SDK 的 Pi.Authenticate 函数返回。
可以使用以下授权标头访问这些端点:
Authorization: Bearer <Pioneer's access token>
示例代码:
const headers = { headers: { authorization: "Bearer " + { PioneerAccessToken } }};
axios.get("https://api.minepi.com/v2/me", headers);服务器 API 密钥(授权密钥)
由于各种原因,某些 API 调用必须从应用程序的后端或服务器发出。要获取 API 请求的授权密钥,请参阅本指南中关于开发者门户的章节。
可以使用以下授权标头访问这些端点:
Authorization: Key <App Server API Key>
示例代码调用/payments端点,使用 paymentID 字符串获取付款信息:
const postingURL = `https://api.minepi.com/v2/payments/${ payment_id }`;
const headers = { headers: { authorization: `key ${ APIKEY }` } };
axios.get(postingURL, null, headers);API 参考
/我
访问先驱者的资源并检索先驱者的信息。
GET api.minepi.com/v2/me
授权方式:访问令牌;
响应类型:UserDTO
如果令牌已被篡改,则请求将失败(401 HTTP 错误代码),因为被篡改的访问令牌不属于任何 Pioneer。
/付款
本指南的“Pi支付流程”部分将更详细地介绍支付流程。如果您是第一次使用Pi支付,强烈建议您同时阅读该部分。该部分详细介绍了Pi支付的发起和完成方式。本节仅介绍API调用。
所有支付 API 都使用基础路由/payments。请勿使用平台 API 创建支付请求。请使用客户端 JavaScript SDK 来实现此目的。
获取信息:
GET api.minepi.com/v2/payments/{payment_id}授权方式:服务器 API 密钥
响应类型:PaymentDTO
批准付款:
这会将支付标记为已在 Pi 服务器上获得批准,从而使 Pioneer 能够批准并将交易提交到区块链。此参数来自 Pi 应用平台 SDKpaymentID的回调函数。有关该函数的更多信息,请参阅Pi 应用平台 SDK页面。此调用需要将参数从客户端传递到服务器端。onReadyForServerApprovalpaymentID
一旦数据paymentID到达服务器端,就/approve使用该数据调用端点来paymentID识别付款:
POST api.minepi.com/v2/payments/{payment_id}/approve授权方式:服务器 API 密钥
响应类型:PaymentDTO
以下是调用端点的代码/approve以及console.log返回的结果。PaymentDTO
//Using a fake paymentID for demonstration, would be passed from client side
const paymentID = eWXszS7lkfsRLHcrDRghLpDD5tHc;
const payment_id = paymentID; //Passed from the Client Side
const headers = { headers: { authorization: `key ${ APIKEY }` } }; //Stored in a .env file
const postingURL = `https://api.minepi.com/v2/payments/${ payment_id }/approve`;
let paymentDTO = await axios.post(postingURL, null,headers);
console.log(paymentDTO)完成付款:
此操作会向 Pi 服务器证明您的应用已获取付款的交易 ID (txID),从而将付款标记为已完成。这是付款流程关闭、Pioneer 返回您的应用之前的最后一步。txIDtxID通过 Pi 应用平台 SDK 的回调函数获取onReadyForServerCompletion。将该 ID 传递txID给您的服务器端并使用它来调用此端点即可完成付款。
别忘了这一点
POST api.minepi.com/v2/payments/{payment_id}/complete授权方式:服务器 API 密钥
响应类型:PaymentDTO
示例请求
//Using a fake txID string for demonstration, would be passed from client side
const txid = "7a7ed20d3d72c365b9019baf8dc4c4e3cce4c08114d866e47ae157e3a796e9e7"
const payment_id = payment_id; //Passed from client side
const tx_id = { txid: txid }; //In production passed from client side
const headers ={headers:{ authorization: `key ${ APIKEY }` } }; //API stored in .env file
const postingURL = `https://api.minepi.com/v2/payments/${ payment_id }/complete`
let paymentDTO = await axios.post(postingURL, tx_id, headers)
console.log(paymentDTO)资源类型
目前 Pi App Platform API 返回两个资源:“AuthResults” 和PaymentDTO。将来可能会有更多资源。
用户DTO
{
"uid": string, // An app-specific Pioneer identifier
"username": string, // The Pioneer's Pi username. Requires the `username` scope.
}
PaymentDTO
{
// Payment data:
"identifier": string, // The payment identifier
"Pioneer_uid": string, // The Pioneer's app-specific ID
"amount": number, // The payment amount
"memo": string, // A string provided by the developer, shown to the Pioneer
"metadata": Object, // An object provided by the developer for their own usage
"to_address": string, // The recipient address of the blockchain transaction
"created_at": string, // The payment's creation timestamp
// Status flags representing the current state of this payment
"status": {
"developer_approved": boolean, // Server-Side Approval
"transaction_verified": boolean, // Blockchain transaction verified
"developer_completed": boolean, // Server-Side Completion
"canceled": boolean, // Canceled by the developer or by Pi Network
"Pioneer_cancelled": boolean, // Canceled by the Pioneer
},
// Blockchain transaction data:
"transaction": null | { // This is null if no transaction has been made yet
"txid": string, // The id of the blockchain transaction
"verified": boolean, // True if the transaction matches the payment, false otherwise
"_link": string, // A link to the operation on the Blockchain API
},
};
Pi 打赏
微信打赏