PluginProbe
MakeCommerce for WooCommerce / 3.0.2
MakeCommerce for WooCommerce v3.0.2
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 3.0.2, at payment/gateway/subscription.php

89 lines 3.4 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 $order_id = \WC_Subscriptions_Renewal_Order::get_parent_order_id( $order );
41
42 $payment_token = get_post_meta( $order_id, '_makecommerce_payment_token', true );
43 $payment_token_valid_until = get_post_meta( $order_id, '_makecommerce_payment_token_valid_until', true );
44
45 error_log( $payment_token.'=>'. $payment_token_valid_until.'=>'. $order->get_status() );
46
47 $body = array(
48 'transaction' => array(
49 'amount' => ( string )sprintf( "%.2f", $amount ),
50 'currency' => method_exists( $order, 'get_currency' ) ? $order->get_currency() : $order->currency,
51 'reference' => $order->get_id(),
52 ),
53 'customer' => array(
54 'email' => $order->get_billing_email(),
55 'ip' => $order->get_customer_ip_address(),
56 'country' => strtolower( $order->get_billing_country() ),
57 'locale' => strtolower( \MakeCommerce\i18n::get_two_char_locale() ),
58 )
59 );
60
61 $transaction = $this->MK->createTransaction( $body );
62
63 $paymentRequest = array(
64 'amount' => (string )sprintf( "%.2f", $transaction->amount ),
65 'currency' => $transaction->currency,
66 'token' => $payment_token
67 );
68
69 try {
70
71 $payment = $this->MK->createPayment( $transaction->id, $paymentRequest );
72 } catch ( \Exception $e ) {
73
74 error_log( 'Payment failed ['.$e->getMessage().']' );
75 $order->add_order_note( __( 'Unable to renew subscription', 'wc_makecommerce_domain' )."\r\n".$e->getMessage() );
76
77 return new \WP_Error( 'makecommerce_payment_declined', __( 'Renewal payment was declined', 'wc_makecommerce_domain' ) );
78 }
79
80 $orderNote = array();
81 $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>';
82 $orderNote[] = __( 'Payment option', 'wc_makecommerce_domain' ) . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true );
83
84 $order->add_order_note( implode( "\r\n", $orderNote ) );
85 $order->payment_complete( $transaction->id );
86
87 return true;
88 }
89 }