cshenfu
2023-05-22 b8c19d1e951619a325a3de2f190725ab198b09c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// uni iap
const IapTransactionState = {
    purchasing: "0", // A transaction that is being processed by the App Store.
    purchased: "1", // A successfully processed transaction.
    failed: "2", // A failed transaction.
    restored: "3", // A transaction that restores content previously purchased by the user.
    deferred: "4" // A transaction that is in the queue, but its final status is pending external action such as Ask to Buy.
};
 
class Iap {
 
    constructor(data={}) {
        this._productIds = data.products || [];
        this._channel = null;
        this._channelError = null;
        this.ready = false;
    }
 
    init() {
        return new Promise((resolve, reject) => {
            this.getChannels((channel) => {
                this.ready = true;
                resolve(channel);
            }, (err) => {
                reject(err);
            })
        })
    }
 
    getProduct(productIds) {
        return new Promise((resolve, reject) => {
            this._channel.requestProduct(productIds || this._productIds, (res) => {
                resolve(res);
            }, (err) => {
                reject(err);
            })
        });
    }
 
    requestPayment(orderInfo) {
        return new Promise((resolve, reject) => {
            uni.requestPayment({
                provider: "appleiap",
                orderInfo: {
                    quantity: 1,
                    manualFinishTransaction: true,
                    ...orderInfo
                },
                success: (res) => {
                    resolve(res);
                },
                fail: (err) => {
                    //console.log('requestPayment-err: ', err)
                    reject(err);
                }
            });
        });
        
    }
 
    restoreCompletedTransactions(username) {
        return new Promise((resolve, reject) => {
            this._channel.restoreCompletedTransactions({
                manualFinishTransaction: true,
                username
            }, (res) => {
                resolve(res);
            }, (err) => {
                console.log('restoreCompletedTransactions-err: ', err)
                reject(err);
            })
        });
    }
 
    finishTransaction(transaction) {
        return new Promise((resolve, reject) => {
            this._channel.finishTransaction(transaction, (res) => {
                resolve(res);
            }, (err) => {
                reject(err);
            });
        });
    }
 
    getChannels(success, fail) {
        if (this._channel !== null) {
            success(this._channel)
            return
        }
 
        if (this._channelError !== null) {
            fail(this._channelError)
            return
        }
 
        uni.getProvider({
            service: 'payment',
            success: (res) => {
                this._channel = res.providers.find((channel) => {
                    return (channel.id === 'appleiap')
                })
 
                if (this._channel) {
                    success(this._channel)
                } else {
                    this._channelError = {
                        errMsg: 'paymentContext:fail iap service not found'
                    }
                    fail(this._channelError)
                }
            }
        });
    }
 
    get channel() {
        return this._channel;
    }
}
 
export default {
    Iap,
    IapTransactionState
};