Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to use Paypal Express Checkout #886

Open
wants to merge 2 commits into
base: 6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions js/webform_civicrm_payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@
});
}
// When an express payment button is clicked, skip the billing fields and submit the form with a placeholder
var $expressButton = $billingPaymentBlock.find('input[name$=_upload_express]');
var $expressButton = $billingPaymentBlock.find('button[name$=_upload_express]');
if ($expressButton.length) {
$("input[value=Submit]").hide();
$expressButton.removeClass('crm-form-submit').click(function(e) {
e.preventDefault();
$billingPaymentBlock.find('input[name=credit_card_number]').val('express');
$(this).closest('form').find('input.webform-submit.button-primary').click();
$(this).closest('form').find('input.webform-button--submit.form-submit').click();
})
}
else {
$("input[value=Submit]").show();
}
$('fieldset.billing_name_address-group').remove();
}
});
Expand Down
8 changes: 8 additions & 0 deletions src/WebformCivicrmConfirmForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function doPayment() {
if (method_exists($paymentProcessor, 'setSuccessUrl')) {
$paymentProcessor->setSuccessUrl($paramsDoPayment['successURL']);
$paymentProcessor->setCancelUrl($paramsDoPayment['cancelURL']);

if ($paymentProcessor->supports('preApproval')) {
$preApprovalParams = $paymentProcessor->doPreApproval($paramsDoPayment);

$paramsDoPayment['token'] = $preApprovalParams['pre_approval_parameters']['token'];

\CRM_Utils_System::redirect($preApprovalParams['redirect_url']);
}
}
try {
$paymentProcessor->doPayment($paramsDoPayment);
Expand Down
69 changes: 69 additions & 0 deletions webform_civicrm.module
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,72 @@ function webform_civicrm_civicrm_pre($op, $objectName, $id, &$params) {
}
}
}

/**
* Define hook_preprocess_webform_confirmation().
*
* @param [type] $vars
* @return void
*/
function webform_civicrm_preprocess_webform_confirmation(&$vars) {

// Condition to execute when is incoming from paypal express checkout
if (!empty($_GET['sid']) && !empty($_GET['token']) && !empty($_GET['PayerID'])) {
$webform_submission = \Drupal\webform\Entity\WebformSubmission::load($_GET['sid']);
$data = $webform_submission->getData();

// ToDo verifiy if if cant be filled different thant 1
if ($data['civicrm']['contribution'][1]['id']) {
$contribution = \Civi\Api4\Contribution::get(FALSE)
->addWhere('id', '=', $data['civicrm']['contribution'][1]['id'] )
->setLimit(1)
->execute()->first();

$contribution['payer_id'] = $_GET['PayerID'];
$contribution['token'] = $_GET['token'];
$contribution['amount'] = $contribution['total_amount'];

$config = \CRM_Core_Config::singleton();
$contribution['currencyID'] = \CRM_Utils_Array::value('currency',
$contribution,
$config->defaultCurrency
);

// Get if payment processor is test or prod
$webform = $webform_submission->getWebform();

$handler_collection = $webform->getHandlers('webform_civicrm');
$instance_ids = $handler_collection->getInstanceIds();
$handler = $handler_collection->get(reset($instance_ids));
$settings = $handler->getConfiguration()['settings'];
$paymentProcessorMode = $settings['civicrm_1_contribution_1_contribution_is_test'] == 1 ? 'test' : 'live';

$paymentProcessor = \CRM_Financial_BAO_PaymentProcessor::getPayment($data['civicrm_1_contribution_1_contribution_payment_processor_id_raw'], $paymentProcessorMode);
$payment = \Civi\Payment\System::singleton()->getByProcessor($paymentProcessor);
$result = $payment->doPayment($contribution);

// Use same workflow than in civicrm-core/CRM/Contribute/Form/Contribution/Confirm.php
if (($result['payment_status_id'] ?? NULL) == 1) {
try {
civicrm_api3('contribution', 'completetransaction', [
'id' => $result['id'],
'trxn_id' => $result['trxn_id'] ?? NULL,
'payment_processor_id' => $result['payment_processor_id'] ?? $data['civicrm_1_contribution_1_contribution_payment_processor_id_raw'],
'is_transactional' => FALSE,
'fee_amount' => $result['fee_amount'] ?? NULL,
'receive_date' => $result['receive_date'] ?? NULL,
'card_type_id' => $paymentParams['card_type_id'] ?? NULL,
'pan_truncation' => $paymentParams['pan_truncation'] ?? NULL,
]);
}
catch (CRM_Core_Exception $e) {
if ($e->getErrorCode() != 'contribution_completed') {
\Civi::log()->error('CRM_Contribute_Form_Contribution_Confirm::completeTransaction CRM_Core_Exception: ' . $e->getMessage());
throw new CRM_Core_Exception('Failed to update contribution in database');
}
}
}
}
}

}