React Native:如何仅获取 Apple Receipt 并通过 API 调用传递它

React Native: How to grab only Apple Receipt and pass it through an API call

我无法仅从承诺中获取 Apple 收据。

这是通常的回应: 承诺 {_40: 0, _65: 0, _55: null, _72: null} _55: null 包含收据,而 null 实际上是收据,因为正文太大而无法读取。

我还在 API 调用中使用 Ignite-Andross 样板。他们正在使用 apisauce。

我正在使用 dooboolab 的 react-native-iap。 我已经尝试过这些选项。

  getLatestReceipt = async () => {
    var recentReceipt = await RNIap.requestReceiptIOS();
    this.setState({latestReceipt: recentReceipt});
    // Alert.alert(this.state.latestReceipt);
    console.log(this.state.latestReceipt);
  };

这会在控制台中打印出正确的当前收据。

但是,当我尝试传递当前收据时,该字段被读取为未定义、null 或 Promise {_40: 0, _65: 0, _55: null, _72: null}。

  var data = {
    apple_request: [
      {
        subscription_number: "A-S00000001",
        // Apple Receipt
       receipt_id: this.state.latestReceipt
      }
    ]
  };

我尝试的另一个解决方案是 JSON.parse(latestReceipt)["_55"] 但是,结果返回为 0-M、1-I、2-I、3-m、4-B... 它吐出每个单独的收据索引,而我需要 _55 的整个值。

最后,唯一可行的方法是在 receipt_id 字段中对整个 Apple 收据进行硬编码。我正在寻找避免这种情况的方法。

预期输出

receipt_id: 读取任何苹果收据

这个问题的解决方案是 API 调用和包含 JSON 主体的函数都需要是异步调用。 环绕:

async function getReceipt() {
    const latestReceipt = await RNIap.requestReceiptIOS();
    api.post(
      url,
      {
        apple_request: [
          {
            subscription: ${subscription_number},
            // Apple Receipt
            receipt_data: latestReceipt
          }
        ]
      },
      {
        headers: {
          "API-Token": ${api_token}
        }
      }
    );
  }

Api async/await 调用:

 const receiptCall = async () => await getReceipt();