Skip to content

Commit

Permalink
add support for card payments
Browse files Browse the repository at this point in the history
  • Loading branch information
mehul0810 committed Sep 14, 2020
1 parent 926347a commit 4ca81ea
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 8 deletions.
19 changes: 12 additions & 7 deletions includes/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@
function paiementpro4give_register_gateway( $gateways ) {

$gateways['paiementpro_mtn_money'] = [
'admin_label' => esc_html__( 'PaiementPro - MTN Money', 'paiementpro-for-give' ),
'checkout_label' => esc_html__( 'MTN Money', 'paiementpro-for-give' ),
'admin_label' => esc_html__( 'PaiementPro - MTN Money', 'give' ),
'checkout_label' => esc_html__( 'MTN Money', 'give' ),
];

$gateways['paiementpro_moov_money'] = [
'admin_label' => esc_html__( 'PaiementPro - Moov Money', 'paiementpro-for-give' ),
'checkout_label' => esc_html__( 'Moov Money', 'paiementpro-for-give' ),
'admin_label' => esc_html__( 'PaiementPro - Moov Money', 'give' ),
'checkout_label' => esc_html__( 'Moov Money', 'give' ),
];

$gateways['paiementpro_orange_money'] = [
'admin_label' => esc_html__( 'PaiementPro - Orange Money', 'paiementpro-for-give' ),
'checkout_label' => esc_html__( 'Orange Money', 'paiementpro-for-give' ),
'admin_label' => esc_html__( 'PaiementPro - Orange Money', 'give' ),
'checkout_label' => esc_html__( 'Orange Money', 'give' ),
];

$gateways['paiementpro_card'] = [
'admin_label' => esc_html__( 'PaiementPro - Credit Card', 'give' ),
'checkout_label' => esc_html__( 'Credit Card', 'give' ),
];

return $gateways;
}

add_filter( 'give_payment_gateways', 'paiementpro4give_register_gateway' );
add_filter( 'give_payment_gateways', 'paiementpro4give_register_gateway' );
3 changes: 2 additions & 1 deletion includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ function paiementpro4give_get_supported_payment_methods() {
'paiementpro4give_orange_money',
'paiementpro4give_mtn_money',
'paiementpro4give_moov_money',
'paiementpro4give_card',
];
}
}
126 changes: 126 additions & 0 deletions includes/payment-methods/class-paiementpro4give-card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* PaiementPro for Give | Credit Card
*
* @since 1.0.0
*/

// Bailout, if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class PaiementPro4Give_Card {
public function __construct() {
add_action( 'give_gateway_paiementpro_card', [ $this, 'process_donation' ] );
add_action( 'give_paiementpro_card_cc_form', '__return_false' );
}

public function process_donation( $data ) {
// Check for any stored errors.
$errors = give_get_errors();

if ( ! $errors ) {

$form_id = ! empty( $data['post_data']['give-form-id'] ) ? intval( $data['post_data']['give-form-id'] ) : false;
$first_name = ! empty( $data['post_data']['give_first'] ) ? $data['post_data']['give_first'] : '';
$last_name = ! empty( $data['post_data']['give_last'] ) ? $data['post_data']['give_last'] : '';
$email = ! empty( $data['post_data']['give_email'] ) ? $data['post_data']['give_email'] : '';
$donation_key = ! empty( $data['purchase_key'] ) ? $data['purchase_key'] : '';
$currency = give_get_currency( $form_id );

// Setup the donation details.
$data_to_send = [
'price' => $data['price'],
'give_form_title' => $data['post_data']['give-form-title'],
'give_form_id' => $form_id,
'give_price_id' => isset( $data['post_data']['give-price-id'] ) ? $data['post_data']['give-price-id'] : '',
'date' => $data['date'],
'user_email' => $email,
'purchase_key' => $data['purchase_key'],
'currency' => $currency,
'user_info' => $data['user_info'],
'status' => 'pending',
'gateway' => $data['gateway'],
];

// Record the pending payment.
$donation_id = give_insert_payment( $data_to_send );

// Verify donation payment.
if ( ! $donation_id ) {

// Record the error.
give_record_gateway_error(
__( 'Payment Error', 'paiementpro-for-give' ),
sprintf(
/* translators: %s: payment data */
__( 'Payment creation failed before processing payment via PaiementPro. Payment data: %s', 'paiementpro-for-give' ),
wp_json_encode( $data )
),
$donation_id
);

// Problems? Send back.
give_send_back_to_checkout( '?payment-mode=' . $data['post_data']['payment-mode'] );
}

// Auto set payment to abandoned in one hour if donor is not able to donate in that time.
wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'paiementpro4give_set_donation_abandoned', [ $donation_id ] );

$url = paiementpro4give_get_api_url();
$merchant_id = paiementpro4give_get_merchant_id();
$args = [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => [
'merchantId' => $merchant_id,
'currency' => 952, // CHF.
'amount' => $data['price'],
'channel' => 'CARD',
'customer_id' => '',
'description' => give_payment_gateway_donation_summary( $data ),
'email' => $email,
'firstname' => $first_name,
'lastname' => $last_name,
// 'phone_mobile' => '',
'referenceNumber' => $donation_key,
'notificationURL' => give_get_success_page_uri(),
'returnContext' => wp_json_encode(
[
'id_order' => $donation_id,
]
),
],
];
$response = wp_remote_post( "{$url}init2.php", $args );
$responseBody = wp_remote_retrieve_body( $response );
$responseCode = wp_remote_retrieve_response_code( $response );

if ( 200 === $responseCode ) {

$responseBodyParts = explode( '|', $responseBody );
$sessionId = $responseBodyParts[1];

$redirect_to_url = add_query_arg(
[
'sessionid' => $sessionId,
'id' => $donation_id,
],
"{$url}processing.php"
);

} else {
// Send user to failed page and change donation status to failed as well.
give_update_payment_status( $donation_id, 'failed' );
$redirect_to_url = give_get_failed_transaction_uri();
}

wp_redirect( $redirect_to_url );
give_die();
}
}
}

new PaiementPro4Give_Card();
1 change: 1 addition & 0 deletions paiementpro-for-give.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public function init() {
require_once PAIEMENTPRO4GIVE_PLUGIN_DIR . 'includes/payment-methods/class-paiementpro4give-mtn-money.php';
require_once PAIEMENTPRO4GIVE_PLUGIN_DIR . 'includes/payment-methods/class-paiementpro4give-moov-money.php';
require_once PAIEMENTPRO4GIVE_PLUGIN_DIR . 'includes/payment-methods/class-paiementpro4give-orange-money.php';
require_once PAIEMENTPRO4GIVE_PLUGIN_DIR . 'includes/payment-methods/class-paiementpro4give-card.php';
require_once PAIEMENTPRO4GIVE_PLUGIN_DIR . 'includes/filters.php';
require_once PAIEMENTPRO4GIVE_PLUGIN_DIR . 'includes/actions.php';

Expand Down

0 comments on commit 4ca81ea

Please sign in to comment.