give
/
src
/
PaymentGateways
/
Gateways
/
Stripe
/
Migrations
/
AddMissingTransactionIdForUncompletedDonations.php
AddMissingTransactionIdForUncompletedDonations.php
4 years ago
AddStatementDescriptorToStripeAccounts.php
4 years ago
AddMissingTransactionIdForUncompletedDonations.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\Stripe\Migrations; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Framework\Migrations\Contracts\Migration; |
| 7 | |
| 8 | /** |
| 9 | * @since 2.19.6 |
| 10 | */ |
| 11 | class AddMissingTransactionIdForUncompletedDonations extends Migration |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @inerhitDoc |
| 16 | */ |
| 17 | public static function id() |
| 18 | { |
| 19 | return 'add-missing-transaction-id-for-uncompleted-stripe-donations'; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @inerhitDoc |
| 24 | */ |
| 25 | public static function timestamp() |
| 26 | { |
| 27 | return strtotime('2022-03-28'); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @inerhitDoc |
| 32 | */ |
| 33 | public function run() |
| 34 | { |
| 35 | $donationMetaTable = DB::prefix('give_donationmeta'); |
| 36 | $commentTable = DB::prefix('give_comments'); |
| 37 | $donationTable = DB::prefix('posts'); |
| 38 | |
| 39 | /* |
| 40 | * This SQL query will add transaction id to donation who pass following conditions: |
| 41 | * - Donation status is other than "publish". |
| 42 | * - Donation created after 20 March 2020. We released GiveWP 2.19.0 on 25th March 2022, |
| 43 | * So donation created after or on following date may not have transaction id. |
| 44 | * @see release information https://github.com/impress-org/givewp/releases/tag/2.19.0 |
| 45 | * - Donation process with Stripe payment method. |
| 46 | * - Donation has note with "Stripe Charge/Payment Intent ID" prefix. |
| 47 | * - Donation does not have transaction id. |
| 48 | */ |
| 49 | DB::query( |
| 50 | " |
| 51 | INSERT INTO $donationMetaTable (donation_id, meta_key, meta_value) |
| 52 | SELECT dm1.donation_id, '_give_payment_transaction_id',SUBSTR( gc.comment_content, 34 ) as transactionId |
| 53 | FROM $donationMetaTable as dm1 |
| 54 | INNER JOIN $commentTable as gc on gc.comment_parent = dm1.donation_id |
| 55 | INNER JOIN $donationTable as p on p.ID = dm1.donation_id |
| 56 | WHERE NOT EXISTS ( |
| 57 | SELECT * |
| 58 | FROM $donationMetaTable as dm2 |
| 59 | WHERE dm1.donation_id=dm2.donation_id |
| 60 | AND meta_key='_give_payment_transaction_id' |
| 61 | ) |
| 62 | AND p.post_status!='publish' |
| 63 | AND p.post_date > '2022-02-20' |
| 64 | AND dm1.meta_key = '_give_payment_gateway' |
| 65 | AND dm1.meta_value like '%stripe%' |
| 66 | AND gc.comment_content like '%Stripe Charge/Payment Intent ID%' |
| 67 | AND SUBSTR( gc.comment_content, 34 ) != '' |
| 68 | ORDER BY dm1.donation_id DESC |
| 69 | " |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @inerhitDoc |
| 75 | */ |
| 76 | public static function title() |
| 77 | { |
| 78 | return 'Add Missing Transaction Id For Uncompleted Stripe Donations'; |
| 79 | } |
| 80 | } |
| 81 |