无法通过订阅优惠购买

时间:2019-05-15 12:06:09

标签: ios node.js swift in-app-purchase auto-renewing

我正在尝试使In-App Purchase订阅优惠有效。因此,我从服务器获取了编码的签名,随机数,时间戳和密钥标识符。我创建一个SKPaymentDiscount对象,并将其设置为paymentDiscount对象的SKMutablePayment

在第一个弹出窗口上显示预期的修改价格->输入密码并继续->第二个弹出窗口:确认订阅:确定->第三个弹出窗口:显示以下错误无法购买 与开发人员联系以获取更多信息。

我尝试传递产品的不适用的商品标识。然后它抛出适当的错误,说:这不能应用于此。

PromoOfferAPI.prepareOffer(usernameHash: "name", productIdentifier: "bundleid.product", offerIdentifier: "TEST10") { (result) in
            switch result {

            case let .success(discount):
                // The original product being purchased.
                let payment = SKMutablePayment(product: option.product)
                // You must set applicationUsername to be the same as the one used to generate the signature.
                payment.applicationUsername = "name"
                // Add the offer to the payment.
                payment.paymentDiscount = discount
                // Add the payment to the queue for purchase.
                SKPaymentQueue.default().add(payment)
                break
            case let .customFail(message):
                print(message)
                break
            case let .failure(error):
                print(error.localizedDescription)
                break
            }
        }

无论我尝试多少次,它总是给我同样的错误。 无法购买 与开发人员联系以获取更多信息。可以采取哪些措施解决此问题。任何帮助深表感谢。

预先感谢!

编辑1:它永远不会进入updatedTransactions函数。它只记录Finishing transaction for payment "bundleid.product" with state: failed.

编辑2:出现错误: 代码-12(无效签名)。无法连接到iTunes Store

生成编码签名的Node.JS代码。

const UUID = require("uuid-v4");
const microtime = require('microtime');
const express = require('express');
const router = express.Router();
const EC = require("elliptic").ec;
const ec = new EC("secp256k1");
const crypto = require('crypto');

const privateKey = `-----BEGIN PRIVATE KEY-----
key goes here
-----END PRIVATE KEY-----`;
//const key = ec.keyFromPrivate(privateKey,'hex');


router.post('/',(req, res)=>{
    const bundle_id = "bundle.id";
    const key_id = "keyed";
    const nonce = String(UUID()).toLowerCase();// Should be lower case
    const timestamp = microtime.now();

    const product = req.body.product;
    const offer = req.body.offer;
    const application_username = req.body.application_username;

    const payload = bundle_id + '\u2063' + key_id + '\u2063' + product + '\u2063' + offer + '\u2063' + application_username + '\u2063' + String(nonce) + '\u2063' + String(timestamp)
    let shaMsg = crypto.createHash("sha256").update(payload).digest();
    let signature = ec.sign(shaMsg, privateKey, {canonical: true});
    let derSign = signature.toDER();
    let buff = new Buffer(derSign);  
    let base64EncodedSignature = buff.toString('base64');
    let response = {
        "signeture": base64EncodedSignature,
        "nonce": nonce,
        "timestamp": timestamp,
        "keyIdentifier": key_id
    }
    res.type('json').send(response);
});

module.exports = router;

3 个答案:

答案 0 :(得分:1)

经过多次试验和错误,弄清了问题所在。基本上是由于错误的算法,以及到处都是小问题。这是Node.js中的完整代码,希望对您有所帮助。

  // https://developer.apple.com/documentation/storekit/in-app_purchase/generating_a_signature_for_subscription_offers
  // Step 1
  const appBundleID = req.body.appBundleID
  const keyIdentifier = req.body.keyIdentifier
  const productIdentifier = req.body.productIdentifier
  const offerIdentifier = req.body.offerIdentifier
  const applicationUsername = req.body.applicationUsername

  const nonce = uuid4()
  const timestamp = Math.floor(new Date())

  // Step 2
  // Combine the parameters into a UTF-8 string with 
  // an invisible separator ('\u2063') between them, 
  // in the order shown:
  // appBundleId + '\u2063' + keyIdentifier + '\u2063' + productIdentifier + 
  // '\u2063' + offerIdentifier + '\u2063' + applicationUsername + '\u2063' + 
  // nonce + '\u2063' + timestamp

  let payload = appBundleID + '\u2063' + keyIdentifier + '\u2063' + productIdentifier + '\u2063' + offerIdentifier + '\u2063' + applicationUsername + '\u2063' + nonce+ '\u2063' + timestamp

  // Step 3
  // Sign the combined string
  // Private Key - p8 file downloaded
  // Algorithm - ECDSA with SHA-256

  const keyPem = fs.readFileSync('file_name.pem', 'ascii');
  // Even though we are specifying "RSA" here, this works with ECDSA
  // keys as well.
  // Step 4
  // Base64-encode the binary signature
  const sign = crypto.createSign('RSA-SHA256')
                   .update(payload)
                   .sign(keyPem, 'base64');

  let response1 = {
    "signature": sign,
    "nonce": nonce,
    "timestamp": timestamp,
    "keyIdentifier": keyIdentifier
  }
  res.type('json').send(response1);

答案 1 :(得分:0)

在测试他们提供的新WWDC2019示例Node.js服务器文件时遇到了相同的问题。遵循自述文件之后,我能够成功生成签名。

但是,令我惊讶的是,无效的签名看起来就像是有效的签名,我花了一段时间才意识到我的签名是无效的。

我的错误如下:我使用Alamofire向服务器发出GET请求,如下所示:

    File datafile = new File(databasepath, AUDIO_RECORDER_FOLDER);

    String dataname = datafile.getAbsolutePath();

    String[] datas = datafile.getAbsoluteFile().list();

WWDC2019 Video on Subscription Offers中提供的文件中,在index.js文件中,它们正在加载我在请求中传递的参数,如下所示:

AF.request("myserver:3000/offer", parameters: parameters).responseJSON { response in

        var signature: String?
        var keyID: String?
        var timestamp: NSNumber?
        var nonce: UUID?

        switch response.result {
        case let .success(value):
            let json = JSON(value)

            // Get required parameters for creating offer
            signature = json["signature"].stringValue
            keyID = json["keyID"].stringValue
            timestamp = json["timestamp"].numberValue
            nonce = UUID(uuidString: json["nonce"].stringValue)

        case let .failure(error):
            print(error)
            return
        }

        // Create offer
        let discountOffer = SKPaymentDiscount(identifier: offerIdentifier, keyIdentifier: keyID!, nonce: nonce!, signature: signature!, timestamp: timestamp!)

        // Pass offer in completion block
        completion(discountOffer) // this completion is a part of the method where this snippet is running
    }
}

但是,我的alamofire请求没有传递体内的参数,而是传递了查询参数。因此,服务器正在生成带有空appBundleID以及其他空字段的签名!因此,我将上述index.js部分更改为以下内容:

const appBundleID = req.body.appBundleID;
const productIdentifier = req.body.productIdentifier;
const subscriptionOfferID = req.body.offerID;
const applicationUsername = req.body.applicationUsername;

我希望这对任何忽略此问题的人都有帮助。请原谅我的不安全行为,但我希望你明白这一点!

答案 2 :(得分:0)

在我的+ Apple的WWDC19节点impl和上面的那个之间进行了几天的交换之后,我发现我的问题不是将iOS的applicationUsername设置为与节点中使用的applicationUsername匹配。具体而言,设置折扣的SKMutablePayment对象iOS端。希望一些幸运的病人能在比我少花几个小时的时间后看到这一点。