Working on Paypal REST integration

This commit is contained in:
David Bomba 2023-06-21 21:11:41 +10:00
parent f647139918
commit 7a4cfbe7cc
3 changed files with 69 additions and 200 deletions

View File

@ -100,20 +100,35 @@ class PayPalRestPaymentDriver extends BaseDriver
public function processPaymentResponse($request)
{
$this->init();
nlog($request->all());
$response = json_decode($request['gateway_response'], true);
$order_id = $response['orderID'];
if($response['status'] == 'COMPLETED'){
nlog($order_id);
$data = [
'payment_type' => PaymentType::PAYPAL,
'amount' => $response['purchase_units'][0]['amount']['value'],
'transaction_reference' => $response['purchase_units'][0]['payments']['captures'][0]['id'],
'gateway_type_id' => GatewayType::PAYPAL,
];
$r = $this->gatewayRequest("/v2/checkout/orders/{$order_id}/capture", 'post', []);
$payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
dd($r->body());
SystemLogger::dispatch(
['response' => $response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_PAYPAL,
$this->client,
$this->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
else {
throw new PaymentFailed('Payment failed. Please try again.', 401);
}
}
private function getClientToken(): string
@ -141,10 +156,10 @@ class PayPalRestPaymentDriver extends BaseDriver
[
"description" =>ctrans('texts.invoice_number').'# '.$invoice->number,
"invoice_id" => $invoice->number,
'reference_id' => 'PUHF',
'description' => 'Sporting Goods',
'custom_id' => 'CUST-HighFashions',
'soft_descriptor' => 'HighFashions',
// 'reference_id' => 'PUHF',
// 'description' => 'Sporting Goods',
// 'custom_id' => 'CUST-HighFashions',
// 'soft_descriptor' => 'HighFashions',
"amount" => [
"value" => (string)$data['amount_with_fee'],
"currency_code"=> $this->client->currency()->code,

View File

@ -68,6 +68,8 @@ trait Inviteable
$qr = $writer->writeString($this->getPaymentLink(), 'utf-8');
return "<div>$qr</div>";
return "<svg class='pqrcode' viewBox='0 0 200 200' width='200' height='200' x='0' y='0' xmlns='http://www.w3.org/2000/svg'>
<rect x='0' y='0' width='100%' height='100%' />{$qr}</svg>";
}

View File

@ -21,87 +21,7 @@
<div class="alert alert-failure mb-4" hidden id="errors"></div>
<div id="paypal-button-container" class="paypal-button-container"></div>
<div class="card_container">
<form id="card-form">
<label for="card-number">Card Number</label>
<div id="card-number" class="card_field"></div>
<div style="display: flex; flex-direction: row;">
<div>
<label for="expiration-date">Expiration Date</label>
<div id="expiration-date" class="card_field"></div>
</div>
<div style="margin-left: 10px;">
<label for="cvv">CVV</label>
<div id="cvv" class="card_field"></div>
</div>
</div>
<label for="card-holder-name">Name on Card</label>
<input
type="text"
id="card-holder-name"
name="card-holder-name"
autocomplete="off"
placeholder="card holder name"
/>
<div>
<label for="card-billing-address-street">Billing Address</label>
<input
type="text"
id="card-billing-address-street"
name="card-billing-address-street"
autocomplete="off"
placeholder="street address"
/>
</div>
<div>
<label for="card-billing-address-unit">&nbsp;</label>
<input
type="text"
id="card-billing-address-unit"
name="card-billing-address-unit"
autocomplete="off"
placeholder="unit"
/>
</div>
<div>
<input
type="text"
id="card-billing-address-city"
name="card-billing-address-city"
autocomplete="off"
placeholder="city"
/>
</div>
<div>
<input
type="text"
id="card-billing-address-state"
name="card-billing-address-state"
autocomplete="off"
placeholder="state"
/>
</div>
<div>
<input
type="text"
id="card-billing-address-zip"
name="card-billing-address-zip"
autocomplete="off"
placeholder="zip / postal code"
/>
</div>
<div>
<input
type="text"
id="card-billing-address-country"
name="card-billing-address-country"
autocomplete="off"
placeholder="country code"
/>
</div>
<br /><br />
<button value="submit" id="submit" class="btn">Pay</button>
</form>
@endsection
@section('gateway_footer')
@ -113,105 +33,16 @@
<script>
if (paypal.HostedFields.isEligible()) {
paypal.HostedFields.render({
createOrder: function(data, actions) {
return "{!! $order_id !!}"
},
styles: {
".valid": {
color: "green",
},
".invalid": {
color: "red",
},
},
fields: {
number: {
selector: "#card-number",
placeholder: "4111 1111 1111 1111",
},
cvv: {
selector: "#cvv",
placeholder: "123",
},
expirationDate: {
selector: "#expiration-date",
placeholder: "MM/YY",
},
},
}).then((cardFields) => {
document.querySelector("#card-form").addEventListener("submit", (event) => {
event.preventDefault();
cardFields
.submit({
// Cardholder's first and last name
cardholderName: document.getElementById("card-holder-name").value,
// Billing Address
billingAddress: {
// Street address, line 1
streetAddress: document.getElementById(
"card-billing-address-street"
).value,
// Street address, line 2 (Ex: Unit, Apartment, etc.)
extendedAddress: document.getElementById(
"card-billing-address-unit"
).value,
// State
region: document.getElementById("card-billing-address-state").value,
// City
locality: document.getElementById("card-billing-address-city")
.value,
// Postal Code
postalCode: document.getElementById("card-billing-address-zip")
.value,
// Country Code
countryCodeAlpha2: document.getElementById(
"card-billing-address-country"
).value,
},
})
.then(() => {
fetch(`/api/orders/${orderId}/capture`, {
method: "post",
})
.then((res) => res.json())
.then((orderData) => {
// Two cases to handle:
// (1) Other non-recoverable errors -> Show a failure message
// (2) Successful transaction -> Show confirmation or thank you
// This example reads a v2/checkout/orders capture response, propagated from the server
// You could use a different API or structure for your 'orderData'
const errorDetail =
Array.isArray(orderData.details) && orderData.details[0];
if (errorDetail) {
var msg = "Sorry, your transaction could not be processed.";
if (errorDetail.description)
msg += "\n\n" + errorDetail.description;
if (orderData.debug_id) msg += " (" + orderData.debug_id + ")";
return alert(msg); // Show a failure message
}
// Show a success message or redirect
alert("Transaction completed!");
});
})
.catch((err) => {
alert("Payment could not be captured! " + JSON.stringify(err));
});
});
});
}
else {
document.querySelector("#card-form").style = "display: none";
paypal.Buttons({
env: 'sandbox', // sandbox | production
client: {
sandbox: "{{ $gateway->company_gateway->getConfigField('clientId') }}",
},
env: "{{ $gateway->company_gateway->getConfigField('testMode') ? 'sandbox' : 'production' }}",
client: {
@if($gateway->company_gateway->getConfigField('testMode'))
sandbox: "{{ $gateway->company_gateway->getConfigField('clientId') }}"
@else
production: "{{ $gateway->company_gateway->getConfigField('clientId') }}"
@endif
},
createOrder: function(data, actions) {
return "{!! $order_id !!}"
},
@ -219,16 +50,37 @@ client: {
return actions.order.capture().then(function(details) {
console.log(details);
document.getElementById("gateway_response").value =JSON.stringify( details );
document.getElementById("server_response").submit();
});
// document.getElementById("gateway_response").value =JSON.stringify( data );
// document.getElementById("server_response").submit();
}
}).render('#paypal-button-container');
});
var FUNDING_SOURCES = [
// paypal.FUNDING.PAYPAL,
paypal.FUNDING.VENMO,
paypal.FUNDING.PAYLATER,
paypal.FUNDING.CREDIT,
paypal.FUNDING.CARD,
]
// Loop over each funding source
FUNDING_SOURCES.forEach(function (fundingSource) {
// Initialize the buttons
var button = paypal.Buttons({
fundingSource: fundingSource,
})
// Check if the button is eligible
if (button.isEligible()) {
// Render the standalone button for that funding source
button.render('#paypal-button-container')
}
})
}
</script>
@endpush