PageFactory.php
5 years ago
PageSeedCommand.php
5 years ago
RecurringDonationFactory.php
5 years ago
RecurringDonationRepository.php
5 years ago
RecurringDonations.php
5 years ago
ServiceProvider.php
5 years ago
RecurringDonations.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\TestData\Addons\RecurringDonations; |
| 4 | |
| 5 | use Throwable; |
| 6 | use Give\TestData\Framework\MetaRepository; |
| 7 | |
| 8 | /** |
| 9 | * Class RecurringDonations |
| 10 | * @package Give\TestData\RecurringDonations |
| 11 | */ |
| 12 | class RecurringDonations { |
| 13 | /** |
| 14 | * @var RecurringDonationFactory |
| 15 | */ |
| 16 | private $donationFactory; |
| 17 | /** |
| 18 | * @var RecurringDonationRepository |
| 19 | */ |
| 20 | private $donationRepository; |
| 21 | |
| 22 | public function __construct( |
| 23 | RecurringDonationFactory $donationFactory, |
| 24 | RecurringDonationRepository $donationRepository |
| 25 | ) { |
| 26 | $this->donationFactory = $donationFactory; |
| 27 | $this->donationRepository = $donationRepository; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @return string |
| 32 | */ |
| 33 | public function getRecurringDonationStatus() { |
| 34 | return 'give_subscription'; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * @param int $donationID |
| 40 | * @param array $donation |
| 41 | */ |
| 42 | public function insertRecurringDonation( $donationID, $donation ) { |
| 43 | global $wpdb; |
| 44 | |
| 45 | // Check if donation status is recurring donation status |
| 46 | if ( $donation['payment_status'] !== $this->getRecurringDonationStatus() ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Factory config |
| 51 | $this->donationFactory->setAmount( $donation['payment_total'] ); |
| 52 | $this->donationFactory->setCustomerId( $donation['donor_id'] ); |
| 53 | $this->donationFactory->setParentDonationId( $donationID ); |
| 54 | $this->donationFactory->setProductId( $donation['payment_form_id'] ); |
| 55 | |
| 56 | // Start DB transaction |
| 57 | $wpdb->query( 'START TRANSACTION' ); |
| 58 | |
| 59 | try { |
| 60 | // Insert recurring donation |
| 61 | $this->donationRepository->insertDonation( |
| 62 | $this->donationFactory->definition() |
| 63 | ); |
| 64 | |
| 65 | // Update donation meta |
| 66 | $metaRepository = new MetaRepository( 'give_donationmeta', 'donation_id' ); |
| 67 | $metaRepository->persist( |
| 68 | $donationID, |
| 69 | [ |
| 70 | '_give_subscription_payment' => 1, |
| 71 | '_give_is_donation_recurring' => 1, |
| 72 | ] |
| 73 | ); |
| 74 | |
| 75 | $wpdb->query( 'COMMIT' ); |
| 76 | |
| 77 | } catch ( Throwable $e ) { |
| 78 | $wpdb->query( 'ROLLBACK' ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | } |
| 83 |