From 7d10f352785c70bf67347e63846986223759cea3 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Fri, 2 Oct 2020 20:23:53 +1000 Subject: [PATCH] MDL-69166 core_payment: Add paymentarea to the payment subsystem --- enrol/fee/classes/payment/provider.php | 6 ++- enrol/fee/templates/payment_region.mustache | 2 + lib/db/install.xml | 6 +-- lib/db/upgrade.php | 43 ++++++++++++++++++ payment/amd/build/gateways_modal.min.js | Bin 4428 -> 4476 bytes payment/amd/build/gateways_modal.min.js.map | Bin 11601 -> 11956 bytes payment/amd/build/repository.min.js | Bin 444 -> 460 bytes payment/amd/build/repository.min.js.map | Bin 1941 -> 2115 bytes payment/amd/src/gateways_modal.js | 13 ++++-- payment/amd/src/repository.js | 10 ++-- .../external/get_gateways_for_currency.php | 19 ++++---- payment/classes/helper.php | 34 +++++++++----- payment/classes/local/callback/provider.php | 8 ++-- .../paypal/amd/build/gateways_modal.min.js | Bin 4761 -> 4779 bytes .../amd/build/gateways_modal.min.js.map | Bin 10123 -> 10400 bytes .../paypal/amd/build/repository.min.js | Bin 400 -> 416 bytes .../paypal/amd/build/repository.min.js.map | Bin 1797 -> 2236 bytes .../gateway/paypal/amd/src/gateways_modal.js | 12 +++-- payment/gateway/paypal/amd/src/repository.js | 13 ++++-- .../classes/external/get_config_for_js.php | 17 ++++--- .../classes/external/transaction_complete.php | 17 ++++--- payment/tests/accounts_test.php | 4 +- payment/tests/generator/lib.php | 11 +++-- 23 files changed, 150 insertions(+), 65 deletions(-) diff --git a/enrol/fee/classes/payment/provider.php b/enrol/fee/classes/payment/provider.php index 2a6d43b7357..8edd405c80f 100644 --- a/enrol/fee/classes/payment/provider.php +++ b/enrol/fee/classes/payment/provider.php @@ -36,10 +36,11 @@ class provider implements \core_payment\local\callback\provider { /** * Callback function that returns the enrolment cost for the course that $instanceid enrolment instance belongs to. * + * @param string $paymentarea * @param int $instanceid The enrolment instance id * @return array['amount' => float, 'currency' => string, 'accountid' => int] */ - public static function get_cost(int $instanceid): array { + public static function get_cost(string $paymentarea, int $instanceid): array { global $DB; $instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST); @@ -54,11 +55,12 @@ class provider implements \core_payment\local\callback\provider { /** * Callback function that delivers what the user paid for to them. * + * @param string $paymentarea * @param int $instanceid The enrolment instance id * @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference * @return bool Whether successful or not */ - public static function deliver_order(int $instanceid, int $paymentid): bool { + public static function deliver_order(string $paymentarea, int $instanceid, int $paymentid): bool { global $DB, $USER; $instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST); diff --git a/enrol/fee/templates/payment_region.mustache b/enrol/fee/templates/payment_region.mustache index b1aa574dc7b..f39babe421a 100644 --- a/enrol/fee/templates/payment_region.mustache +++ b/enrol/fee/templates/payment_region.mustache @@ -24,6 +24,7 @@ Data attributes required for JS: * data-component + * data-paymentarea * data-componentid * data-cost * data-description @@ -58,6 +59,7 @@ type="button" id="gateways-modal-trigger-{{ uniqid }}" data-component="enrol_fee" + data-paymentarea="fee" data-componentid="{{instanceid}}" data-cost="{{cost}}" data-description={{# quote }}{{description}}{{/ quote }} diff --git a/lib/db/install.xml b/lib/db/install.xml index 187e8c7b52b..5fbfe08dfe8 100755 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -4321,7 +4321,8 @@ - + + @@ -4337,9 +4338,8 @@ - - +
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 22075e1a667..9cf500dad8b 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2974,5 +2974,48 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021052500.26); } + if ($oldversion < 2021052500.27) { + + // Define field paymentarea to be added to payments. + $table = new xmldb_table('payments'); + $field = new xmldb_field('paymentarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'component'); + + // Conditionally launch add field paymentarea. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Define index component (not unique) to be dropped form payments. + $table = new xmldb_table('payments'); + $index = new xmldb_index('component', XMLDB_INDEX_NOTUNIQUE, ['component']); + + // Conditionally launch drop index component. + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } + + // Define index componentid (not unique) to be dropped form payments. + $table = new xmldb_table('payments'); + $index = new xmldb_index('componentid', XMLDB_INDEX_NOTUNIQUE, ['componentid']); + + // Conditionally launch drop index componentid. + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } + + // Define index component-paymentarea-componentid (not unique) to be added to payments. + $table = new xmldb_table('payments'); + $index = new xmldb_index('component-paymentarea-componentid', XMLDB_INDEX_NOTUNIQUE, + ['component', 'paymentarea', 'componentid']); + + // Conditionally launch add index component-paymentarea-componentid. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2021052500.27); + } + return true; } diff --git a/payment/amd/build/gateways_modal.min.js b/payment/amd/build/gateways_modal.min.js index 27798093fe87081d11bf08d627a69802f4d9cab9..ddfcd0e870d44c627e452fbbeca057d66d2bb7f1 100644 GIT binary patch delta 97 zcmX@3^hasKYj*B}#LC>%ypqJC)WppX*hQF-1tvdW7u)=wa}f)tPP%4wSz?hw#^nDj rL7RW^En$?&uuaq}2I{oV&?_j)Pfjf^)=1V#(Mi=w(@CGaLtqL3aOEY3 delta 65 zcmeyPbVg~zYxd2I9Gpy>6}Z;2uxnPAB^D{9PyWvmv{{3H38PfHZK7UrYF>$Tx?Vw1 VesXGYu|~2^icYFd+T<4kQvmsT7byS$ diff --git a/payment/amd/build/gateways_modal.min.js.map b/payment/amd/build/gateways_modal.min.js.map index 1a72198c48e07a5ebe9de9fcc843b07477997f54..a9028616e0851ecfa8a4ddb0766b2f0d9e22dc69 100644 GIT binary patch delta 1063 zcmaJ=O-~a+7|ue_(hmwE0xESaJ!qMx1^GOgnb|2_0Rf?cL=OeZw%zV-rCYuZ-nf`( z)H#bM?!!+=WHpS*gzCOQ>R`BcsqIiz~^eu*50>5}h0IhM- zz7QEENXJ+g9)^~1+L9R)1Tn1RNq*A4a=Ap7AZ(G62vvv%1rERFUso#pnZN&H5 z{K&uBvrExD#>;}ONXi6dG0s{%5JLe*^{GG3t z9|uyc)F%9+Z(vztz*1nFAj)8woHVAI&V!`QW3mNkXobC*pcXHCHbvL8AWDF ztHh8qhiR3J@le-oWt>lR4c_$%y!T$D9-~)0=`nh3k2DzXNXFd;;~uL&@A?&N-Z?%L zNz@M`v9^w;MMRHnJ|2nI-w%Gd5Yji*^?cc}jjHN8{8ewyjiwD%@8_(%>TIcQA+P3O oW!0k1SK~Joogc*Gie5jCCzRf%0;HaAH2*R5@;|YD{sMje4XmX)BLDyZ delta 794 zcmZuvxo%TY5Y;nsz+3E>uqX>cBrYKDdch8hmFjNTPCzkoOdt}46OjF4`PuOjfqN>X zU}*|IfDZu4G<*UQ1ynRpL?RW40(!*EHAqO%+;`8+nVB=^-BKSc1fu`yTxm|kVjD0bk@KP&rR2LiMfKebWL<5+r%-C;8{3cY3sUR z4N6?sNN#J*@&q`FCLj?MrOB+COp&AbwqC{1p5DkxBY9b}rr`WG22orGVv(~}29gn- zdPucg(czIDJ#9AW>w;rE9B0o4~hLb!OnBV_WX%o0G7DJFRBB-v*eQi diff --git a/payment/amd/build/repository.min.js b/payment/amd/build/repository.min.js index 707571d6aa6d052f9ad7c2c149525d7bc49c72d0..7e70376fa00c3b72a642b642379ffdf69ef183d1 100644 GIT binary patch delta 44 ycmdnPe1>_#Hzu8wiQf{W3lb}HQ}ap^i&7JejbqTIk7;+R7VLan}{ZxnW7Y{qm-Li zP>`9IUaVxLWajNy?&PRb>f#tWajlVlrIWLcv!i2`6Nsz?v4eBmbbK8h{hf8f9UZ-$ zbvzv%eL#|-Kq6=|BcqO+pH8r&V=zc8#1TlkxB>}RH-C`&aFD30ACPp5021!#q8=bo zH(wwTI60P4iZN_+1!E`+WAWs3tg=SMB}G6-)+!)*G$v0$OTnQau_!TDp*pWLHz~EK sRsrGl$^TdsFqMBo8dq|DZb5!tYFFo7 zM<>|PF&HT97~%*dU0i{LtDCbIM>Z^92(AlUFfHF@|hD${5PR7(7{; oO;*@5uOu}+wWwABVdvy5wn<1%m@LdbWpY0|CsSt1 { @@ -126,7 +127,8 @@ const show = async(rootNode, { } }); - const gateways = await getGatewaysSupportingCurrency(rootNode.dataset.component, rootNode.dataset.componentid); + const gateways = await getGatewaysSupportingCurrency(rootNode.dataset.component, rootNode.dataset.paymentarea, + rootNode.dataset.componentid); const context = { gateways }; @@ -170,15 +172,16 @@ const updateCostRegion = async(root, defaultCost = '') => { * Process payment using the selected gateway. * * @param {string} gateway The gateway to be used for payment - * @param {string} component Name of the component that the componentid belongs to - * @param {number} componentid An internal identifier that is used by the component + * @param {string} component Name of the component that the componentId belongs to + * @param {string} paymentArea Name of the area in the component that the componentId belongs to + * @param {number} componentId An internal identifier that is used by the component * @param {string} description Description of the payment * @param {processPaymentCallback} callback The callback function to call when processing is finished * @returns {Promise} */ -const processPayment = async(gateway, component, componentid, description, callback) => { +const processPayment = async(gateway, component, paymentArea, componentId, description, callback) => { const paymentMethod = await import(`pg_${gateway}/gateways_modal`); - paymentMethod.process(component, componentid, description, callback); + paymentMethod.process(component, paymentArea, componentId, description, callback); }; /** diff --git a/payment/amd/src/repository.js b/payment/amd/src/repository.js index 5199c336a7c..d81c79af2e7 100644 --- a/payment/amd/src/repository.js +++ b/payment/amd/src/repository.js @@ -27,16 +27,18 @@ import Ajax from 'core/ajax'; /** * Returns the list of gateways that can process payments in the given currency. * - * @param {String} component - * @param {Integer} componentid + * @param {string} component + * @param {string} paymentArea + * @param {number} componentId * @returns {Promise<{shortname: string, name: string, description: String}[]>} */ -export const getGatewaysSupportingCurrency = (component, componentid) => { +export const getGatewaysSupportingCurrency = (component, paymentArea, componentId) => { const request = { methodname: 'core_payment_get_gateways_for_currency', args: { component, - componentid + paymentarea: paymentArea, + componentid: componentId, } }; return Ajax.call([request])[0]; diff --git a/payment/classes/external/get_gateways_for_currency.php b/payment/classes/external/get_gateways_for_currency.php index 30df0b990ac..6a3c822e60e 100644 --- a/payment/classes/external/get_gateways_for_currency.php +++ b/payment/classes/external/get_gateways_for_currency.php @@ -43,35 +43,38 @@ class get_gateways_for_currency extends external_api { * @return external_function_parameters */ public static function execute_parameters(): external_function_parameters { - return new external_function_parameters( - ['component' => new external_value(PARAM_COMPONENT, 'Component'), - 'componentid' => new external_value(PARAM_INT, 'Component id')] - ); + return new external_function_parameters([ + 'component' => new external_value(PARAM_COMPONENT, 'Component'), + 'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'), + 'componentid' => new external_value(PARAM_INT, 'An identifier for payment area in the component') + ]); } /** * Returns the list of gateways that can process payments in the given currency. * * @param string $component + * @param string $paymentarea * @param int $componentid * @return \stdClass[] */ - public static function execute(string $component, int $componentid): array { + public static function execute(string $component, string $paymentarea, int $componentid): array { $params = external_api::validate_parameters(self::execute_parameters(), [ 'component' => $component, + 'paymentarea' => $paymentarea, 'componentid' => $componentid, ]); $list = []; - $gateways = \core_payment\helper::get_gateways_for_currency($params['component'], $params['componentid']); + $gateways = helper::get_gateways_for_currency($params['component'], $params['paymentarea'], $params['componentid']); [ 'amount' => $amount, 'currency' => $currency - ] = \core_payment\helper::get_cost($params['component'], $params['componentid']); + ] = helper::get_cost($params['component'], $params['paymentarea'], $params['componentid']); foreach ($gateways as $gateway) { - $surcharge = \core_payment\helper::get_gateway_surcharge($gateway); + $surcharge = helper::get_gateway_surcharge($gateway); $list[] = (object)[ 'shortname' => $gateway, 'name' => get_string('gatewayname', 'pg_' . $gateway), diff --git a/payment/classes/helper.php b/payment/classes/helper.php index 13fbe46d4c5..fe12f804456 100644 --- a/payment/classes/helper.php +++ b/payment/classes/helper.php @@ -63,17 +63,18 @@ class helper { * Returns the list of gateways that can process payments in the given currency. * * @param string $component + * @param string $paymentarea * @param int $componentid * @return string[] */ - public static function get_gateways_for_currency(string $component, int $componentid): array { + public static function get_gateways_for_currency(string $component, string $paymentarea, int $componentid): array { $gateways = []; [ 'amount' => $amount, 'currency' => $currency, 'accountid' => $accountid, - ] = self::get_cost($component, $componentid); + ] = self::get_cost($component, $paymentarea, $componentid); $account = new account($accountid); if (!$account->get('id') || !$account->get('enabled')) { return $gateways; @@ -141,20 +142,22 @@ class helper { * Returns the attributes to place on a pay button. * * @param string $component Name of the component that the componentid belongs to + * @param string $paymentarea * @param int $componentid An internal identifier that is used by the component * @param string $description Description of the payment * @return array */ - public static function gateways_modal_link_params(string $component, int $componentid, string $description): array { + public static function gateways_modal_link_params(string $component, string $paymentarea, int $componentid, string $description): array { [ 'amount' => $amount, 'currency' => $currency - ] = self::get_cost($component, $componentid); + ] = self::get_cost($component, $paymentarea, $componentid); return [ 'id' => 'gateways-modal-trigger', 'role' => 'button', 'data-component' => $component, + 'data-paymentarea' => $paymentarea, 'data-componentid' => $componentid, 'data-cost' => self::get_cost_as_string($amount, $currency), 'data-description' => $description, @@ -165,12 +168,13 @@ class helper { * Asks the cost from the related component. * * @param string $component Name of the component that the componentid belongs to + * @param string $paymentarea * @param int $componentid An internal identifier that is used by the component * @return array['amount' => float, 'currency' => string, 'accountid' => int] * @throws \moodle_exception */ - public static function get_cost(string $component, int $componentid): array { - $cost = component_class_callback("$component\\payment\\provider", 'get_cost', [$componentid]); + public static function get_cost(string $component, string $paymentarea, int $componentid): array { + $cost = component_class_callback("$component\\payment\\provider", 'get_cost', [$paymentarea, $componentid]); if ($cost === null || !is_array($cost) || !array_key_exists('amount', $cost) || !array_key_exists('currency', $cost) || !array_key_exists('accountid', $cost) ) { @@ -184,13 +188,15 @@ class helper { * Returns the gateway configuration for given component and gateway * * @param string $component + * @param string $paymentarea * @param int $componentid * @param string $gatewayname * @return array * @throws \moodle_exception */ - public static function get_gateway_configuration(string $component, int $componentid, string $gatewayname): array { - $x = self::get_cost($component, $componentid); + public static function get_gateway_configuration(string $component, string $paymentarea, int $componentid, + string $gatewayname): array { + $x = self::get_cost($component, $paymentarea, $componentid); $gateway = null; $account = new account($x['accountid']); if ($account && $account->get('enabled')) { @@ -208,12 +214,14 @@ class helper { * @uses \core_payment\local\callback\provider::deliver_order() * * @param string $component Name of the component that the componentid belongs to + * @param string $paymentarea * @param int $componentid An internal identifier that is used by the component * @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference * @return bool Whether successful or not */ - public static function deliver_order(string $component, int $componentid, int $paymentid): bool { - $result = component_class_callback("$component\\payment\\provider", 'deliver_order', [$componentid, $paymentid]); + public static function deliver_order(string $component, string $paymentarea, int $componentid, int $paymentid): bool { + $result = component_class_callback("$component\\payment\\provider", 'deliver_order', + [$paymentarea, $componentid, $paymentid]); if ($result === null) { throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component); @@ -228,6 +236,7 @@ class helper { * * @param int $accountid Account id * @param string $component Name of the component that the componentid belongs to + * @param string $paymentarea * @param int $componentid An internal identifier that is used by the component * @param int $userid Id of the user who is paying * @param float $amount Amount of payment @@ -235,12 +244,13 @@ class helper { * @param string $gateway The gateway that is used for the payment * @return int */ - public static function save_payment(int $accountid, string $component, int $componentid, int $userid, float $amount, string $currency, - string $gateway): int { + public static function save_payment(int $accountid, string $component, string $paymentarea, int $componentid, int $userid, + float $amount, string $currency, string $gateway): int { global $DB; $record = new \stdClass(); $record->component = $component; + $record->paymentarea = $paymentarea; $record->componentid = $componentid; $record->userid = $userid; $record->amount = $amount; diff --git a/payment/classes/local/callback/provider.php b/payment/classes/local/callback/provider.php index 1e247cea702..e4c3b5a01d5 100644 --- a/payment/classes/local/callback/provider.php +++ b/payment/classes/local/callback/provider.php @@ -37,15 +37,17 @@ defined('MOODLE_INTERNAL') || die(); interface provider { /** - * @param int $identifier An identifier that is known to the plugin + * @param string $paymentarea + * @param int $componentid An identifier that is known to the plugin * @return array['amount' => float, 'currency' => string, 'accountid' => int] */ - public static function get_cost(int $identifier): array; + public static function get_cost(string $paymentarea, int $componentid): array; /** + * @param string $paymentarea * @param int $componentid An identifier that is known to the plugin * @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference * @return bool Whether successful or not */ - public static function deliver_order(int $componentid, int $paymentid): bool; + public static function deliver_order(string $paymentarea, int $componentid, int $paymentid): bool; } diff --git a/payment/gateway/paypal/amd/build/gateways_modal.min.js b/payment/gateway/paypal/amd/build/gateways_modal.min.js index 1993d1f8c74c43aa89577a6e63ee1e44328888e7..ddeb503d11be3ca9b12935f3bb367f23e45d0c4e 100644 GIT binary patch delta 294 zcmWkpy-LJD805st`$-{I76##D*ZT;1c3INN6+VF=S1idM#=Gpd`F3*}jucitz*e@F z+6Y$GK8NohHn#3HGYrEFGtc9<@%wlG0G52SmK0Z@0l`2G1lIrbpDv-~b1rgeRXk0r zOeR)oCv>)A6->FnOy8W%g$Z;%7j z$cDCu(Nz!q*Ju`SFY<>*wpExbNy&5pIaUUB&}EU9{xzB$V{%&-ig7{8HKgMulVM!f Q@g!U*6fykxi(YpA0aM^+xBvhE delta 256 zcmWlTzfQw27{nDO3i*T7g^hqz$u5=!?G`Hw0|PS~WQY=1gfyQM+bOC_hCahLv9hut zvBIMuHXeZ)ce=ab?sspKm&yD0;h1h#xsGerFkp(ctYaGegIm1z=T|hNqR5SmXGxvr z+(@vZNtdK%Gb$i$?k+N+oLpw2O%*SCttAv|(d~jVRseqa(+GGejn!&R=s<ke5I>UKVTXZocdnG0z{lw5b?+a3lT~y8 diff --git a/payment/gateway/paypal/amd/build/gateways_modal.min.js.map b/payment/gateway/paypal/amd/build/gateways_modal.min.js.map index 4c8f59d5aaad38765107cf677d023947498ee837..afd4b9d757961dffb145691f0ba14e217623bf1c 100644 GIT binary patch delta 938 zcmZ`&J#Q015Y<_+vE`goU<~2ISXPP%Rw5yS2zPUH_W205<=9Dp&hj_0&mRfcX9>B7 z25B4U(o({;fJ8w75`Q5D6-q}#L&MIV5$7P9*`4>^zL~kbeO@>!zIc0~U$MJ&>kx+Z zN|Gh3y>7o}iQIhi&hJ~Ek(E(|kF!5>?^Cx~pF<-lw_-~)e7l?Ap) zj!lh!@!wW0>ES{>t_J$*(1VQlv%vJ+4NX`;jgAe7#Ie>yCw}IY61n9OOUF3>7Sz>E z*^KKt;v1oZ`W<8?fD(C>K}0<*LOOFD*&e_i!D%5u$k2eszj&5#lkfsSLF&h{wn=-m zHG$vdKSF1x#jHTK4e=36d1NM(b0RP4vb=1oOed-$U`u`?WG#GG83Bv4xsDvg?X4&E22F@v?h7X)eaX5w4*1P*>NGS7 zLu5Rsaz2NDDv^yjjDJ-V5o-=|eyJY=ipk$cmoFb24%=4qX~Odq66IDsLBBnJ8C{XG zYl`W7k6uyEdsHm(_r36?*r)$-&T(ud_~)gOYsy=TA6>k}kK>`Gb}i*GtP$?4Uabay E17nUC^+x6vK8 z-+zfLgufPkT2|Y9JEcuJa_PyQLV%H08E}eZW;(jT%r*m}ssq&6T+>^8+|Y~z3ea)D zn0+qPBQo!o)tUykd6(>5Llq$3teTIJ!m{+Z#}aIqGttdNM>JE$H|1zQ(Vt-g^EEn2 z-qutBL^|Lgj`dX??wx*PCDvz>x!|g~9CH#IGSswaU`}F()=l#}wlYCg1*mfO8W3hP z1a5W{l?UK4of%5KJlhy(;&Z%Cw#00xash#N@~%rh@H8Mz@Rl$vLb;n77vHH69HBf#llqR(J#whP7G=!m$W8(3gl@byt&fO!Axx9JLGkS)xf?XwA&llYEy~ z#I~u&({@LqQyzU|;&=Y1e#F5Z&SiyHmj77x$sA{{*(v+k92GvcKSk+dt|rf)?FRnS l#=(o!zpop^bDS4K@>!c7q@9+qqaS?r9H&Mw%OW+W)qSWiBfvM)%WjYsr7cCaDPpCHP_>(?p{v+x1JUjw+-$Nn#oBd*R#q<@6nY<{g1a>f zS;TyJiIWIzPGJ=e8qDVq3?UhDjZH=@$y36EJ|72|5t6`_a17TQn%elFjcRIAf6BA< zf&Y<~6TiRR8Dm%`i?GXx w1eX+L=B4W>Bo!s*rQ{{%rdlZkPj+FqL~_IADeTE&a3|>~z?qpTlhZju0YlO_ga7~l diff --git a/payment/gateway/paypal/amd/src/gateways_modal.js b/payment/gateway/paypal/amd/src/gateways_modal.js index eab6336f468..9858b637579 100644 --- a/payment/gateway/paypal/amd/src/gateways_modal.js +++ b/payment/gateway/paypal/amd/src/gateways_modal.js @@ -45,20 +45,21 @@ const showModalWithPlaceholder = async() => { /** * Process the payment. * - * @param {string} component Name of the component that the componentid belongs to - * @param {number} componentid An internal identifier that is used by the component + * @param {string} component Name of the component that the componentId belongs to + * @param {string} paymentArea The area of the component that the componentId belongs to + * @param {number} componentId An internal identifier that is used by the component * @param {string} description Description of the payment * @param {processCallback} callback The callback function to call when processing is finished * @returns {Promise} */ -export const process = async(component, componentid, description, callback) => { +export const process = async(component, paymentArea, componentId, description, callback) => { const [ modal, paypalConfig, ] = await Promise.all([ showModalWithPlaceholder(), - Repository.getConfigForJs(component, componentid), + Repository.getConfigForJs(component, paymentArea, componentId), ]); const currency = paypalConfig.currency; const amount = paypalConfig.cost; // Cost with surcharge. @@ -104,7 +105,8 @@ export const process = async(component, componentid, description, callback) => { methodname: 'pg_paypal_create_transaction_complete', args: { component, - componentid, + paymentarea: paymentArea, + componentid: componentId, orderid: data.orderID, }, }])[0] diff --git a/payment/gateway/paypal/amd/src/repository.js b/payment/gateway/paypal/amd/src/repository.js index fff839a1f2a..50cfc413670 100644 --- a/payment/gateway/paypal/amd/src/repository.js +++ b/payment/gateway/paypal/amd/src/repository.js @@ -27,12 +27,19 @@ import Ajax from 'core/ajax'; /** * Return the PayPal JavaScript SDK URL. * - * @returns {Promise<{clientid: String, brandname: String}>} + * @param {string} component Name of the component that the componentid belongs to + * @param {string} paymentArea The area of the component that the componentid belongs to + * @param {number} componentId An internal identifier that is used by the component + * @returns {Promise<{clientid: string, brandname: string}>} */ -export const getConfigForJs = (component, componentid) => { +export const getConfigForJs = (component, paymentArea, componentId) => { const request = { methodname: 'pg_paypal_get_config_for_js', - args: {component, componentid}, + args: { + component, + paymentarea: paymentArea, + componentid: componentId, + }, }; return Ajax.call([request])[0]; diff --git a/payment/gateway/paypal/classes/external/get_config_for_js.php b/payment/gateway/paypal/classes/external/get_config_for_js.php index 134784240fa..a467f9204c3 100644 --- a/payment/gateway/paypal/classes/external/get_config_for_js.php +++ b/payment/gateway/paypal/classes/external/get_config_for_js.php @@ -45,24 +45,29 @@ class get_config_for_js extends external_api { */ public static function execute_parameters(): external_function_parameters { return new external_function_parameters([ - 'component' => new external_value(PARAM_COMPONENT, ''), - 'componentid' => new external_value(PARAM_INT, ''), + 'component' => new external_value(PARAM_COMPONENT, 'Component'), + 'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'), + 'componentid' => new external_value(PARAM_INT, 'An identifier for payment area in the component'), ]); } /** - * Returns the full URL of the PayPal JavaScript SDK. + * Returns the config values required by the PayPal JavaScript SDK. * + * @param string $component + * @param string $paymentarea + * @param int $componentid * @return string[] */ - public static function execute($component, $componentid): array { + public static function execute(string $component, string $paymentarea, int $componentid): array { self::validate_parameters(self::execute_parameters(), [ 'component' => $component, + 'paymentarea' => $paymentarea, 'componentid' => $componentid, ]); - $config = helper::get_gateway_configuration($component, $componentid, 'paypal'); - $cost = helper::get_cost($component, $componentid); + $config = helper::get_gateway_configuration($component, $paymentarea, $componentid, 'paypal'); + $cost = helper::get_cost($component, $paymentarea, $componentid); $surcharge = helper::get_gateway_surcharge('paypal'); return [ diff --git a/payment/gateway/paypal/classes/external/transaction_complete.php b/payment/gateway/paypal/classes/external/transaction_complete.php index 56016a8bc09..9186b6f41a6 100644 --- a/payment/gateway/paypal/classes/external/transaction_complete.php +++ b/payment/gateway/paypal/classes/external/transaction_complete.php @@ -47,7 +47,8 @@ class transaction_complete extends external_api { public static function execute_parameters() { return new external_function_parameters([ 'component' => new external_value(PARAM_COMPONENT, 'The component name'), - 'componentid' => new external_value(PARAM_INT, 'The item id in the context of the component'), + 'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'), + 'componentid' => new external_value(PARAM_INT, 'The item id in the context of the component area'), 'orderid' => new external_value(PARAM_TEXT, 'The order id coming back from PayPal'), ]); } @@ -57,27 +58,29 @@ class transaction_complete extends external_api { * This function does not take cost as a parameter as we cannot rely on any provided value. * * @param string $component Name of the component that the componentid belongs to + * @param string $paymentarea * @param int $componentid An internal identifier that is used by the component * @param string $orderid PayPal order ID * @return array */ - public static function execute(string $component, int $componentid, string $orderid): array { + public static function execute(string $component, string $paymentarea, int $componentid, string $orderid): array { global $USER, $DB; self::validate_parameters(self::execute_parameters(), [ 'component' => $component, + 'paymentarea' => $paymentarea, 'componentid' => $componentid, 'orderid' => $orderid, ]); - $config = (object)helper::get_gateway_configuration($component, $componentid, 'paypal'); + $config = (object)helper::get_gateway_configuration($component, $paymentarea, $componentid, 'paypal'); $sandbox = $config->environment == 'sandbox'; [ 'amount' => $amount, 'currency' => $currency, 'accountid' => $accountid, - ] = payment_helper::get_cost($component, $componentid); + ] = payment_helper::get_cost($component, $paymentarea, $componentid); // Add surcharge if there is any. $surcharge = helper::get_gateway_surcharge('paypal'); @@ -99,8 +102,8 @@ class transaction_complete extends external_api { $success = true; // Everything is correct. Let's give them what they paid for. try { - $paymentid = payment_helper::save_payment((int)$accountid, $component, $componentid, (int) $USER->id, - $amount, $currency, 'paypal'); + $paymentid = payment_helper::save_payment((int) $accountid, $component, $paymentarea, $componentid, + (int) $USER->id, $amount, $currency, 'paypal'); // Store PayPal extra information. $record = new \stdClass(); @@ -109,7 +112,7 @@ class transaction_complete extends external_api { $DB->insert_record('pg_paypal', $record); - payment_helper::deliver_order($component, $componentid, $paymentid); + payment_helper::deliver_order($component, $paymentarea, $componentid, $paymentid); } catch (\Exception $e) { debugging('Exception while trying to process payment: ' . $e->getMessage(), DEBUG_DEVELOPER); $success = false; diff --git a/payment/tests/accounts_test.php b/payment/tests/accounts_test.php index 318850c85bf..d273441fe55 100644 --- a/payment/tests/accounts_test.php +++ b/payment/tests/accounts_test.php @@ -118,8 +118,8 @@ class accounts_testcase extends advanced_testcase { // Delete account with payments - it will be archived. $this->setAdminUser(); $account = helper::save_payment_account((object)['name' => 'Test 1', 'idnumber' => '']); - $DB->insert_record('payments', ['accountid' => $account->get('id'), 'component' => 'test', 'componentid' => 1, - 'userid' => $USER->id]); + $DB->insert_record('payments', ['accountid' => $account->get('id'), 'component' => 'test', 'paymentarea' => 'test', + 'componentid' => 1, 'userid' => $USER->id]); helper::delete_payment_account(account::get_record(['id' => $account->get('id')])); $this->assertEquals(1, $DB->get_field('payment_accounts', 'archived', ['id' => $account->get('id')])); diff --git a/payment/tests/generator/lib.php b/payment/tests/generator/lib.php index 804882765a8..2cf19a1c8f5 100644 --- a/payment/tests/generator/lib.php +++ b/payment/tests/generator/lib.php @@ -70,11 +70,12 @@ class core_payment_generator extends component_generator_base { $data['gateway'] = reset($gateways); } - $id = $DB->insert_record('payments', $data + - ['component' => 'testcomponent', - 'componentarea' => 'teatarea', - 'componentid' => 0, - 'currency' => 'AUD']); + $id = $DB->insert_record('payments', $data + [ + 'component' => 'testcomponent', + 'paymentarea' => 'teatarea', + 'componentid' => 0, + 'currency' => 'AUD' + ]); return $id; } -- 2.43.0