Can't run handleCreatePaymentRedirect method #7218
Unanswered
cusursuz
asked this question in
Developer Q&A
Replies: 2 comments 17 replies
-
Hey @cusursuz, Can you please clarify what you mean by you can't run handleCreatePaymentRedirect method, are you receiving an error? If so, please share. Thanks! |
Beta Was this translation helpful? Give feedback.
13 replies
-
Return url begin with return_url=http://customgateway.local?give-listener=give-gateway&..... |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I implemented custom gateway conform
https://givewp.com/documentation/developers/how-to-build-a-gateway-add-on-for-givewp/
can't run handleCreatePaymentRedirect method after receiving data from bank. Signature is valid and has not expired
How can run it?
thanks
`<php
require_once(DIR . '/vendor/autoload.php');
use Give\Donations\Models\Donation;
use Give\Donations\Models\DonationNote;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\Http\Response\Types\RedirectResponse;
use Give\Framework\PaymentGateways\Commands\PaymentRefunded;
use Give\Framework\PaymentGateways\Commands\RedirectOffsite;
use Give\Framework\PaymentGateways\PaymentGateway;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Maib\MaibApi\MaibClient;
/**
@inheritdoc
*/
class ExampleGatewfayOffsiteClass extends PaymentGateway
{
private $client;
public function __construct()
{
$options = [
'base_uri' => MaibClient::MAIB_TEST_BASE_URI,
'debug' => false,
'verify' => true,
'cert' => [MaibClient::MAIB_TEST_CERT_URL, MaibClient::MAIB_TEST_CERT_PASS],
'ssl_key' => MaibClient::MAIB_TEST_CERT_KEY_URL,
'config' => [
'curl' => [
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
]
]
];
}
/**
*/
public $secureRouteMethods = [
'handleCreatePaymentRedirect',
];
/**
*/
public static function id(): string
{
return 'maib-gateway-offsite';
}
/**
*/
public function getId(): string
{
return self::id();
}
/**
*/
public function getName(): string
{
return __('MAIB Gateway - Offsite', 'maib-give');
}
/**
*/
public function getPaymentMethodLabel(): string
{
return __('MAIB Gateway - Offsite', 'maib-give');
}
/**
*/
public function getLegacyFormFieldMarkup(int $formId, array $args): string
{
// For an offsite gateway, this is just help text that displays on the form.
return "
You will be taken away to MAIB to complete the donation!
}
/**
Get Sample Data to send to a gateway.
*/
public function getParameters(Donation $donation): array
{
// this is just an example but,
// you would need to get your own merchant ID and key from the gateway.
// Typically, these are retrieved from the GiveWP gateway settings when the admin
// sets up the gateway using their account.
return [
'cancel_url' => give_get_failed_transaction_uri(),
'notify_url' => get_site_url() . '/?give-listener=MAIB',
'name_first' => $donation->firstName,
'name_last' => $donation->lastName,
'email_address' => $donation->email,
'm_payment_id' => $donation->id,
'amount' => $donation->amount->formatToDecimal(),
'item_name' => $donation->formTitle,
'item_description' => sprintf(__('Donation via GiveWP, ID %s', 'maib-give'), $donation->id),
];
}
/**
@inheritdoc
*/
public function createPayment(Donation $donation, $gatewayData)
{
// The Parameters required to use MaibClient methods
$amount = $donation->amount->formatToDecimal(); // The amount of the transaction
//MDL $donation->amount->getCurrency()->getCode();
$currency = 498; // The currency of the transaction - is the 3 digits code of currency from ISO 4217
$clientIpAddr = $donation->donorIp; // The client IP address
$description = 'Donation';// . $donation->currency; // The description of the transaction
$lang = 'en'; // The language for the payment gateway
// Other parameters
$redirect_url = MaibClient::MAIB_TEST_REDIRECT_URL . '?trans_id=';
$registerSmsTransaction = $this->client->registerSmsTransaction($amount, $currency, $clientIpAddr, $description, $lang);
$sms_transaction_id = $registerSmsTransaction["TRANSACTION_ID"];
$sms_redirect_url = $redirect_url . $sms_transaction_id;
$donation->gatewayTransactionId = $sms_transaction_id;
$donation->save();
// Step 1: generate a secure gateway route URL that will be used to redirect the donor to the gateway.
// The args you pass it will be available in the $queryParams parameter in the secureRouteMethod that you defined below.
$returnUrl = $this->generateSecureGatewayRouteUrl(
'handleCreatePaymentRedirect',
$donation->id,
[
'givewp-donation-id' => $donation->id,
'givewp-success-url' => urlencode(give_get_success_page_uri()),
// this would likely be a transaction ID from the gateway upon return.
'givewp-gateway-transaction-id' => urlencode($sms_transaction_id),//'123456789',
]
);
// Step 2: Get the parameters you need to send to the gateway.
$queryParams = array_merge(
$this->getParameters($donation),
// this just an example of a return url parameter, this will be specific to your gateway.
['return_url' => $returnUrl, 'transaction_id' => $sms_transaction_id]
);
// Step 3: Generate the URL to redirect the donor to, using the queryParams you created that contains the secure gateway route URL.
$gatewayUrl = add_query_arg($queryParams, $sms_redirect_url);
// Step 4: Return a RedirectOffsite command with the generated URL to redirect the donor to the gateway.
return new RedirectOffsite($gatewayUrl);
}
/**
An example of using a secureRouteMethod for extending the Gateway API to handle a redirect.
@throws Exception
*/
protected function handleCreatePaymentRedirect(array $queryParams): RedirectResponse
{
// Step 1: Use the $queryParams to get the data you need to complete the donation.
$donationId = $queryParams['givewp-donation-id'];
$gatewayTransactionId = $queryParams['givewp-gateway-transaction-id'];
// $successUrl = $queryParams['givewp-success-url'];
// Step 2: Typically you will find the donation from the donation ID.
/** @var Donation $donation */
$donation = Donation::find($donationId);
$clientIpAddr = $donation->donorIp;
$transaction_id = $queryParams['trans_id'];
$transactionResult = $this->client->getTransactionResult($transaction_id, $clientIpAddr);
error_log(print_r($transactionResult), true);
if ($transactionResult["RESULT_CODE"] == '000') {
$content = "Donation Completed";
$donation->status = DonationStatus::COMPLETE();
$redirectUrl = $queryParams['givewp-success-url'];
} elseif ($transactionResult['RESULT_CODE'] == "116") {
$content = "Tranzactie nereusita. Mijloace insuficiente";
$donation->status = DonationStatus::FAILED();
$redirectUrl = $queryParams['cancel_url'];
} else {
$content = "Tranzactie nereusita. Pentru mai multe detalii rugam sa va adresati bancii emitente a cardului la nr de telefon indicat pe versoul cardului";
$donation->status = DonationStatus::FAILED();
$redirectUrl = $queryParams['cancel_url'];
}
// Step 3: Use the Donation model to update the donation based on the transaction and response from the gateway.
// $donation->status = DonationStatus::COMPLETE();
// $donation->gatewayTransactionId = $gatewayTransactionId;
$donation->save();
DonationNote::create([
'donationId' => $donation->id,
'content' => $content,//'Donation Completed from MAIB Gateway Offsite.'
]);
// Step 4: Return a RedirectResponse to the GiveWP success page.
return new RedirectResponse($redirectUrl);
//return new RedirectResponse($successUrl);
}
/**
*/
public function refundDonation(Donation $donation): PaymentRefunded
{
// Step 1: refund the donation with your gateway.
// Step 2: return a command to complete the refund.
return new PaymentRefunded();
}
}
`
Beta Was this translation helpful? Give feedback.
All reactions