MDL-69166 enrol_fee: Show a warning when no currency is supported
[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
29use external_api;
30use external_function_parameters;
31use external_value;
048a6521
SR
32use core_payment\helper as payment_helper;
33use pg_paypal\paypal_helper;
4403b8f6
SR
34
35defined('MOODLE_INTERNAL') || die();
36
37require_once($CFG->libdir . '/externallib.php');
38
39class transaction_complete extends external_api {
40
41 /**
42 * Returns description of method parameters.
43 *
44 * @return external_function_parameters
45 */
46 public static function execute_parameters() {
47 return new external_function_parameters([
48 'component' => new external_value(PARAM_COMPONENT, 'The component name'),
49 'componentid' => new external_value(PARAM_INT, 'The item id in the context of the component'),
50 'orderid' => new external_value(PARAM_TEXT, 'The order id coming back from PayPal'),
4403b8f6
SR
51 ]);
52 }
53
54 /**
55 * Perform what needs to be done when a transaction is reported to be complete.
56 * This function does not take cost as a parameter as we cannot rely on any provided value.
57 *
58 * @param string $component Name of the component that the componentid belongs to
59 * @param int $componentid An internal identifier that is used by the component
60 * @param string $orderid PayPal order ID
4403b8f6
SR
61 * @return array
62 */
048a6521 63 public static function execute(string $component, int $componentid, string $orderid): array {
4403b8f6
SR
64 global $USER, $DB;
65
66 self::validate_parameters(self::execute_parameters(), [
67 'component' => $component,
68 'componentid' => $componentid,
69 'orderid' => $orderid,
4403b8f6
SR
70 ]);
71
72 $config = get_config('pg_paypal');
bbb887f6 73 $sandbox = $config->environment == 'sandbox';
4403b8f6
SR
74
75 [
76 'amount' => $amount,
77 'currency' => $currency
048a6521 78 ] = payment_helper::get_cost($component, $componentid);
4403b8f6 79
1882bb47
SR
80 // Add surcharge if there is any.
81 if ($config->surcharge) {
82 $amount += $amount * $config->surcharge / 100;
83 }
84
048a6521
SR
85 $paypalhelper = new paypal_helper($config->clientid, $config->secret, $sandbox);
86 $orderdetails = $paypalhelper->get_order_details($orderid);
4403b8f6
SR
87
88 $success = false;
89 $message = '';
90
048a6521
SR
91 if ($orderdetails) {
92 if ($orderdetails['status'] == paypal_helper::ORDER_STATUS_APPROVED &&
93 $orderdetails['intent'] == paypal_helper::ORDER_INTENT_CAPTURE) {
94 $item = $orderdetails['purchase_units'][0];
95 if ($item['amount']['value'] == $amount && $item['amount']['currency_code'] == $currency) {
96 $capture = $paypalhelper->capture_order($orderid);
97 if ($capture && $capture['status'] == paypal_helper::CAPTURE_STATUS_COMPLETED) {
98 $success = true;
99 // Everything is correct. Let's give them what they paid for.
100 try {
101 payment_helper::deliver_order($component, $componentid);
102
103 $paymentid = payment_helper::save_payment($component, $componentid, (int) $USER->id, $amount,
104 $currency, 'paypal');
105
106 // Store PayPal extra information.
107 $record = new \stdClass();
108 $record->paymentid = $paymentid;
109 $record->pp_orderid = $orderid;
110
111 $DB->insert_record('pg_paypal', $record);
112 } catch (\Exception $e) {
113 debugging('Exception while trying to process payment: ' . $e->getMessage(), DEBUG_DEVELOPER);
114 $success = false;
115 $message = get_string('internalerror', 'pg_paypal');
116 }
117 } else {
4403b8f6 118 $success = false;
048a6521 119 $message = get_string('paymentnotcleared', 'pg_paypal');
4403b8f6 120 }
048a6521 121 } else {
4403b8f6 122 $success = false;
048a6521
SR
123 $message = get_string('amountmismatch', 'pg_paypal');
124 }
125 } else {
126 $success = false;
127 $message = get_string('paymentnotcleared', 'pg_paypal');
4403b8f6
SR
128 }
129 } else {
130 // Could not capture authorization!
131 $success = false;
048a6521 132 $message = get_string('cannotfetchorderdatails', 'pg_paypal');
4403b8f6
SR
133 }
134
135 return [
136 'success' => $success,
137 'message' => $message,
138 ];
139 }
140
141 /**
142 * Returns description of method result value.
143 *
144 * @return external_function_parameters
145 */
146 public static function execute_returns() {
147 return new external_function_parameters([
148 'success' => new external_value(PARAM_BOOL, 'Whether everything was successful or not.'),
149 'message' => new external_value(PARAM_TEXT, 'Message (usually the error message).', VALUE_OPTIONAL),
150 ]);
151 }
152}