PluginProbe
MakeCommerce for WooCommerce / trunk
MakeCommerce for WooCommerce vtrunk
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / payment / gateway / subscription.php

subscription.php in MakeCommerce for WooCommerce trunk, at payment/gateway/subscription.php

98 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce\Payment\Gateway;
4
5 trait Subscription {
6
7 /**
8 * Subscription payment, called by hooks woocommerce_scheduled_subscription_payment_[id] and scheduled_subscription_payment_[id]
9 *
10 * @since 3.0.0
11 */
12 public function process_subscription_payment_start( $amount_to_charge, $renewal_order, $product_id = '' ) {
13
14 $result = $this->process_subscription_payment( $renewal_order, $amount_to_charge );
15
16 if ( is_wp_error( $result ) ) {
17 \WC_Subscriptions_Manager::process_subscription_payment_failure_on_order( $renewal_order, $product_id );
18 } else {
19 \WC_Subscriptions_Manager::process_subscription_payments_on_order( $renewal_order );
20 }
21 }
22
23 /**
24 * Process subscription payment itself
25 *
26 * @since 3.0.0
27 */
28 public function process_subscription_payment( $order, $amount = 0 ) {
29
30 if ( 0 === $amount ) {
31 $order->payment_complete();
32
33 return true;
34 }
35
36 if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() ){
37 return true;
38 }
39
40 $payment_token = $order->get_meta( '_makecommerce_payment_token', true );
41 $payment_token_valid_until = $order->get_meta( '_makecommerce_payment_token_valid_until', true );
42
43 // Subscription order token missing, try parent order
44 if ( empty( $payment_token ) || empty( $payment_token_valid_until ) ) {
45 $parent_id = \WC_Subscriptions_Renewal_Order::get_parent_order_id( $order );
46 $parent_order = wc_get_order( $parent_id );
47
48 $payment_token = $parent_order->get_meta( '_makecommerce_payment_token', true );
49 $payment_token_valid_until = $parent_order->get_meta( '_makecommerce_payment_token_valid_until', true );
50 }
51
52 error_log( $payment_token.'=>'. $payment_token_valid_until.'=>'. $order->get_status() );
53
54 $body = array(
55 'transaction' => array(
56 'amount' => ( string )sprintf( "%.2f", $amount ),
57 'currency' => method_exists( $order, 'get_currency' ) ? $order->get_currency() : $order->currency,
58 'reference' => $order->get_order_number(),
59 ),
60 'customer' => array(
61 'email' => $order->get_billing_email(),
62 'ip' => $order->get_customer_ip_address(),
63 'country' => strtolower( $order->get_billing_country() ),
64 'locale' => strtolower( \MakeCommerce\i18n::get_two_char_locale() ),
65 )
66 );
67
68 $transaction = $this->MK->createTransaction( $body );
69
70 $paymentRequest = array(
71 'amount' => (string )sprintf( "%.2f", $transaction->amount ),
72 'currency' => $transaction->currency,
73 'token' => $payment_token
74 );
75
76 try {
77
78 $payment = $this->MK->createPayment( $transaction->id, $paymentRequest );
79 } catch ( \Exception $e ) {
80
81 error_log( 'Payment failed ['.$e->getMessage().']' );
82 $order->add_order_note( __( 'Unable to renew subscription', 'wc_makecommerce_domain' )."\r\n".$e->getMessage() );
83
84 return new \WP_Error( 'makecommerce_payment_declined', __( 'Renewal payment was declined', 'wc_makecommerce_domain' ) );
85 }
86
87 $orderNote = array();
88 $orderNote[] = __( 'Transaction ID', 'wc_makecommerce_domain' ) . ': <a target=_blank href="'.$this->MK->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transaction->id .'">'.$transaction->id.'</a>';
89 $orderNote[] = __( 'Payment option', 'wc_makecommerce_domain' ) . ': ' . $order->get_meta( '_makecommerce_preselected_method', true );
90
91 $order->add_order_note( implode( "\r\n", $orderNote ) );
92 $order->payment_complete( $transaction->id );
93
94 return true;
95 }
96
97 }
98