Skip to content

Commit

Permalink
Merge pull request #9 from emmajiugo/rave-woocommerce-subscription
Browse files Browse the repository at this point in the history
Rave woocommerce subscription
  • Loading branch information
temiadesina authored Apr 3, 2019
2 parents 21dd3b8 + 58a6871 commit 5e69873
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 28 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
- **Contributors:** Flutterwave Developers
- **Tags:** rave, woocommerce, payment gateway, bank account, credit card, debit card, nigeria, kenya, international, mastercard, visa, barter
- **Requires at least:** 4.4
- **Tested up to:** 5.0.3
- **Stable tag:** 2.0.0
- **Tested up to:** 5.1.1
- **Stable tag:** 2.1.0
- **License:** MIT - see below

Take payments on your store using Rave.
Expand All @@ -25,6 +25,14 @@ Rave is available in:
* __Nigeria__
* __Ghana__
* __Kenya__
* __Uganda__
* __Tanzania__
* __Rwanda__
* __South Africa__


## Recurring Payment Support
For Recurring payment, you will need to install the [WooCommerce Subscriptions](https://woocommerce.com/products/woocommerce-subscriptions/) plugin. No subscription plans is created on Rave. The WooCommerce Subscriptions plugin handles all the subscription functionality.



Expand Down Expand Up @@ -96,16 +104,13 @@ To configure the plugin, go to __WooCommerce > Settings__ from the left menu, cl

### Suggestions / Contributions

For issues and feature request, [click here](https://github.com/bosunolanrewaju/WooCommerce-Rave-Payment-Gateway/issues).
To contribute, fork the repo, add your changes and modifications then create a pull request.


### License

##### MIT License

Copyright (c) 2016 Olatunbosun Olanrewaju

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
4 changes: 3 additions & 1 deletion assets/js/flw.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ switch (curr) {
}

var processPayment = function() {
console.log(firstname+" .......... "+lastname);
// console.log(firstname+" .......... "+lastname);

var popup = getpaidSetup({
amount: amount,
Expand All @@ -87,6 +87,8 @@ var processPayment = function() {
}else{
alert(response.respmsg);
}

popup.close(); // close modal
}
});

Expand Down
5 changes: 2 additions & 3 deletions flutterwave-rave-php-sdk/lib/rave.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,6 @@ function requeryTransaction($referenceNumber){
$data = array(
'txref' => $this->txref,
'SECKEY' => $this->secretKey,
// 'last_attempt' => '1'
// 'only_successful' => '1'
);

// make request to endpoint using unirest.
Expand All @@ -444,7 +442,8 @@ function requeryTransaction($referenceNumber){

//log error
$err = $e->getMessage();
file_put_contents('Rave_Err_'.time(), (string)$err);
file_put_contents('Rave_Err_'.time(), $err);
echo $err;
}

//check the status is success
Expand Down
120 changes: 113 additions & 7 deletions includes/class.flw_wc_payment_gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ public function __construct() {
$this->country = $this->get_option( 'country' );
$this->modal_logo = $this->get_option( 'modal_logo' );

// enable saved cards
// $this->saved_cards = $this->get_option( 'saved_cards' ) === 'yes' ? true : false;

// declare support for Woocommerce subscription
$this->supports = array(
'products',
'tokenization',
'subscriptions',
'subscription_cancellation',
'subscription_suspension',
'subscription_reactivation',
'subscription_amount_changes',
'subscription_date_changes',
'subscription_payment_method_change',
'subscription_payment_method_change_customer',
'subscription_payment_method_change_admin',
'multiple_subscriptions',
);

add_action( 'admin_notices', array( $this, 'admin_notices' ) );
add_action( 'woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_action( 'woocommerce_api_flw_wc_payment_gateway', array($this, 'flw_verify_payment'));
Expand All @@ -65,7 +84,7 @@ public function __construct() {
if ( 'yes' === $this->go_live ) {
$this->base_url = 'https://api.ravepay.co';
$this->public_key = $this->live_public_key;
$this->secret_key = $this->live_secret_key;
$this->secret_key = $this->live_secret_key;

}

Expand Down Expand Up @@ -178,11 +197,19 @@ public function init_form_fields() {
'default' => 'NG'
),
'modal_logo' => array(
'title' => __( 'Modal Custom Logo', 'flw-payments' ),
'type' => 'text',
'description' => __( 'Optional - URL to your store\'s logo. Preferably a square image', 'flw-payments' ),
'default' => ''
)
'title' => __( 'Modal Custom Logo', 'flw-payments' ),
'type' => 'text',
'description' => __( 'Optional - URL to your store\'s logo. Preferably a square image', 'flw-payments' ),
'default' => ''
),
// 'saved_cards' => array(
// 'title' => __('Saved Cards', 'flw-payments' ),
// 'label' => __('Enable Payment via Saved Cards', 'flw-payments' ),
// 'type' => 'checkbox',
// 'description' => __('If enabled, users will be able to pay with a saved card during checkout. Card details are saved on Rave servers, not on your store.<br>Note that you need to have a valid SSL certificate installed.', 'flw-payments' ),
// 'default' => 'no',
// 'desc_tip' => true,
// ),

);

Expand All @@ -196,14 +223,15 @@ public function init_form_fields() {
public function process_payment( $order_id ) {

$order = wc_get_order( $order_id );

return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);

}


/**
* Handles admin notices
*
Expand Down Expand Up @@ -502,6 +530,84 @@ public function flw_rave_webhooks() {

}

/**
* Save Customer Card Details
*/
public static function save_card_details( $rave_response, $user_id, $order_id ) {

if ( isset( $rave_response->card->card_tokens[0]->embedtoken ) ) {
$token_code = $rave_response->card->card_tokens[0]->embedtoken;
} else {
$token_code = '';
}

// save payment token to the order
self::save_subscription_payment_token( $order_id, $token_code );
// $save_card = get_post_meta( $order_id, '_wc_rave_save_card', true );

// if ( isset( $rave_response->data->card ) && $user_id && self::saved_cards && $save_card && ! empty( $token_code ) ) {

// $last4 = $rave_response->data->card->last4digits;

// if ( 4 !== strlen( $rave_response->data->card->expiryyear ) ) {
// $exp_year = substr( date( 'Y' ), 0, 2 ) . $rave_response->data->card->expiryyear;
// } else {
// $exp_year = $rave_response->data->card->expiryyear;
// }

// $brand = $rave_response->data->card->brand;
// $exp_month = $rave_response->data->card->expirymonth;
// $token = new WC_Payment_Token_CC();
// $token->set_token( $token_code );
// $token->set_gateway_id( 'rave' );
// $token->set_card_type( $brand );
// $token->set_last4( $last4 );
// $token->set_expiry_month( $exp_month );
// $token->set_expiry_year( $exp_year );
// $token->set_user_id( $user_id );
// $token->save();

// }

// delete_post_meta( $order_id, '_wc_rave_save_card' );
}

/**
* Save payment token to the order for automatic renewal for further subscription payment
*/
public function save_subscription_payment_token( $order_id, $payment_token ) {

if ( ! function_exists ( 'wcs_order_contains_subscription' ) ) {
return;
}

if ( WC_Subscriptions_Order::order_contains_subscription( $order_id ) && ! empty( $payment_token ) ) {

// Also store it on the subscriptions being purchased or paid for in the order
if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) {

$subscriptions = wcs_get_subscriptions_for_order( $order_id );

} elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) {

$subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id );

} else {

$subscriptions = array();

}

foreach ( $subscriptions as $subscription ) {

$subscription_id = $subscription->get_id();

update_post_meta( $subscription_id, '_rave_wc_token', $payment_token );

}
}
}

}

?>
Loading

0 comments on commit 5e69873

Please sign in to comment.