MDL-69166 core_payment: Add paymentarea to the payment subsystem
[moodle.git] / payment / gateway / paypal / classes / external / transaction_complete.php
CommitLineData
4403b8f6
SR
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This class contains a list of webservice functions related to the PayPal payment gateway.
19 *
20 * @package pg_paypal
21 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25declare(strict_types=1);
26
27namespace pg_paypal\external;
28
895f38cc 29use core_payment\helper;
4403b8f6
SR
30use external_api;
31use external_function_parameters;
32use external_value;
048a6521
SR
33use core_payment\helper as payment_helper;
34use pg_paypal\paypal_helper;
4403b8f6
SR
35
36defined('MOODLE_INTERNAL') || die();
37
38require_once($CFG->libdir . '/externallib.php');
39
40class transaction_complete extends external_api {
41
42 /**
43 * Returns description of method parameters.
44 *
45 * @return external_function_parameters
46 */
47 public static function execute_parameters() {
48 return new external_function_parameters([
49 'component' => new external_value(PARAM_COMPONENT, 'The component name'),
7d10f352
SR
50 'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'),
51 'componentid' => new external_value(PARAM_INT, 'The item id in the context of the component area'),
4403b8f6 52 'orderid' => new external_value(PARAM_TEXT, 'The order id coming back from PayPal'),
4403b8f6
SR
53 ]);
54 }
55
56 /**
57 * Perform what needs to be done when a transaction is reported to be complete.
58 * This function does not take cost as a parameter as we cannot rely on any provided value.
59 *
60 * @param string $component Name of the component that the componentid belongs to
7d10f352 61 * @param string $paymentarea
4403b8f6
SR
62 * @param int $componentid An internal identifier that is used by the component
63 * @param string $orderid PayPal order ID
4403b8f6
SR
64 * @return array
65 */
7d10f352 66 public static function execute(string $component, string $paymentarea, int $componentid, string $orderid): array {
4403b8f6
SR
67 global $USER, $DB;
68
69 self::validate_parameters(self::execute_parameters(), [
70 'component' => $component,
7d10f352 71 'paymentarea' => $paymentarea,
4403b8f6
SR
72 'componentid' => $componentid,
73 'orderid' => $orderid,
4403b8f6
SR
74 ]);
75
7d10f352 76 $config = (object)helper::get_gateway_configuration($component, $paymentarea, $componentid, 'paypal');
bbb887f6 77 $sandbox = $config->environment == 'sandbox';
4403b8f6
SR
78
79 [
80 'amount' => $amount,
895f38cc
MG
81 'currency' => $currency,
82 'accountid' => $accountid,
7d10f352 83 ] = payment_helper::get_cost($component, $paymentarea, $componentid);
4403b8f6 84
1882bb47 85 // Add surcharge if there is any.
15a4e4c8
MG
86 $surcharge = helper::get_gateway_surcharge('paypal');
87 $amount = helper::get_cost_with_surcharge($amount, $surcharge, $currency);
1882bb47 88
048a6521
SR
89 $paypalhelper = new paypal_helper($config->clientid, $config->secret, $sandbox);
90 $orderdetails = $paypalhelper->get_order_details($orderid);
4403b8f6
SR
91
92 $success = false;
93 $message = '';
94
048a6521
SR
95 if ($orderdetails) {
96 if ($orderdetails['status'] == paypal_helper::ORDER_STATUS_APPROVED &&
97 $orderdetails['intent'] == paypal_helper::ORDER_INTENT_CAPTURE) {
98 $item = $orderdetails['purchase_units'][0];
99 if ($item['amount']['value'] == $amount && $item['amount']['currency_code'] == $currency) {
100 $capture = $paypalhelper->capture_order($orderid);
101 if ($capture && $capture['status'] == paypal_helper::CAPTURE_STATUS_COMPLETED) {
102 $success = true;
103 // Everything is correct. Let's give them what they paid for.
104 try {
7d10f352
SR
105 $paymentid = payment_helper::save_payment((int) $accountid, $component, $paymentarea, $componentid,
106 (int) $USER->id, $amount, $currency, 'paypal');
048a6521
SR
107
108 // Store PayPal extra information.
109 $record = new \stdClass();
110 $record->paymentid = $paymentid;
111 $record->pp_orderid = $orderid;
112
113 $DB->insert_record('pg_paypal', $record);
15a4e4c8 114
7d10f352 115 payment_helper::deliver_order($component, $paymentarea, $componentid, $paymentid);
048a6521
SR
116 } catch (\Exception $e) {
117 debugging('Exception while trying to process payment: ' . $e->getMessage(), DEBUG_DEVELOPER);
118 $success = false;
119 $message = get_string('internalerror', 'pg_paypal');
120 }
121 } else {
4403b8f6 122 $success = false;
048a6521 123 $message = get_string('paymentnotcleared', 'pg_paypal');
4403b8f6 124 }
048a6521 125 } else {
4403b8f6 126 $success = false;
048a6521
SR
127 $message = get_string('amountmismatch', 'pg_paypal');
128 }
129 } else {
130 $success = false;
131 $message = get_string('paymentnotcleared', 'pg_paypal');
4403b8f6
SR
132 }
133 } else {
134 // Could not capture authorization!
135 $success = false;
048a6521 136 $message = get_string('cannotfetchorderdatails', 'pg_paypal');
4403b8f6
SR
137 }
138
139 return [
140 'success' => $success,
141 'message' => $message,
142 ];
143 }
144
145 /**
146 * Returns description of method result value.
147 *
148 * @return external_function_parameters
149 */
150 public static function execute_returns() {
151 return new external_function_parameters([
152 'success' => new external_value(PARAM_BOOL, 'Whether everything was successful or not.'),
15a4e4c8 153 'message' => new external_value(PARAM_RAW, 'Message (usually the error message).'),
4403b8f6
SR
154 ]);
155 }
156}