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