 wp-pay-extensions    /
                    memberpress
                      wp-pay-extensions    /
                    memberpress
                
                            | 1 | <?php | ||||
| 2 | /** | ||||
| 3 | * Pronamic | ||||
| 4 | * | ||||
| 5 | * @author Pronamic <[email protected]> | ||||
| 6 | * @copyright 2005-2021 Pronamic | ||||
| 7 | * @license GPL-3.0-or-later | ||||
| 8 | * @package Pronamic\WordPress\Pay\Extensions\MemberPress | ||||
| 9 | */ | ||||
| 10 | |||||
| 11 | namespace Pronamic\WordPress\Pay\Extensions\MemberPress; | ||||
| 12 | |||||
| 13 | use MeprTransaction; | ||||
| 14 | use Pronamic\WordPress\Money\Money; | ||||
| 15 | use Pronamic\WordPress\Money\TaxedMoney; | ||||
| 16 | use Pronamic\WordPress\Pay\AddressHelper; | ||||
| 17 | use Pronamic\WordPress\Pay\Customer; | ||||
| 18 | use Pronamic\WordPress\Pay\ContactName; | ||||
| 19 | use Pronamic\WordPress\Pay\Core\Util as Core_Util; | ||||
| 20 | use Pronamic\WordPress\Pay\Payments\Payment; | ||||
| 21 | use Pronamic\WordPress\Pay\Payments\PaymentLines; | ||||
| 22 | use Pronamic\WordPress\Pay\Subscriptions\Subscription; | ||||
| 23 | use Pronamic\WordPress\Pay\Subscriptions\SubscriptionInterval; | ||||
| 24 | use Pronamic\WordPress\Pay\Subscriptions\SubscriptionPhase; | ||||
| 25 | |||||
| 26 | /** | ||||
| 27 | * Pronamic | ||||
| 28 | * | ||||
| 29 | * @author Remco Tolsma | ||||
| 30 | * @version 3.0.1 | ||||
| 31 | * @since 2.0.5 | ||||
| 32 | */ | ||||
| 33 | class Pronamic { | ||||
| 34 | /** | ||||
| 35 | * Get Pronamic payment from MemberPress transaction. | ||||
| 36 | * | ||||
| 37 | * @param MeprTransaction $memberpress_transaction MemberPress transaction object. | ||||
| 38 | * | ||||
| 39 | * @return Payment | ||||
| 40 | */ | ||||
| 41 | 	public static function get_payment( MeprTransaction $memberpress_transaction ) { | ||||
| 42 | $payment = new Payment(); | ||||
| 43 | |||||
| 44 | // MemberPress. | ||||
| 45 | $memberpress_user = $memberpress_transaction->user(); | ||||
| 46 | $memberpress_product = $memberpress_transaction->product(); | ||||
| 47 | $memberpress_subscription = $memberpress_transaction->subscription(); | ||||
| 48 | |||||
| 49 | // Title. | ||||
| 50 | $title = sprintf( | ||||
| 51 | /* translators: %s: payment data title */ | ||||
| 52 | __( 'Payment for %s', 'pronamic_ideal' ), | ||||
| 53 | sprintf( | ||||
| 54 | /* translators: %s: order id */ | ||||
| 55 | __( 'MemberPress transaction %s', 'pronamic_ideal' ), | ||||
| 56 | $memberpress_transaction->id | ||||
| 57 | ) | ||||
| 58 | ); | ||||
| 59 | |||||
| 60 | $payment->order_id = $memberpress_transaction->id; | ||||
| 61 | $payment->title = $title; | ||||
| 62 | $payment->description = $memberpress_product->post_title; | ||||
| 63 | $payment->user_id = $memberpress_user->ID; | ||||
| 64 | $payment->source = 'memberpress'; | ||||
| 65 | $payment->source_id = $memberpress_transaction->id; | ||||
| 66 | $payment->issuer = null; | ||||
| 67 | |||||
| 68 | // Contact. | ||||
| 69 | $contact_name = new ContactName(); | ||||
| 70 | $contact_name->set_first_name( $memberpress_user->first_name ); | ||||
| 71 | $contact_name->set_last_name( $memberpress_user->last_name ); | ||||
| 72 | |||||
| 73 | $customer = new Customer(); | ||||
| 74 | $customer->set_name( $contact_name ); | ||||
| 75 | $customer->set_email( $memberpress_user->user_email ); | ||||
| 76 | $customer->set_user_id( $memberpress_user->ID ); | ||||
| 77 | |||||
| 78 | $payment->set_customer( $customer ); | ||||
| 79 | |||||
| 80 | /** | ||||
| 81 | * Address. | ||||
| 82 | * | ||||
| 83 | * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprUser.php#L1191-L1216 | ||||
| 84 | */ | ||||
| 85 | $address = AddressHelper::from_array( | ||||
| 86 | array( | ||||
| 87 | 'line_1' => $memberpress_user->address( 'one', false ), | ||||
| 88 | 'line_2' => $memberpress_user->address( 'two', false ), | ||||
| 89 | 'postal_code' => $memberpress_user->address( 'zip', false ), | ||||
| 90 | 'city' => $memberpress_user->address( 'city', false ), | ||||
| 91 | 'region' => $memberpress_user->address( 'state', false ), | ||||
| 92 | 'country_code' => $memberpress_user->address( 'country', false ), | ||||
| 93 | ) | ||||
| 94 | ); | ||||
| 95 | |||||
| 96 | 		if ( null !== $address ) { | ||||
| 97 | $address->set_name( $contact_name ); | ||||
| 98 | } | ||||
| 99 | |||||
| 100 | $payment->set_billing_address( $address ); | ||||
| 101 | $payment->set_shipping_address( $address ); | ||||
| 102 | |||||
| 103 | /* | ||||
| 104 | * Totals. | ||||
| 105 | */ | ||||
| 106 | $payment->set_total_amount( | ||||
| 107 | new TaxedMoney( | ||||
| 108 | $memberpress_transaction->total, | ||||
| 109 | MemberPress::get_currency(), | ||||
| 110 | $memberpress_transaction->tax_amount, | ||||
| 111 | $memberpress_transaction->tax_rate | ||||
| 112 | ) | ||||
| 113 | ); | ||||
| 114 | |||||
| 115 | /* | ||||
| 116 | * Vat number. | ||||
| 117 | * @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type= | ||||
| 118 | * @todo | ||||
| 119 | */ | ||||
| 120 | |||||
| 121 | /* | ||||
| 122 | * Subscription. | ||||
| 123 | * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php#L603-L618 | ||||
| 124 | */ | ||||
| 125 | $payment->subscription = self::get_subscription( $memberpress_transaction ); | ||||
| 126 | |||||
| 127 | 		if ( $payment->subscription ) { | ||||
| 128 | $payment->subscription_source_id = $memberpress_transaction->subscription_id; | ||||
| 129 | |||||
| 130 | $payment->add_period( $payment->subscription->new_period() ); | ||||
| 0 ignored issues–
                            show             Bug
    
    
    
        introduced 
                            by  
  Loading history... | |||||
| 131 | |||||
| 132 | 			if ( $memberpress_subscription->in_trial() ) { | ||||
| 133 | $payment->set_total_amount( | ||||
| 134 | new TaxedMoney( | ||||
| 135 | $memberpress_subscription->trial_total, | ||||
| 136 | MemberPress::get_currency(), | ||||
| 137 | $memberpress_subscription->trial_tax_amount, | ||||
| 138 | $memberpress_subscription->tax_rate | ||||
| 139 | ) | ||||
| 140 | ); | ||||
| 141 | } | ||||
| 142 | } | ||||
| 143 | |||||
| 144 | /* | ||||
| 145 | * Lines. | ||||
| 146 | */ | ||||
| 147 | $payment->lines = new PaymentLines(); | ||||
| 148 | |||||
| 149 | $line = $payment->lines->new_line(); | ||||
| 150 | |||||
| 151 | $line->set_id( $memberpress_product->ID ); | ||||
| 152 | $line->set_name( $memberpress_product->post_title ); | ||||
| 153 | $line->set_quantity( 1 ); | ||||
| 154 | $line->set_unit_price( $payment->get_total_amount() ); | ||||
| 155 | $line->set_total_amount( $payment->get_total_amount() ); | ||||
| 156 | $line->set_product_url( get_permalink( $memberpress_product->ID ) ); | ||||
| 0 ignored issues–
                            show It seems like  get_permalink($memberpress_product->ID)can also be of typefalse; however, parameter$product_urlofPronamic\WordPress\Pay\P...Line::set_product_url()does only seem to acceptnull|string, maybe add an additional type check?
                                                                                                                                                                                           (
                                     Ignorable by Annotation
                                ) If this is a false-positive, you can also ignore this issue in your code via the  
  Loading history... | |||||
| 157 | |||||
| 158 | /* | ||||
| 159 | * Return. | ||||
| 160 | */ | ||||
| 161 | return $payment; | ||||
| 162 | } | ||||
| 163 | |||||
| 164 | /** | ||||
| 165 | * Get Pronamic subscription from MemberPress transaction. | ||||
| 166 | * | ||||
| 167 | * @param MeprTransaction $memberpress_transaction MemberPress transaction object. | ||||
| 168 | * | ||||
| 169 | * @return Subscription|null | ||||
| 170 | */ | ||||
| 171 | 	public static function get_subscription( MeprTransaction $memberpress_transaction ) { | ||||
| 172 | $memberpress_product = $memberpress_transaction->product(); | ||||
| 173 | |||||
| 174 | 		if ( $memberpress_product->is_one_time_payment() ) { | ||||
| 175 | return null; | ||||
| 176 | } | ||||
| 177 | |||||
| 178 | $memberpress_subscription = $memberpress_transaction->subscription(); | ||||
| 179 | |||||
| 180 | 		if ( ! $memberpress_subscription ) { | ||||
| 181 | return null; | ||||
| 182 | } | ||||
| 183 | |||||
| 184 | /** | ||||
| 185 | * Subscription. | ||||
| 186 | */ | ||||
| 187 | $subscription = new Subscription(); | ||||
| 188 | |||||
| 189 | $start_date = new \DateTimeImmutable(); | ||||
| 190 | |||||
| 191 | // Trial phase. | ||||
| 192 | 		if ( $memberpress_subscription->in_trial() ) { | ||||
| 193 | $trial_phase = new SubscriptionPhase( | ||||
| 194 | $subscription, | ||||
| 195 | $start_date, | ||||
| 196 | new SubscriptionInterval( 'P' . $memberpress_subscription->trial_days . 'D' ), | ||||
| 197 | new TaxedMoney( | ||||
| 198 | $memberpress_subscription->trial_total, | ||||
| 199 | MemberPress::get_currency(), | ||||
| 200 | $memberpress_subscription->trial_tax_amount, | ||||
| 201 | $memberpress_subscription->tax_rate | ||||
| 202 | ) | ||||
| 203 | ); | ||||
| 204 | |||||
| 205 | $trial_phase->set_total_periods( 1 ); | ||||
| 206 | $trial_phase->set_trial( true ); | ||||
| 207 | |||||
| 208 | $subscription->add_phase( $trial_phase ); | ||||
| 209 | |||||
| 210 | $start_date = $trial_phase->get_end_date(); | ||||
| 211 | } | ||||
| 212 | |||||
| 213 | // Total periods. | ||||
| 214 | $total_periods = null; | ||||
| 215 | |||||
| 216 | $limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num; | ||||
| 217 | |||||
| 218 | 		if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) { | ||||
| 219 | $total_periods = $limit_cycles_number; | ||||
| 220 | } | ||||
| 221 | |||||
| 222 | // Regular phase. | ||||
| 223 | $regular_phase = new SubscriptionPhase( | ||||
| 224 | $subscription, | ||||
| 225 | $start_date, | ||||
| 0 ignored issues–
                            show It seems like  $start_datecan also be of typenull; however, parameter$start_dateofPronamic\WordPress\Pay\S...ionPhase::__construct()does only seem to acceptDateTimeImmutable, maybe add an additional type check?
                                                                                                                                                                                           (
                                     Ignorable by Annotation
                                ) If this is a false-positive, you can also ignore this issue in your code via the  
  Loading history... | |||||
| 226 | new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ), | ||||
| 227 | new Money( $memberpress_transaction->total, MemberPress::get_currency() ) | ||||
| 228 | ); | ||||
| 229 | |||||
| 230 | $regular_phase->set_total_periods( $total_periods ); | ||||
| 231 | |||||
| 232 | $subscription->add_phase( $regular_phase ); | ||||
| 233 | |||||
| 234 | return $subscription; | ||||
| 235 | } | ||||
| 236 | } | ||||
| 237 | 
