5

I'm writing a website that let users submit credit card information and subscribe my web service. I'm dealing with database record and PayPal API, and I notice there would be some potential problem. That is, I can't rollback a PayPal API call if the following database operation fails. For example, let say, we create a recurring payment profile and write a record to database like this, in a RMDB transaction

Transaction begin
CreateRecurringPaymentsProfile (PayPal)
Insert a record to table 'subscription'
Transaction end

This works fine if nothing goes wrong, but what if our insertion of subscription record to database fails?

Transaction begin
CreateRecurringPaymentsProfile (PayPal)
Insert a record to table 'subscription' failed
Transaction rolled back

Of course we can rollback the transaction of database, but we cannot rollback the PayPal API call. Then we have an orphan recurring payment profile at PayPal. I have an idea, I can create the subscription record first, and update it later.

Transaction begin
Insert a record to table 'subscription'
Transaction end

Transaction begin
CreateRecurringPaymentsProfile (PayPal)
Update subscription record
Transaction end

In this way, if the database operation fails, at least we have the subscription record. This might be a possible solution, but I'm wondering what are good practices to deal with database operation cannot be rolled back like this? Especially when we are dealing with money.

Thanks.

4

1 に答える 1

3

あなたが考えたように、これを2つのステップで行うことをお勧めします。トランザクションは、最初に「初期化済み」などのステータスで作成されます。次に、PayPal の支払いが正常に返されたら、ステータスを「支払い済み」に変更します。

PayPal トランザクションが失敗した場合でも、トランザクションが保存されているという利点があるため、失敗した/放棄された支払いの回数を知ることができます。

于 2012-09-03T17:29:18.740 に答える