Skip to content

Add support for paymaya payment intent attach. #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":3601:{a:2:{s:7:"defects";a:10:{s:56:"Luigel\Paymongo\Tests\PaymentTest::it_can_create_payment";i:4;s:93:"Luigel\Paymongo\Tests\PaymentTest::it_cannot_create_payment_when_token_is_used_more_than_once";i:3;s:83:"Luigel\Paymongo\Tests\PaymentTest::it_cannot_create_payment_when_token_is_not_valid";i:4;s:60:"Luigel\Paymongo\Tests\PaymentTest::it_can_retrieve_a_payment";i:4;s:80:"Luigel\Paymongo\Tests\PaymentTest::it_can_not_retrieve_a_payment_with_invalid_id";i:3;s:58:"Luigel\Paymongo\Tests\PaymentTest::it_can_get_all_payments";i:4;s:54:"Luigel\Paymongo\Tests\TokenTest::it_can_retrieve_token";i:4;s:88:"Luigel\Paymongo\Tests\PaymongoValidateSignatureTest::it_will_not_allow_invalid_signature";i:3;s:82:"Luigel\Paymongo\Tests\PaymongoValidateSignatureTest::it_will_allow_valid_signature";i:5;s:52:"Luigel\Paymongo\Tests\BaseModelTest::it_can_set_data";i:4;}s:5:"times";a:31:{s:69:"Luigel\Paymongo\Tests\PaymentIntentTest::it_can_create_payment_intent";d:0.543;s:93:"Luigel\Paymongo\Tests\PaymentIntentTest::it_cannot_create_payment_intent_when_data_is_invalid";d:0.426;s:69:"Luigel\Paymongo\Tests\PaymentIntentTest::it_can_cancel_payment_intent";d:0.861;s:87:"Luigel\Paymongo\Tests\PaymentIntentTest::it_can_attach_payment_method_to_payment_intent";d:1.744;s:90:"Luigel\Paymongo\Tests\PaymentIntentTest::it_cannot_attach_payment_intent_with_invalid_data";d:0.444;s:71:"Luigel\Paymongo\Tests\PaymentIntentTest::it_can_retrieve_payment_intent";d:0.945;s:69:"Luigel\Paymongo\Tests\PaymentMethodTest::it_can_create_payment_method";d:0.544;s:90:"Luigel\Paymongo\Tests\PaymentMethodTest::it_cannot_create_payment_method_with_invalid_data";d:0.539;s:71:"Luigel\Paymongo\Tests\PaymentMethodTest::it_can_retrieve_payment_method";d:1.019;s:56:"Luigel\Paymongo\Tests\PaymentTest::it_can_create_payment";d:1.41;s:93:"Luigel\Paymongo\Tests\PaymentTest::it_cannot_create_payment_when_token_is_used_more_than_once";d:6.54;s:83:"Luigel\Paymongo\Tests\PaymentTest::it_cannot_create_payment_when_token_is_not_valid";d:1.581;s:60:"Luigel\Paymongo\Tests\PaymentTest::it_can_retrieve_a_payment";d:1.617;s:80:"Luigel\Paymongo\Tests\PaymentTest::it_can_not_retrieve_a_payment_with_invalid_id";d:0.989;s:58:"Luigel\Paymongo\Tests\PaymentTest::it_can_get_all_payments";d:1.1;s:62:"Luigel\Paymongo\Tests\SourceTest::it_can_create_a_gcash_source";d:0.426;s:65:"Luigel\Paymongo\Tests\SourceTest::it_can_create_a_grab_pay_source";d:0.442;s:54:"Luigel\Paymongo\Tests\TokenTest::it_can_retrieve_token";d:0.896;s:59:"Luigel\Paymongo\Tests\WebhookTest::it_can_list_all_webhooks";d:0.406;s:58:"Luigel\Paymongo\Tests\WebhookTest::it_can_retrieve_webhook";d:0.798;s:58:"Luigel\Paymongo\Tests\WebhookTest::it_can_create_a_webhook";d:1.378;s:68:"Luigel\Paymongo\Tests\WebhookTest::it_can_disable_and_enable_webhook";d:2.322;s:52:"Luigel\Paymongo\Tests\TokenTest::it_can_create_token";d:0.574;s:73:"Luigel\Paymongo\Tests\TokenTest::it_cannot_create_token_with_invalid_data";d:0.456;s:95:"Luigel\Paymongo\Tests\UnauthorizedTest::it_expects_unauthorized_exception_with_invalid_api_keys";d:0.445;s:88:"Luigel\Paymongo\Tests\PaymongoValidateSignatureTest::it_will_not_allow_invalid_signature";d:0.008;s:82:"Luigel\Paymongo\Tests\PaymongoValidateSignatureTest::it_will_allow_valid_signature";d:0.005;s:52:"Luigel\Paymongo\Tests\BaseModelTest::it_can_set_data";d:0.002;s:72:"Luigel\Paymongo\Tests\AmounToIntegerTest::it_can_convert_without_decimal";d:0.049;s:78:"Luigel\Paymongo\Tests\AmounToIntegerTest::it_can_convert_with_in_tenth_decimal";d:0.001;s:82:"Luigel\Paymongo\Tests\AmounToIntegerTest::it_can_convert_with_in_hundredth_decimal";d:0.001;}}}
{"version":1,"defects":{"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_can_attach_paymaya_payment_method_to_payment_intent":4},"times":{"Luigel\\Paymongo\\Tests\\AmounToIntegerTest::it_can_convert_without_decimal":0.052,"Luigel\\Paymongo\\Tests\\AmounToIntegerTest::it_can_convert_with_in_tenth_decimal":0.002,"Luigel\\Paymongo\\Tests\\AmounToIntegerTest::it_can_convert_with_in_hundredth_decimal":0.001,"Luigel\\Paymongo\\Tests\\BaseModelTest::it_can_set_data":0.002,"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_can_create_payment_intent":0.549,"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_cannot_create_payment_intent_when_data_is_invalid":0.388,"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_can_cancel_payment_intent":0.83,"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_can_attach_payment_method_to_payment_intent":1.62,"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_cannot_attach_payment_intent_with_invalid_data":0.402,"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_can_retrieve_payment_intent":0.829,"Luigel\\Paymongo\\Tests\\PaymentMethodTest::it_can_create_payment_method":0.484,"Luigel\\Paymongo\\Tests\\PaymentMethodTest::it_cannot_create_payment_method_with_invalid_data":0.41,"Luigel\\Paymongo\\Tests\\PaymentMethodTest::it_can_retrieve_payment_method":0.924,"Luigel\\Paymongo\\Tests\\PaymentTest::it_can_create_payment":1.098,"Luigel\\Paymongo\\Tests\\PaymentTest::it_cannot_create_payment_when_token_is_used_more_than_once":1.455,"Luigel\\Paymongo\\Tests\\PaymentTest::it_cannot_create_payment_when_token_is_not_valid":1.493,"Luigel\\Paymongo\\Tests\\PaymentTest::it_can_retrieve_a_payment":1.48,"Luigel\\Paymongo\\Tests\\PaymentTest::it_can_not_retrieve_a_payment_with_invalid_id":0.888,"Luigel\\Paymongo\\Tests\\PaymentTest::it_can_get_all_payments":0.965,"Luigel\\Paymongo\\Tests\\PaymongoValidateSignatureTest::it_will_not_allow_invalid_signature":0.007,"Luigel\\Paymongo\\Tests\\PaymongoValidateSignatureTest::it_will_allow_valid_signature":0.007,"Luigel\\Paymongo\\Tests\\SourceTest::it_can_create_a_gcash_source":0.407,"Luigel\\Paymongo\\Tests\\SourceTest::it_can_create_a_grab_pay_source":0.412,"Luigel\\Paymongo\\Tests\\TokenTest::it_can_create_token":0.464,"Luigel\\Paymongo\\Tests\\TokenTest::it_cannot_create_token_with_invalid_data":0.388,"Luigel\\Paymongo\\Tests\\TokenTest::it_can_retrieve_token":0.853,"Luigel\\Paymongo\\Tests\\UnauthorizedTest::it_expects_unauthorized_exception_with_invalid_api_keys":0.39,"Luigel\\Paymongo\\Tests\\WebhookTest::it_can_list_all_webhooks":0.4,"Luigel\\Paymongo\\Tests\\WebhookTest::it_can_retrieve_webhook":0.786,"Luigel\\Paymongo\\Tests\\PaymentIntentTest::it_can_attach_paymaya_payment_method_to_payment_intent":1.322}}
40 changes: 39 additions & 1 deletion docs/docs/Usage/payment-intents.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $cancelledPaymentIntent = $paymentIntent->cancel();
Attach the payment intent.

### Sample

1. Simple attaching of payment method in payment intent.
```php
use Luigel\Paymongo\Facades\Paymongo;

Expand All @@ -62,6 +62,44 @@ $paymentIntent = Paymongo::paymentIntent()->find('pi_hsJNpsRFU1LxgVbxW4YJHRs6');
$successfulPayment = $paymentIntent->attach('pm_wr98R2gwWroVxfkcNVZBuXg2');
```

2. Attaching paymaya payment method in payment intent.
```php
$paymentIntent = Paymongo::paymentIntent()
->create([
'amount' => 100,
'payment_method_allowed' => [
'paymaya', 'card' // <--- Make sure to add paymaya here.
],
'payment_method_options' => [
'card' => [
'request_three_d_secure' => 'automatic',
],
],
'description' => 'This is a test payment intent',
'statement_descriptor' => 'LUIGEL STORE',
'currency' => 'PHP',
]);

$paymentMethod = Paymongo::paymentMethod()
->create([
'type' => 'paymaya', // <--- and payment method type should be paymaya
'billing' => [
'address' => [
'line1' => 'Somewhere there',
'city' => 'Cebu City',
'state' => 'Cebu',
'country' => 'PH',
'postal_code' => '6000',
],
'name' => 'Rigel Kent Carbonel',
'email' => 'rigel20.kent@gmail.com',
'phone' => '0935454875545',
],
]);

$attachedPaymentIntent = $paymentIntent->attach($paymentMethod->id, 'http://example.com/success'); // <--- And the second parameter should be the return_url.
```

## Get Payment Intent

You can retrieve a Payment Intent by providing a payment intent ID. The prefix for the id is `pi_` followed by a unique hash representing the payment. Just pass the payment id to `find($paymentIntentId)` method.
Expand Down
9 changes: 7 additions & 2 deletions src/Models/PaymentIntent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ public function cancel()
return (new Paymongo)->paymentIntent()->cancel($this);
}

public function attach($paymentMethodId)
/**
* @param string $paymentMethodId
* @param string|null $returnUrl
* @return \Luigel\Paymongo\Models\BaseModel
*/
public function attach($paymentMethodId, $returnUrl = null)
{
return (new Paymongo)->paymentIntent()->attach($this, $paymentMethodId);
return (new Paymongo)->paymentIntent()->attach($this, $paymentMethodId, $returnUrl);
}
}
26 changes: 18 additions & 8 deletions src/Traits/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Database\Eloquent\Model;
use Luigel\Paymongo\Exceptions\BadRequestException;
use Luigel\Paymongo\Exceptions\NotFoundException;
use Luigel\Paymongo\Exceptions\PaymentErrorException;
use Luigel\Paymongo\Exceptions\UnauthorizedException;
use Luigel\Paymongo\Models\BaseModel;
use Luigel\Paymongo\Models\PaymentIntent;
use Luigel\Paymongo\Models\Webhook;

Expand All @@ -21,7 +21,7 @@ trait Request
* Request a create to API.
*
* @param array $payload
* @return Model
* @return BaseModel
*/
public function create($payload)
{
Expand All @@ -45,7 +45,7 @@ public function create($payload)
* Request to retrieve a resource in API.
*
* @param string $payload
* @return Model
* @return BaseModel
*/
public function find($payload)
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public function all()
*
* @param Webhook $webhook
* @param array $payload
* @return Model
* @return BaseModel
*/
public function update(Webhook $webhook, array $payload)
{
Expand All @@ -113,7 +113,7 @@ public function update(Webhook $webhook, array $payload)
* Cancels the payment intent.
*
* @param PaymentIntent $intent
* @return Model
* @return BaseModel
*/
public function cancel(PaymentIntent $intent)
{
Expand All @@ -135,14 +135,19 @@ public function cancel(PaymentIntent $intent)
*
* @param PaymentIntent $intent
* @param string $paymentMethodId
* @return Model
* @param null|string $returnUrl
* @return BaseModel
*/
public function attach(PaymentIntent $intent, $paymentMethodId)
public function attach(PaymentIntent $intent, $paymentMethodId, $returnUrl = null)
{
$this->method = 'POST';
$this->apiUrl = $this->apiUrl.$intent->id.'/attach';
$this->payload = ['payment_method' => $paymentMethodId];

if ($returnUrl) {
$this->payload = array_merge($this->payload, ['return_url' => $returnUrl]);
}

$this->formRequestData();
$this->setOptions([
'headers' => [
Expand All @@ -158,7 +163,12 @@ public function attach(PaymentIntent $intent, $paymentMethodId)
/**
* Send request to API.
*
* @return mixed|Throwable
* @return mixed
*
* @throws \Luigel\Paymongo\Exceptions\BadRequestException
* @throws \Luigel\Paymongo\Exceptions\UnauthorizedException
* @throws \Luigel\Paymongo\Exceptions\PaymentErrorException
* @throws \Luigel\Paymongo\Exceptions\NotFoundException
*/
protected function request()
{
Expand Down
44 changes: 44 additions & 0 deletions tests/PaymentIntentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,48 @@ public function it_can_retrieve_payment_intent()

$this->assertEquals($paymentIntent, $retrievedPaymentIntent);
}

/** @test */
public function it_can_attach_paymaya_payment_method_to_payment_intent()
{
$paymentIntent = Paymongo::paymentIntent()
->create([
'amount' => 100,
'payment_method_allowed' => [
'paymaya', 'card',
],
'payment_method_options' => [
'card' => [
'request_three_d_secure' => 'automatic',
],
],
'description' => 'This is a test payment intent',
'statement_descriptor' => 'LUIGEL STORE',
'currency' => 'PHP',
]);

$paymentMethod = Paymongo::paymentMethod()
->create([
'type' => 'paymaya',
'billing' => [
'address' => [
'line1' => 'Somewhere there',
'city' => 'Cebu City',
'state' => 'Cebu',
'country' => 'PH',
'postal_code' => '6000',
],
'name' => 'Rigel Kent Carbonel',
'email' => 'rigel20.kent@gmail.com',
'phone' => '0935454875545',
],
]);

$attachedPaymentIntent = $paymentIntent->attach($paymentMethod->id, 'http://example.com/success');

$this->assertEquals($paymentIntent->id, $attachedPaymentIntent->id);
$this->assertEquals('awaiting_next_action', $attachedPaymentIntent->status);
$this->assertEquals('redirect', $attachedPaymentIntent->next_action['type']);
$this->assertNotNull($attachedPaymentIntent->next_action['redirect']['url']);
}
}