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